就是一个模板而已。 为了保存一下代码~
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<set>
#include<string>
#define INF 1000000005
#define LL long long
using namespace std;
const int MAXN =5;
struct Matrix
{
int n,m;
int a[MAXN][MAXN];
void clear(int x=0,int y=0)
{
n=x;
m=y;
memset(a,0,sizeof(a));
}
Matrix operator * (const Matrix &b)const
{
Matrix tmp;
tmp.clear(n,b.m);
for(int i=0; i<n; ++i)
for(int j=0; j<b.m; ++j)
for(int k=0; k<m; ++k)
{
tmp.a[i][j]+=a[i][k]*b.a[k][j];
tmp.a[i][j]%=7;
}
return tmp;
}
void show()
{
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
};
Matrix quickPow(Matrix mat,int n)
{
Matrix res;
res.clear(2,2);
res.a[0][0]=res.a[1][1]=1;
while(n)
{
if(n&1) res=res*mat;
mat=mat*mat;
n=n>>1;
}
return res;
}
int main()
{
int A,B,n;
while(scanf("%d%d%d",&A,&B,&n)!=EOF)
{
if(!A&&!B&&!n) break;
if(n==1||n==2)
{
puts("1");
continue;
}
Matrix mat;
mat.clear(2,2);
mat.a[0][0]=0;
mat.a[0][1]=B;
mat.a[1][0]=1;
mat.a[1][1]=A;
Matrix f;
f.clear(1,2);
f.a[0][0]=1;
f.a[0][1]=1;
n-=2;
Matrix s=f*quickPow(mat,n);
printf("%d\n",(s.a[0][1])%7);
}
return 0;
}