[关闭]
@Chilling 2016-08-11T13:25:52.000000Z 字数 776 阅读 861

HDU-2802: F(N)(循环节)

数论


Description



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

Input

Each test case contains a single integer . 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

分析: 这个题需要找循环节,还是看了别人怎么写,不然想不到竟然是循环,先求出循环节,是多少个数一轮循环,将数保存在数组里面,输入n之后模以一个循环节,输出F(n)即可。


  1. #include<stdio.h>
  2. #define LL long long
  3. #define mod 2009
  4. LL f[100000];
  5. int main()
  6. {
  7. LL n,i,repetend;
  8. f[1]=1,f[2]=7;
  9. for(i=3;;i++)
  10. {
  11. LL t=i%mod;
  12. f[i]=(f[i-2]+3*t*t-3*t+1)%mod;
  13. //f[i]=f[i-2]-(i-1)^3+i^3,化简得到上面的式子
  14. if(f[i-1]==1&&f[i]==7)
  15. break;
  16. }
  17. repetend=i-2; //每4018一次循环
  18. /* for(i=1;i<=repetend+2;i++)
  19. printf("%lld ",f[i]);*/
  20. while(scanf("%lld",&n),n)
  21. {
  22. n=n%repetend;
  23. printf("%lld\n",f[n]);
  24. }
  25. return 0;
  26. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注