链接http://acm.hdu.edu.cn/showproblem.php?pid=1005
大意:求
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
此类题首先考虑找一般规律,即循环节、打表
二是数论 如卡特兰数等。。。
对于公式 f[n] = A * f[n-1] + B * f[n-2]; 后者只有7 * 7 = 49 种可能,为什么这么说,因为对于f[n-1] 或者 f[n-2] 的取值只有 0,1,2,3,4,5,6 这7个数,A,B又是固定的,所以就只有49种可能值了。
注意f[0]的处理,循环节从1到[i-2],即f[0]=上个循环节的最后一个数f[i-2]
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int a,b,n;
int f[1000];
while(cin>>a>>b>>n&&(a||b||n)){
f[0]=f[1]=f[2]=1;
int i;
for(i=3;i<1000;++i){
f[i]=(a*f[i-1]+b*f[i-2])%7;
if(f[i]==1&&f[i-1]==1){
break;
}
}
f[0]=f[i-2];
cout<<f[n%(i-2)]<<endl;
}
return 0;
}