Saleh127

Light OJ 1142 / Mat Expo

Mar 3rd, 2022 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. /***
  2.  created: 2022-03-03-21.32.02
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11. #define row vector<ll>
  12. #define matrix vector<vector<ll>>
  13.  
  14. matrix operator*(const matrix& a, const matrix& b)
  15. {
  16.     ll r,c,p;
  17.  
  18.     r=a.size();
  19.     p=a[0].size();
  20.     assert(p==b.size()); ///checking row==col
  21.  
  22.     /// first matrix must have the same number of columns
  23.     ///as the second matrix has rows
  24.  
  25.     c=b[0].size();
  26.  
  27.     matrix ans(r,row(c,0));
  28.  
  29.     ll i,j,k;
  30.  
  31.     for(i=0; i<r; i++)
  32.     {
  33.         for(j=0; j<c; j++)
  34.         {
  35.             for(k=0; k<p; k++)
  36.             {
  37.                 ans[i][j]=(ans[i][j]+a[i][k]*b[k][j])%10;
  38.             }
  39.         }
  40.     }
  41.     return ans;
  42. }
  43.  
  44. matrix addition(const matrix& a, const matrix& b)
  45. {
  46.     ll r,c,p,i,j,k;
  47.  
  48.     r=a.size();
  49.     c=b[0].size();
  50.  
  51.     matrix ans(r,row(c,0));
  52.  
  53.     for(i=0; i<r; i++)
  54.     {
  55.         for(j=0; j<c; j++)
  56.         {
  57.             ans[i][j]=(a[i][j]+b[i][j])%10;
  58.         }
  59.     }
  60.  
  61.     return ans;
  62. }
  63.  
  64. matrix bigmod(matrix &a,ll p)
  65. {
  66.     if(p==1) return a;
  67.  
  68.     matrix x=bigmod(a,p/2);
  69.  
  70.     x=x*x;
  71.  
  72.     if(p&1) x=(x*a);
  73.  
  74.     return x;
  75. }
  76.  
  77. matrix solve(matrix a,ll k)
  78. {
  79.     if(k==1) return a;
  80.  
  81.     if(k&1)
  82.     {
  83.         return addition(solve(a,k-1),bigmod(a,k));
  84.     }
  85.  
  86.     matrix xx=solve(a,k/2);
  87.  
  88.     return addition(xx,(xx*bigmod(a,k/2)));
  89. }
  90.  
  91.  
  92. int main()
  93. {
  94.     ios_base::sync_with_stdio(0);
  95.     cin.tie(0);
  96.     cout.tie(0);
  97.  
  98.  
  99.     /*
  100.    
  101.     For Even K
  102.  
  103.     f(k=2x) = [A + A^2 + A^3 + ... + A^x ] + [A^(x+1) + A^(x+2) + .... + A^(x+x)]
  104.     = [A + A^2 + A^3 + ... + A^x ] + [A^x(A + A^2 + A^3 + ... + A^x)]
  105.     = [A + A^2 + A^3 + ... + A^x ] * (A^x + 1)
  106.     = f(k/2) * A^(k/2) +  f(k/2) ;
  107.  
  108.     For Odd K
  109.  
  110.     f(K)= f(k-1)+A^k
  111.    
  112.     */
  113.  
  114.     test
  115.     {
  116.         ll n,m,i,j,k,l;
  117.  
  118.         cin>>n>>k;
  119.  
  120.         matrix base(n+2,row(n+2,0));
  121.  
  122.         for(i=0; i<n; i++)
  123.         {
  124.             for(j=0; j<n; j++)
  125.             {
  126.                 cin>>base[i][j];
  127.             }
  128.         }
  129.  
  130.         matrix ans=solve(base,k);
  131.  
  132.         cout<<"Case "<<cs<<":"<<nl;
  133.  
  134.         for(i=0; i<n; i++)
  135.         {
  136.             for(j=0; j<n; j++)
  137.             {
  138.                 cout<<ans[i][j];
  139.             }
  140.             cout<<nl;
  141.         }
  142.  
  143.     }
  144.  
  145.     get_lost_idiot;
  146. }
  147.  
Add Comment
Please, Sign In to add comment