Ideas see Hihocoder, with the Kuangbin matrix fast Power, a ac,6 of a stroke.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < string> #include <stack> #include <cmath> #include <queue> #include <set> #include <map > #define for (i,s,t) for (int i = (s); I <= (t); ++i) #define Lchild o<<1#define rchild o<<1|1typedef Lo ng Long ll;typedef unsigned long long ull;using namespace Std;const int Inf=0x3f3f3f3f;const ll Inf=0x3f3f3f3f3f3f3f3f;con St int Maxn=1e6+5;int n;ll dp[2][8];struct matrix{long Long mat[8][8];}; Matrix Mul (Matrix A,matrix b) {matrix ret; for (int i=0;i<8;i++) for (int j=0;j<8;j++) {ret.mat[i][j]=0; for (int k=0;k<8;k++) {ret.mat[i][j]+=a.mat[i][k]*b.mat[k][j]; ret.mat[i][j]%= (12357); }} return ret;} Matrix Pow_m (Matrix A,int N) {matrix ret; memset (ret.mat,0,sizeof (Ret.mat)); for (int i=0;i<8;++i) ret.mat[i][i]=1; Matrix TEmp=a; while (n) {if (n&1) Ret=mul (ret,temp); Temp=mul (temp,temp); n>>=1; } return ret;} int main () {//freopen ("In.txt", "R", stdin); Matrix A; memset (a.mat,0,sizeof A.mat); for (int i=0;i<8;++i) a.mat[i][7-i]=1; A.mat[3][7]=1; A.mat[6][7]=1; A.mat[7][3]=1; A.mat[7][6]=1; ll b[8]={0,0,0,0,0,0,0,1}; while (cin>>n) {Matrix tmp = pow_m (a,n); ll ans = 0; for (int i=0;i<8;++i) {ans + = b[i]*tmp.mat[i][7]; } cout<<ans<<endl; } return 0;}
Hihocoder 1151 Domino cover problem two (Matrix fast power)