| ||||||||||
| 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 | |||||||||
不用Pell数列,另有算法0ms过掉大概大家都得到了:
(n+1)*n=2*k^2
但是注意到n+1和n没有公约数
n+1和n有一个奇数,另一个是偶数
为了满足 (n+1)*n=2*k^2,必须有:
奇数那个是 完全平方数
偶数那个是 2*完全平方数
我的算法是,枚举所有奇数的平方,他有可能是n,或者可能是n+1
#include<stdio.h>
#include<math.h>
inline int test(int n)
{
int t=int(sqrt(n));
if(t*t==n)
return t;
else
return 0;
}
main()
{
int t=0,i=3,s,k,n;
while(t<10)
{
s=i*i;
if(k=test((s-1)/2))
n=s-1;
else if(k=test((s+1)/2))
n=s;
if(k)
{
printf ("%10d%10d\n", k*i, n);
t++;
}
i+=2;
}
}
这仍然是枚举算法,复杂度O(sqrt(n))
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator