struct mat{ LL a,b; mat(LL A=0,LL B=0):a(A),b(B){} bool isIdentity()const{ return a==1 && b==0; } mat operator+(const mat &rhs)const{ return mat( (a+rhs.a)%MOD, (b+rhs.b)%MOD ); } mat operator*(const mat &rhs)const{ return mat( (a*rhs.a+b*rhs.b)%MOD, (a*rhs.b+b*(rhs.a+rhs.b))%MOD); } mat power(LL b)const{ mat a=*this,ans(1,0); while(b){ if(b&1)ans=ans*a; b>>=1; a=a*a; } return ans; } };
来源题目:http://codeforces.com/contest/719/problem/E
代码来源:http://codeforces.com/contest/719/standings 的 sk_aswd
或者用下面这个模板,不过要一直用struct而已
来自http://www.cnblogs.com/heimao5027/p/5923401.html
struct Node{ LL mat[2][2]; void reset(){memset(mat, 0, sizeof(mat));} void getone(){ reset(); mat[0][0] = mat[1][1] = 1; } }; inline Node mul(Node A, Node B){ Node ans; ans.reset(); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) ans.mat[i][j] = (ans.mat[i][j] + A.mat[i][k] * B.mat[k][j]) % mod; return ans; } inline Node add(Node a, Node b){ Node ans; ans.reset(); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % mod; return ans; } inline Node kpow(LL k){ Node ans; ans.reset(); ans.mat[0][0] = ans.mat[1][1] = 1; Node A; A.mat[0][0] = A.mat[1][0] = A.mat[0][1] = 1; A.mat[1][1] = 0; while (k){ if (k & 1) ans = mul(ans, A); A = mul(A, A); k >>= 1; } return ans; }