矩阵快速幂裸题·····
#include <cstdio> #include <iostream> #include <algorithm> #define modd 7 using namespace std; struct matrix { int m[2][2]; }; int a, b, n; matrix cash(matrix a,matrix b){ matrix tem; for (int i = 0; i < 2 ; ++i) { for (int j = 0; j < 2 ; ++j) { tem.m[i][j] = 0; for (int k = 0; k < 2; ++k) tem.m[i][j] += (a.m[i][k]*b.m[k][j])%modd; } } return tem; } int ma_pow(int n){ matrix ans, muti; ans.m[0][0] = (((a+b)*a)%modd+b)%modd ;ans.m[0][1] = (a+b)%modd; ans.m[1][0] = 1; ans.m[1][1] = 1; muti.m[0][0] = a;muti.m[0][1] = 1;muti.m[1][0] = b;muti.m[1][1] = 0; while(n){ if(n&1) ans = cash(ans,muti); muti = cash(muti,muti); n>>=1; } return ans.m[1][1]%modd; } int main(int argc, char const *argv[]) { while(~scanf("%d%d%d",&a,&b ,&n) && n != 0) printf("%d ", ma_pow(n-1)); return 0; return 0; }