Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
此题可用数学方法解决,复杂度O(n)。此题可用数学方法解决,复杂度O(n)。 观察规律如下: 1 1 1*1 1 2*1-1 1 1 2 1*1+1 2 2*1 1 2 1 4 2*2 3 2*2-1 1 2 2 1 6 2*2+2 4 2*2 1 2 3 2 1 9 3*3 5 2*3-1 1 2 3 3 2 1 12 3*3+3 6 2*3 参考程序: #include <cstdio> #include <cmath> using namespace std; int main() { long n,x,y; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d %d",&x,&y); long m=y-x,l=long(sqrt(m)); if(m==0) printf("0\n"); else if(m==l*l) printf("%d\n",2*l-1); else if(m<=l*l+l) printf("%d\n",2*l); else printf("%d\n",2*(l+1)-1); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator