Advertisement
RaFiN_

matrix multiplication

Oct 13th, 2020
2,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1.  
  2. //matrix multiplication
  3. struct matrix {
  4.     ll mat[6][6];
  5.     matrix(){
  6.         for(int i = 0;i<6;i++){
  7.             for(int j = 0;j<6;j++)
  8.                 mat[i][j] = 0;
  9.         }
  10.     }
  11. };
  12.  
  13. matrix mul(matrix &a,matrix &b){
  14.     matrix res;
  15.     for(int i = 0;i<6;i++){
  16.         for(int j = 0;j<6;j++){
  17.             for(int k = 0;k<6;k++){
  18.                 res.mat[i][j] += (a.mat[i][k] * b.mat[k][j])%M;
  19.                 res.mat[i][j] %= M;
  20.             }
  21.         }
  22.     }
  23.     return res;
  24. }
  25.  
  26. matrix expo(matrix &base,ll power){
  27.     if(power==1){
  28.         return base;
  29.     }
  30.     if(power&1ll){
  31.         matrix temp = expo(base,power-1);
  32.         return mul(base,temp);
  33.     }
  34.     matrix temp = expo(base,power/2);
  35.     return mul(temp,temp);
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement