Advertisement
Saleh127

Light OJ 1096 / Mat Expo

Mar 17th, 2022
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. /***
  2.  created: 2022-03-17-12.47.44
  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=10007;
  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.  
  59. int main()
  60. {
  61.     ios_base::sync_with_stdio(0);
  62.     cin.tie(0);
  63.     cout.tie(0);
  64.  
  65.     test
  66.     {
  67.  
  68.         ll a,b,c,n,m;
  69.  
  70.         cin>>n>>a>>b>>c;
  71.  
  72.         matrix base = {{a,0,b,1},
  73.             {1,0,0,0},
  74.             {0,1,0,0},
  75.             {0,0,0,1},
  76.         };
  77.  
  78.  
  79.         matrix statem = {{c},{0},{0},{c}};
  80.  
  81.         cout<<"Case "<<cs<<": ";
  82.  
  83.        
  84.         if(n<=3)
  85.         {
  86.             if(n<=2) cout<<0<<nl;
  87.             else cout<<c<<nl;
  88.            
  89.             continue;
  90.         }  
  91.        
  92.         matrix ans=bigmod(base,n-3);
  93.  
  94.         ans=ans*statem;
  95.  
  96.  
  97.         cout<<ans[0][0]%mod<<nl;
  98.  
  99.     }
  100.  
  101.     get_lost_idiot;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement