#include <bits/stdc++.h>using namespace STD;typedef Long LongLL;Const intn=Ten;intKt,m;structmatrix{intMp[n][n];}; Matrix Mul (Matrix A,matrix b) {intI,j,k; Matrix C; for(i=0; i<Ten; i++) for(j=0; j<Ten; J + +) {c.mp[i][j]=0; for(k=0; k<Ten; k++) {c.mp[i][j]= (c.mp[i][j]+a.mp[i][k]*b.mp[k][j])%m; } }returnC;} MatrixPOW(Matrix T,intx) {if(x==1)returnT Matrix C; C=POW(t,x/2); C=mul (C,C);if(x&1)returnMul (c,t);Else returnC;}intMain () {intI,j,ans; Matrix a,t; while(~scanf("%d%d", &kt,&m)) { for(i=0; i<n;i++)scanf("%d", &a.mp[0][i]);if(kt<Ten) {printf("%d\n", kt%m);Continue; } for(i=1; i<n;i++) for(j=0; j<n;j++) {if(i==j+1) a.mp[i][j]=1;Elsea.mp[i][j]=0; } t=POW(a,kt-9); for(i=0, ans=0; i<n;i++) {//printf ("%d\n", ans);Ans= (ans+t.mp[0][i]* (n-i-1))%m; }printf("%d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1757 A simple Math problem matrix optimization + fast Power