标签:
此题关键是要发现周期性。
#include <stdio.h> #include <string.h> int f[1000]; int compute(int a, int b, int n) { int i, cycle; memset(f, 0, 1000*sizeof(int)); f[1] = 1; f[2] = 1; for(i = 3; i < 1000; i++) { f[i] = (a * f[i-1] + b * f[i-2]) % 7; if(f[i-1] == 1 && f[i] == 1) { break; } printf("f[%d]=%d\n", i, f[i]); } cycle = i - 2; f[0] = f[cycle]; return f[n % cycle]; } int main(void) { int a, b, n; while(scanf("%d %d %d", &a, &b, &n) && (0 != a && 0 != b && 0 != n)) { printf("%d\n", compute(a, b, n)); } }
标签:
原文地址:http://www.cnblogs.com/mylinuxer/p/4335755.html