题目:
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
解析:
1. 本题是一个找规律的题,对于%7来说保证了f(n)最多有7钟,f(n+1)也是7种,所以f(n+2)最多49种。所以49之后就是规律。下图是我打表出来的图。第一张为1 1 {1~50},循环周期为16,第二张是1,2,{1~50},循环周期是6
2. 总结打表的规律是,当出现两个连续的1时,说明此时进入下一个循环。
#include int main() { int a, b, n; int s[101] = { 0 ,1,1}; while (~scanf("%d%d%d", &a, &b, &n) && (a + b + n)) { int m = 0,i; for (i = 3; i < 100; i++) { s[i] = (a*s[i - 1] + b*s[i - 2])%7; if (s[i] == 1 && s[i - 1] == 1) break; } m = i - 2;//周期 n %= m; if(n!=0) printf("%d\n", s[n]); else printf("%d\n", s[i-2]); } return 0; }