Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

此题可用数学方法解决,复杂度O(n)。

Posted by jourkliu at 2009-03-05 19:23:35 on Problem 2590
此题可用数学方法解决,复杂度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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator