Meaning of the questions:
First input X0, X1, a, b and follow Xi = a * X (i-1) + b * X (i-2) (i> = 2)
After re-enter n, mod, so that you find a formula based on the value of X (n) to the modulo mod
answer:
I believe we will soon be able to quickly find the coefficient matrix of power
|X0 X1| * |0 a| = |X1 X2|
|1 b|
It is to write fast power matrix according to the above formula
But we must pay attention to the number of bits n input on the topic has long been beyond the scope of a long long, so in order to find out the coefficient matrix of power is still a problem
We observed that the power of 2 ^ 121, which is a 1,2 * 10,1 * 100
2^121 = (2^1)*(2^10)*(2^2)*(2^10)*(2^1)
According to this feature, we can put a big power several times to unpack ^ _ ^
Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int size=1e6+5; 5 char n[size]; 6 int x0,x1,a,b; 7 typedef long long LL; 8 LL mod; 9 struct mat 10 { 11 int m[3][3]; 12 mat(){for(int i=0;i<3;i++)for(int j=0;j<3;j++) m[i][j]=0;} 13 friend mat operator*(mat x,mat y) 14 { 15 mat ans; 16 for(int i=1;i<=2;i++) 17 { 18 for(int j=1;j<=2;j++) 19 { 20 for(intk = 1 ; k <= 2 ; k ++ ) 21 { 22 ans.m [i] [k] = (ans.m [i] [k] + 1LL * xm [i] [j] * ym [j] [ k])% change; 23 } 24 } 25 } 26 return year; 27 } 28 }; 29 inline quick_pow mat (mat is, int b) 30 { 31 matt years; 32 ans.m [ 1 ] [ 1 ] = 1 ; ans.m [ 2 ] [ 2 ] =1; 33 while(b) 34 { 35 if(b&1) ans=ans*a; 36 a=a*a; 37 b>>=1; 38 } 39 return ans; 40 } 41 int main() 42 { 43 scanf("%d%d%d%d",&x0,&x1,&a,&b); 44 scanf("%s%lld",n,&mod); 45 mat ori; 46 int len =strlen(n); 47 ori.m[1][2]=1,ori.m[2][1]=b;ori.m[2][2]=a; 48 mat ans; 49 ans.m[1][1]=1;ans.m[2][2]=1; 50 for(int i=len-1;i>=0;i--) 51 { 52 ans=ans*quick_pow(ori,n[i]-'0'); 53 ori=quick_pow(ori,10); 54 } 55 printf("%lld\n",(1LL*x0*ans.m[1][1]+1LL*x1*ans.m[1][2])%mod); 56 }