题目给出
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7
要求输入n(1~100000000) 输出f(n)
#include <iostream>
using namespace std;
int main()
{
int A,B,n,T,i,f,result;
int app[7][7]; //矩阵统计连续两数是否出现过
int a[50];
while(scanf("%d%d%d",&A,&B,&n)!=EOF && !(A==0&&B==0&&n==0))
{
memset(app,0,sizeof(app)); //初始化为0
a[1]=a[2]=1;
app[1][1]=1; //标记序列1,1已出现在a[1]
f=(A+B)%7;
i=2;
while(!app[a[i]][f])
{
a[++i]=f;
app[a[i-1]][a[i]]=i-1;
f=(a[i]*A+a[i-1]*B)%7;
}
if(n<=i) //n在循环前
printf("%d/n",a[n]);
else
{
T=i-app[a[i]][f]; //循环的长度
result=(n-app[a[i]][f])%T+app[a[i]][f];
printf("%d/n",a[result]);
}
}
return 0;
}