HDU 2802 F(N)(简单题,找循环解)

F(N)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2037    Accepted Submission(s): 677


Problem Description

Giving the N, can you tell me the answer of F(N)?
 

 

Input
Each test case contains a single integer N(1<=N<=10^9). The input is terminated by a set starting with N = 0. This set should not be processed.
 

 

Output
For each test case, output on a line the value of the F(N)%2009.
 

 

Sample Input
1 2 3 0
 

 

Sample Output
1 7 20
 

 

Source
 

 

Recommend
lcy
 
 
 
以后遇到这样的题目,第一感觉就应该去找循环解。
其实循环解也挺好找出来的。打表,程序中判断下就出来循环了。
本题的循环是4018.
至于为什么4018是循环,现在还没有想出来,感觉挺神奇的
/*

找循环周期

周期是4018,只要模4018就可以了。f[4018]=f[0]

*/



#include<stdio.h>

const int MOD=2009;

const int MAXN=10000;

int f[MAXN];

void init()

{

    f[1]=1;

    f[2]=7;

    for(int i=3;i<4018;i++)

    {

        f[i]=f[i-2]+3*i*i-3*i+1;

        f[i]%=MOD;

    }

}

int main()

{

    //freopen("in.txt","r",stdin);

   // freopen("out.txt","w",stdout);

    init();

    int n;

    while(scanf("%d",&n),n)

    {

        printf("%d\n",f[n%4018]);

    }

    return 0;

}

 

你可能感兴趣的:(HDU)