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

不用Pell数列,另有算法0ms过掉

Posted by lda at 2008-09-27 11:48:40 on Problem 1320
大概大家都得到了:
    (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:
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