题目链接:https://cn.vjudge.net/contest/306123#problem/J
思路:n超级大,若平常代码写,肯定不对。题中有f(1) = 1, f(2) = 1。若进行递推式f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7,若出现f[i-1]=1&&f[i]=1就可以跳出循环。最后特判一下n=i-2的情况。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int A,B,n,i;
int st[10000];
int main(){
while(~scanf("%d%d%d",&A,&B,&n)&&(A+B+n)){
st[1]=1,st[2]=1;
for(i=3; i<1000; i++){
st[i]=(A*st[i-1]+B*st[i-2])%7;
if(st[i]==1&&st[i-1]==1)
break;
}
n=n%(i-2);
if(n==0)
n=i-2;
printf("%d\n",st[n]);
}
return 0;
}