思路:
十进制快速幂。
1 #include <stdio.h>//sprintf 2 #include <cstdlib>////malloc exit strcat itoa system("cls") 3 #include <iostream>//pair 4 #include <fstream> 5 #include <bitset> 6 //#include <map> https://ac.nowcoder.com/acm/contest/885/B 7 #include <vector> 8 #include <stack> 9 #include <set> 10 #include <string.h>//strstr 11 #include <string> 12 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 13 #include <cmath> 14 #include <queue>//priority_queue<long long, vector<long long>, greater<long long> > q; 15 #include <vector> 16 //#include <math.h> 17 //#include <windows.h> https://www.nitacm.com/problem_show.php?pid=222 18 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 19 using namespace std; 20 #define pr printf 21 #define sc scanf 22 #define fo(a,b,c) for((a)=(b);(a)<=(c);(a)++)//register long long i 23 #define fr(a,b,c) for((a)=(b);(a)>=(c);(a)--) 24 #define mem(a,b) memset((a),(b),sizeof((a))) 25 const double e=2.718281828; 26 const double PI=acos(-1.0); 27 const double ESP=1e-6; 28 const long long inf=99999999; 29 const long long N=1000006; 30 long long mod; 31 const long long MAXN=3; 32 33 long long x0,x1,a,b,MOD; 34 struct MAT 35 { 36 long long mat[MAXN][MAXN]; 37 MAT() 38 { 39 mat[1][1]=mat[2][2]=1;//对角矩阵 E; 40 mat[2][1]=mat[1][2]=0; 41 } 42 MAT operator*(const MAT &a)const 43 { 44 MAT b; 45 long long i,j,k; 46 mem(b.mat,0); 47 fo(i,1,MAXN-1) 48 { 49 fo(j,1,MAXN-1) 50 { 51 fo(k,1,MAXN-1) 52 { 53 b.mat[i][j]=(b.mat[i][j]+mat[i][k]*a.mat[k][j]); 54 b.mat[i][j]%=mod; 55 } 56 } 57 } 58 return b; 59 } 60 }transfer; 61 62 MAT Mqpow(MAT x,long long n) 63 { 64 struct MAT temp; 65 while(n) 66 { 67 if(n&1) 68 temp=temp*x; 69 x=x*x; 70 n>>=1; 71 } 72 return temp; 73 } 74 75 char n[N]; 76 struct MAT mark[30],start; 77 int main() 78 { 79 sc("%lld%lld%lld%lld%s%lld",&x0,&x1,&a,&b,n,&MOD); 80 // mem(transfer.mat,0); 81 mod=MOD; 82 transfer.mat[1][1]=a,transfer.mat[1][2]=b; 83 transfer.mat[2][1]=1,transfer.mat[2][2]=0; 84 start.mat[1][1]=x1,start.mat[2][1]=x0; 85 mark[1]=transfer; 86 for(int i=2;i<=9;++i) 87 mark[i]=transfer*mark[i-1]; 88 int l=strlen(n); 89 struct MAT ans; 90 for(int i=0;i<=l-1;++i) 91 { 92 ans=Mqpow(ans,10); 93 ans=ans*mark[n[i]-'0']; 94 } 95 start=ans*start; 96 pr("%lld ",start.mat[2][1]%MOD); 97 return 0; 98 }