Number Sequence--hdu1005

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 128332    Accepted Submission(s): 31212


Problem Description
A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).
 
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
 
Output
For each test case, print the value of f(n) on a single line.
 
Sample Input
1 1 3
1 2 10
0 0 0
 
Sample Output
2 5

这个题其实应该可以考虑到有循环的,看了好多博客说直接能得出来循环节为49,但是我太笨,不明白!

我还是自己找吧!一切数据都是由f[1]=1和f[2]=1演化出来的,所以当再次出现时,一定是在循环了!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int f[300];
 6 int main()
 7 {
 8     int i,j,a,b,n;
 9     f[1]=1;
10     f[2]=1;
11     while(scanf("%d%d%d",&a,&b,&n),a||b||n)
12     {
13         int t;
14         for(i=3;i<200;i++)
15         {
16             f[i]=(a*f[i-1]+b*f[i-2])%7;
17             if(f[i-1]==1&&f[i]==1)
18             break;
19 
20         }
21             i-=2;
22             n=n%i;
23             if(n==0)//当n=0时,说明正好是循环节的倍数,把它转化为一个循环的最后一个
24             n=i;
25             printf("%d
",f[n]);
26     }
27     return 0;
28  } 
原文地址:https://www.cnblogs.com/Eric-keke/p/4719122.html