hdu 1757 A Simple Math Problem 矩阵快速幂
快速幂套模板呀!!!!
矩阵快速幂模板
#include<bits/stdc++.h> using namespace std; const int N = 12; int n,Mod; struct Mat { int mat[N][N]; }; Mat operator *(Mat a, Mat b) { Mat c; memset(c.mat, 0, sizeof(c.mat)); for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) if(a.mat[i][k]) for(int j = 0; j < n; ++j) if(b.mat[k][j]) c.mat[i][j] = (c.mat[i][j] +a.mat[i][k] * b.mat[k][j])%Mod; return c; } Mat operator ^(Mat a, int k) { Mat c; memset(c.mat,0,sizeof(c.mat)); for(int i = 0; i < n; ++i) c.mat[i][i]=1; for(; k; k >>= 1) { if(k&1) c = c*a; a = a*a; } return c; } int main() { // freopen("Input.txt","r",stdin); int k; int aa[12]; n=10; while(~scanf("%d%d",&k,&Mod)) { for(int i=0;i<10;i++) scanf("%d",aa+i); if(k<10) { printf("%d\n",k); continue; } Mat a; memset(a.mat,0,sizeof(a.mat)); for(int i=0;i<10;i++) a.mat[i][0]=aa[i]; for(int i=1;i<10;i++) a.mat[i-1][i]=1; a=a^(k-9); int ans=0; for(int i=0;i<10;i++) ans=(ans+a.mat[i][0]*(9-i))%Mod; printf("%d\n",ans); } return 0; }