Advertisement
Saleh127

Light OJ 1131 / Mat Expo

Feb 20th, 2022
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. /***
  2.  created: 2022-02-19-22.11.08
  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. ll mod;
  14.  
  15. matrix operator*(const matrix& a, const matrix& b)
  16. {
  17.     ll r,c,p;
  18.  
  19.     r=a.size();
  20.     p=a[0].size();
  21.     assert(p==b.size());
  22.  
  23.     /// first matrix must have the same number of columns
  24.     ///as the second matrix has rows
  25.  
  26.     c=b[0].size();
  27.  
  28.     matrix ans(r,row(c,0));
  29.  
  30.     ll i,j,k;
  31.  
  32.     for(i=0; i<r; i++)
  33.     {
  34.         for(j=0; j<c; j++)
  35.         {
  36.             for(k=0; k<p; k++)
  37.             {
  38.                 ans[i][j]=(ans[i][j]+a[i][k]*b[k][j])%mod;
  39.             }
  40.         }
  41.     }
  42.     return ans;
  43. }
  44.  
  45. matrix bigmod(matrix &a,ll p)
  46. {
  47.     if(p==1) return a;
  48.  
  49.     matrix x=bigmod(a,p/2);
  50.  
  51.     x=x*x;
  52.  
  53.     if(p&1) x=(x*a);
  54.  
  55.     return x;
  56. }
  57.  
  58. int main()
  59. {
  60.     ios_base::sync_with_stdio(0);
  61.     cin.tie(0);
  62.     cout.tie(0);
  63.  
  64.     test
  65.     {
  66.  
  67.         ll n,q,a1,a2,b1,b2,c1,c2,f0,f1,f2,g0,g1,g2;
  68.  
  69.         cin>>a1>>b1>>c1;
  70.         cin>>a2>>b2>>c2;
  71.         cin>>f0>>f1>>f2;
  72.         cin>>g0>>g1>>g2;
  73.  
  74.         matrix base = {{a1,b1,0,0,0,c1},
  75.             {1,0,0,0,0,0},
  76.             {0,1,0,0,0,0},
  77.             {0,0,c2,a2,b2,0},
  78.             {0,0,0,1,0,0},
  79.             {0,0,0,0,1,0}
  80.         };
  81.  
  82.         matrix statem = {{f2},{f1},{f0},{g2},{g1},{g0}};
  83.  
  84.         cin>>mod;
  85.  
  86.         cin>>q;
  87.  
  88.         cout<<"Case "<<cs<<":"<<nl;
  89.  
  90.         while(q--)
  91.         {
  92.             cin>>n;
  93.  
  94.             if(n==0)
  95.             {
  96.                 cout<<f0%mod<<" "<<g0%mod<<nl;
  97.             }
  98.             else if(n==1)
  99.             {
  100.                 cout<<f1%mod<<" "<<g1%mod<<nl;
  101.             }
  102.             else if(n==2)
  103.             {
  104.                 cout<<f2%mod<<" "<<g2%mod<<nl;
  105.             }
  106.             else
  107.             {
  108.                 matrix ans=bigmod(base,n-2);
  109.  
  110.                 ans=ans*statem;
  111.  
  112.                 cout<<ans[0][0]%mod<<" "<<ans[3][0]%mod<<nl;
  113.  
  114.             }
  115.  
  116.         }
  117.  
  118.     }
  119.  
  120.     get_lost_idiot;
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement