题意:
已知f[1] = f[2] = 1,输入三个数a,b,n,求f[n] = (a*f[n-1]+b*f[n-2])%7的结果
分析:
f[n-1]和f[n-2]最多为7种情况(0,1,2,3,4,5,6),则它们的组合最多为49,即周期为49.
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>17:19:592016-07-25
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MON 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24
25 const int maxn = 50;
26 int f[maxn];
27
28 int main()
29 {
30 int a, b, n;
31 while(scanf("%d%d%d", &a, &b, &n)==3&&a&&b&&n)
32 {
33 int i;
34 f[1] = f[2] = 1;
35 for(i = 3; i < 50; i++ )
36 f[i] = (a*f[i-1]+b*f[i-2])%7;
37 printf("%d
", f[n%49]);
38 }
39
40 return 0;
41 }