HDOJ 1005
#include<iostream>
using namespace std;
int main()
{
int a,b,g;
while(cin>>a>>b>>g&&(a!=0&&b!=0&&g!=0))
{
int r[50]={0};
r[1]=1;
r[2]=1;
int cle[7][7]={0};
cle[1][1]=2;
int f1=1,f2=1,f;
int n=2,k;
while(n++)
{
f=(b*f1+a*f2)%7;
f1=f2;
f2=f;
r[n]=f;
if(cle[f1][f2]==0)
{
cle[f1][f2]=n;
}else
{
k=n-cle[f1][f2];
break;
}
}
if(g>cle[f1][f2])
{
cout<<r[(g-cle[f1][f2])%k+cle[f1][f2]]<<endl;
}
else
{
cout<<r[g]<<endl;
}
}
return 0;
}