#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; struct node { int mp[15][15]; } init,res; long long int k,mod; void chu() { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(i == j) { res.mp[i][j] = 1; } else { res.mp[i][j] = 0; } } } for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(i-1 == j) { init.mp[i][j] = 1; } else { init.mp[i][j] = 0; } } } } struct node mult(struct node a,struct node b) { struct node c; for(int i=0; i<10; i++) { for(int j=0; j<10; j++) { c.mp[i][j] = 0; for(int k=0; k<10; k++) { c.mp[i][j] += (a.mp[i][k] * b.mp[k][j])%mod; c.mp[i][j] %= mod; } } } return c; } ; struct node Cal(long long int kk) { struct node tmp = res,x = init; while(kk) { if(kk%2 == 1) { tmp = mult(x,tmp); } x = mult(x,x); kk>>=1; } return tmp; }; int main() { while(~scanf("%lld%lld",&k,&mod)) { chu(); for(int i=0; i<10; i++) { scanf("%d",&init.mp[0][i]); } if(k<10) { printf("%d\n",k%mod); continue; } struct node tt; tt = Cal(k-9); int sum = 0; for(int i=0; i<10; i++) { sum += (tt.mp[0][i]*(9-i))%mod; } printf("%d\n",sum%mod); } return 0; }
HDU 1757 A Simple Math Problem(矩阵快速幂)
最新推荐文章于 2022-10-10 22:20:10 发布