Advertisement
fooker

matrix exponentiation

May 25th, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. const ll nmax=1e9+7;
  5.  
  6. vector<vector<ll>> mat_mul(vector<vector<ll>> a, vector<vector<ll>> b, ll mod){
  7.     ll n=a.size();
  8.     vector<vector<ll>> res(n,vector<ll>(n));
  9.     for (ll i=0; i<n; i++){
  10.         for (ll j=0; j<n; j++){
  11.             for (ll k=0; k<n; k++){
  12.                 res[i][j]+=a[i][k]*b[k][j];
  13.                 res[i][j]%=mod;
  14.             }
  15.         }
  16.     }
  17.     return res;
  18. }
  19. vector<vector<ll>> mat_pow(vector<vector<ll>> a, ll x, ll mod){
  20.     ll n=a.size();
  21.     vector<vector<ll>> res(n,vector<ll>(n));
  22.     for (ll i=0; i<n; i++) res[i][i]=1;
  23.     while(x>0){
  24.         if (x&1) res=mat_mul(res,a,mod);
  25.         a = mat_mul(a,a,mod);
  26.         x>>=1;
  27.     }
  28.     return res;
  29. }
  30. int main()
  31. {
  32.     vector<vector<ll>> ans(2,vector<ll>(2));
  33.     ans[0][0]=19;
  34.     ans[0][1]=6;
  35.     ans[1][0]=7;
  36.     ans[1][1]=20;
  37.     ll n;
  38.     cin>>n;
  39.     ans=mat_pow(ans,n,nmax);
  40.     cout<<ans[0][0];
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement