Saleh127

Light OJ 1065 / Mat Expo

Apr 12th, 2022
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. /***
  2.  created: 2022-04-12-21.13.43
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define ull unsigned long long
  9. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  10. #define get_lost_idiot return 0
  11. #define nl '\n'
  12. #define row vector<ll>
  13. #define matrix vector<vector<ll>>
  14.  
  15. ll mod;
  16.  
  17. matrix operator*(const matrix& a, const matrix& b)
  18. {
  19.     ll r,c,p;
  20.  
  21.     r=a.size();
  22.     p=a[0].size();
  23.     assert(p==b.size());
  24.  
  25.     /// first matrix must have the same number of columns
  26.     ///as the second matrix has rows
  27.  
  28.     c=b[0].size();
  29.  
  30.     matrix ans(r,row(c,0));
  31.  
  32.     ll i,j,k;
  33.  
  34.     for(i=0; i<r; i++)
  35.     {
  36.         for(j=0; j<c; j++)
  37.         {
  38.             for(k=0; k<p; k++)
  39.             {
  40.                 ans[i][j]=(ans[i][j]+a[i][k]*b[k][j])%mod;
  41.             }
  42.         }
  43.     }
  44.     return ans;
  45. }
  46.  
  47. matrix bigmod(matrix &a,ll p)
  48. {
  49.     if(p==1) return a;
  50.  
  51.     matrix x=bigmod(a,p/2);
  52.  
  53.     x=x*x;
  54.  
  55.     if(p&1) x=(x*a);
  56.  
  57.     return x;
  58. }
  59.  
  60.  
  61. int main()
  62. {
  63.     ios_base::sync_with_stdio(0);
  64.     cin.tie(0);
  65.     cout.tie(0);
  66.  
  67.     test
  68.     {
  69.         ll a,b,c,n,m,p,q;
  70.  
  71.         cin>>a>>b>>n>>mod;
  72.  
  73.         mod=pow(10,mod);
  74.  
  75.         matrix base = {{1,1},{1,0}};
  76.  
  77.         matrix statem = {{b},{a}};
  78.  
  79.         cout<<"Case "<<cs<<": ";
  80.  
  81.         if(n==0) cout<<a<<nl;
  82.         else if(n==1) cout<<b<<nl;
  83.         else
  84.         {
  85.             matrix ans=bigmod(base,n-1);
  86.  
  87.             ans=ans*statem;
  88.  
  89.             cout<<ans[0][0]%mod<<nl;
  90.         }
  91.     }
  92.  
  93.     get_lost_idiot;
  94. }
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment