Number Sequence

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
题解:打表找规律
感受:将for循环里面的1000换成maxn,会wrong,想不通。将for换成while也会wrong,懵逼!!!!!!
 1 #pragma warning(disable:4996)
 2 #include<map>
 3 #include<string>
 4 #include<cstdio>
 5 #include<bitset>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 typedef long long ll;
11 
12 const int maxn = 1005;
13 
14 int a, b, n;
15 int F[maxn];
16 
17 int main()
18 {
19     F[1] = F[2] = 1;
20     while (scanf("%d%d%d", &a, &b, &n) != EOF) {
21         if (!a && !b && !n) break;
22         int k;
23         for (k = 3; k < 1000; k++) {
24             F[k] = (a*F[k - 1] + b * F[k - 2]) % 7;
25             if (F[k] == 1 && F[k - 1] == 1) break;
26         }
27         n = n % (k - 2);
28         F[0] = F[k - 2];
29         printf("%d\n", F[n]);
30     }
31     return 0;
32 }

 

posted @ 2018-03-21 23:55  天之道,利而不害  阅读(2734)  评论(0编辑  收藏  举报