Channel
Test instructions: Calculation (5+26√)1+2^x .
Idea: The cyclic section is (p+1) * (p-1), then the bare matrix fast power.
Code:
#include <cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespaceStd;typedefLong Longll;Const intN =2;intMOD;structmatrix{ll Mat[n][n]; Matrixoperator*(Constmatrix& m)Const{Matrix tmp; for(inti =0; i < N; i++){ for(intj =0; J < N; J + +) {Tmp.mat[i][j]=0; for(intK =0; K < N; k++) {Tmp.mat[i][j]+ = mat[i][k]*m.mat[k][j]%MOD; TMP.MAT[I][J]%=MOD; } } } returntmp; }};ll Pow (Matrix&m, ll K) { if(k = =1) return 9; K--; Matrix ans; memset (Ans.mat,0,sizeof(Ans.mat)); for(inti =0; i < N; i++) Ans.mat[i][i]=1; //printf ("%d\n", k); while(k) {if(k&1) ans= ans*m; K>>=1; M= m*m; } //out (ans);ll x = (ans.mat[0][0]*5+ans.mat[0][1]*2)%MOD; return(x *2-1)%MOD;} ll Now;ll Pow (intx) {ll res=1, both =2; while(X >0) { if(X &1) Res = (res * both)%Now ; both= (both *)%Now ; X>>=1; } returnRes;}intMain () {intT; Matrix m; scanf ("%d", &T); intCAS =0; while(t-->0) { intN; scanf ("%d%d", &n, &MOD); now= (MOD +1) * (MOD-1); ll nn= Pow (n) +1 ; m.mat[0][0] =5; m.mat[0][1] = A; m.mat[1][0] =2; m.mat[1][1] =5; printf ("Case #%d:%i64d\n", ++CAs, Pow (M, nn)); }}
View Code
"Matrix fast Power + cyclic section" HDU 5451 best Solver