没改LL见祖宗系列。
矩乘不难推吧。然后要用快速乘
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> using namespace std; typedef long long LL; LL mod; LL n,AA,C,X,G; struct Matrix { LL a[5][5]; Matrix(){} Matrix clear() { memset(a,0,sizeof(a)); } }; LL quick_multi(LL k,LL p) { if(k==0||p==0)return 0LL; LL A=k,ret=0; while(p!=0) { if(p%2==1)ret=(ret+A)%mod; A=(A+A)%mod;p/=2; } return ret; } Matrix multi(Matrix A,Matrix B) { Matrix C;C.clear(); for(int i=1;i<=2;i++) for(int j=1;j<=2;j++) for(int k=1;k<=2;k++) C.a[i][j]=(C.a[i][j]+quick_multi(A.a[i][k],B.a[k][j]))%mod; return C; } Matrix quick_pow(LL p) { Matrix A,ret;A.clear();ret.clear(); A.a[1][1]=1;A.a[1][2]=0; A.a[2][1]=1;A.a[2][2]=AA; ret.a[1][1]=1;ret.a[1][2]=0; ret.a[2][1]=0;ret.a[2][2]=1; while(p!=0) { if(p%2==1)ret=multi(ret,A); A=multi(A,A);p/=2; } return ret; } int main() { scanf("%lld%lld%lld%lld%lld%lld",&mod,&AA,&C,&X,&n,&G); Matrix ans;ans.clear(); ans.a[1][1]=C;ans.a[2][1]=X; ans=multi(quick_pow(n),ans); printf("%lld ",ans.a[2][1]%G); return 0; }