HDU 2802 F(N) (找循环节)

来源:互联网 发布:租房 知乎 编辑:程序博客网 时间:2024/05/04 05:52

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2802



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
1230

Sample Output
1720

Source
HDU 2009-4 Programming Contest 

代码如下:

#include <cstdio>int F[10000];int main(){F[1] = 1;F[2] = 7;int t, mod = 2009;for(int i = 3 ; ;i++ )//找循环节的方法{F[i]=F[i-2]-(i-1)*(i-1)*(i-1)+i*i*i;F[i]%=mod;if(F[i]==7 && F[i-1] == 1)break;}int circle = i - 2;int n ;while(~scanf("%d",&n) && n){n %= circle;printf("%d\n",F[n]);}return 0;}


1 0
原创粉丝点击