题目链接: 题目大意:给定f[1] = 1, f[2] = 1, f[n] = ((A*f[n-1]) + (B*f[n-2])) % 7, 求f[n] (n最大100,000,000)
思路:f[n]显然都是小于7的,而f[n]又只与f[n-1]和f[n-2]有关,于是想到只有7*7种组合,一定可以找到循环节。
代码: #include #include #include using namespace std;int main(){ int vis[7][7]; int f[100]; int A,B,n; while(cin>>A>>B>>n){ if (A + B + n == 0){ break; } int t1 = 0, t2 = 0; memset(vis, 0, sizeof(vis)); memset(f, 0, sizeof(f)); int t = 2; int p1 = 1, p2 = 1; vis[1][1] = 1; f[1] = f[2] = 1; while(1){ int p3 = (A * p2 + B * p1) % 7; p1 = p2; p2 = p3; if (!vis[p1][p2]){ vis[p1][p2] = t; f[t] = p1; t ++; } else{ t1 = vis[p1][p2]; t2 = t; t ++; break; } } if (n < t1){ cout<