hdu4565

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 100010
 17 #define For(i,a,b) for(long long i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 long long aa,bb,n,mod,tt;
 23 double t;
 24 struct matrix{
 25     long long a[5];
 26     long long b[5][5];
 27     matrix operator *(const matrix&c)const{
 28         matrix r;
 29         For(i,1,2)
 30             For(j,1,2){
 31                 r.b[i][j]=0;
 32                 For(k,1,2)
 33                     r.b[i][j]=(r.b[i][j]+b[i][k]*c.b[k][j]%mod)%mod;
 34             }
 35         return r;
 36     }
 37 }a;
 38 
 39 void in(long long &x){
 40     long long y=1;
 41     char c=g();x=0;
 42     while(c<'0'||c>'9'){
 43         if(c=='-')y=-1;
 44         c=g();
 45     }
 46     while(c<='9'&&c>='0'){
 47         x=(x<<1)+(x<<3)+c-'0';c=g();
 48     }
 49     x*=y;
 50 }
 51 void o(long long x){
 52     if(x<0){
 53         p('-');
 54         x=-x;
 55     }
 56     if(x>9)o(x/10);
 57     p(x%10+'0');
 58 }
 59 
 60 matrix ksm(matrix a,long long b){
 61     matrix r=a;
 62     b--;
 63     if(b==0)    return r;
 64     while(b%2==0){
 65         a=a*a;
 66         b>>=1;
 67     }
 68     while(b>0){
 69         if(b%2==1)
 70             r=r*a;
 71         a=a*a;
 72         b>>=1;
 73     }
 74     return r;
 75 }
 76 
 77 int main(){
 78     while(cin>>aa>>bb>>n>>mod){
 79         if(n==0){
 80             o((long long)1%mod);p('\n');
 81             continue;
 82         }
 83         a.a[1]=1;
 84         a.a[2]=0;
 85         a.b[1][1]=aa%mod;
 86         a.b[1][2]=bb%mod;
 87         a.b[2][1]=1;
 88         a.b[2][2]=aa%mod;
 89         matrix r,ans;
 90         r=ksm(a,n);
 91         For(i,1,2){
 92                 ans.a[i]=0;
 93                 For(k,1,2)
 94                     ans.a[i]=(ans.a[i]+a.a[k]*r.b[i][k]%mod)%mod;
 95             }
 96         tt=0;
 97         tt=(tt+ans.a[1])%mod;
 98         o((2*tt%mod+mod)%mod);p('\n');
 99     }
100     return 0;
101 }
View Code

 

posted @ 2019-05-07 11:04  WeiAR  阅读(190)  评论(0编辑  收藏  举报