自己根据矩阵快速幂的模板写的:----但超时了
#include <iostream>
#include <list>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
struct mat
{
ll m[2][2];
mat()
{
memset(m,0,sizeof(m));
}
};
mat mul(mat A,mat B)
{
mat C;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
C.m[i][j]=(C.m[i][j]+A.m[i][k]*B.m[k][j])%7;
}
}
return C;
}
mat pow(mat A,ll n)
{
mat B;
for(int i=0;i<2;i++) B.m[i][i]=1;
while(n)
{
if(n&1) B=mul(B,A);
A=mul(A,A);
n >>= 1;
}
return B;
}
int main()
{
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
if(a==0&&b==0&&n==0) break;
mat A;
A.m[0][0]=a;
A.m[0][1]=b;
A.m[1][0]=1;
mat B=pow(A,n-2);
printf("%lld\n",(B.m[0][0]+B.m[0][1])%7);
}
return 0;
}
附上别人ac代码:
#include<stdio.h>
int main(){
int a[100];
int A,B,n;
a[1] = 1;
a[2] = 1;
while(scanf("%d %d %d", &A, &B, &n) && A+B+n){
for(int i = 3 ; i <= 49 ; i++)
a[i] = (A*a[i-1] + B*a[i-2])%7;
printf("%d\n", a[n%49]);
}
return 0;
}
可见其差距,^&^