扩大
缩小

NYOJ427Number Sequence

Number Sequence

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
描述
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).
 
输入
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.
输出
For each test case, print the value of f(n) on a single line.
样例输入
1 1 3
1 2 10
0 0 0
样例输出
2
5
View Code
 1  
 2 #include<stdio.h>
 3 int main()
 4 {
 5     int f[100000];
 6     int a,b,n,i;
 7     while(scanf("%d%d%d",&a,&b,&n)&&(a||b||n))
 8         {
 9            
10             f[1]=1;f[2]=1;
11             if(n==1||n==2){printf("1\n");continue;}
12             for(i=3;i<100000;i++)
13             {
14                f[i]=(a*f[i-1]+b*f[i-2])%7;
15                if(f[i]==1&&f[i-1]==1) break;
16             }
17             i=i-2;
18             f[0]=f[i];
19             printf("%d\n",f[n%i]);
20         }
21     return 0;
22 }
23         

 

posted on 2012-11-14 21:42  LinuxPanda  阅读(267)  评论(0编辑  收藏  举报

导航