Advertisement
pdpd123

Untitled

Sep 15th, 2020
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 2147483647
  3. #define mp make_pair
  4. #define pb push_back
  5. #define int long long
  6. #define pii pair<int,int>
  7. #define mod (1000000007)
  8. using namespace std;
  9.  
  10. struct matrix{
  11.     int v[2][2];
  12.     void operator= (matrix a){
  13.         for(int i=0;i<2;i++)
  14.             for(int j=0;j<2;j++)
  15.                 v[i][j]=a.v[i][j];
  16.     }
  17.     matrix operator* (matrix a){
  18.         matrix ans;
  19.         for(int i=0;i<2;i++)
  20.             for(int j=0;j<2;j++){
  21.                 ans.v[i][j]=0;
  22.                 for(int k=0;k<2;k++)
  23.                     ans.v[i][j]+=(v[i][k]*a.v[k][j])%mod;
  24.                     ans.v[i][j]%=mod;
  25.             }
  26.         return ans;
  27.     }
  28.     matrix(){
  29.         for(int i=0;i<2;i++)
  30.             for(int j=0;j<2;j++)
  31.                 v[i][j]=((i==j)?1:0);
  32.     }
  33. };
  34.  
  35. matrix power(matrix a,int p){
  36.     matrix e=a;
  37.     matrix ans;
  38.     do{
  39.         if(p&1) ans=ans*e;
  40.     }while(e=e*e,p>>=1);
  41.     return ans;
  42. }
  43.  
  44. signed main()
  45. {
  46.     int x1,x2,a,b,n;
  47.     cin >> x1 >> x2 >> a >> b >> n;
  48.  
  49.     matrix w;
  50.     w.v[0][0]=0;
  51.     w.v[1][0]=1;
  52.     w.v[0][1]=a;
  53.     w.v[1][1]=b;
  54.     matrix ans=power(w,n-1);
  55.     cout << ((ans.v[1][0]*x2)%mod + (ans.v[0][0]*x1)%mod)%mod << endl;
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement