mr_dot_convict

matrix-lib-mr.convict

Jul 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. struct Mat
  9. {
  10.    int sz;
  11.    static const int mxn = 55, inf = (int)1e9;
  12.    int M[mxn][mxn];
  13.  
  14.    Mat(int n_, int val = 0)
  15.    {
  16.       sz = n_;
  17.       set(val);
  18.    }
  19.  
  20.    Mat(const Mat& other)
  21.    {
  22.       sz = other.sz;
  23.       for (int i = 0; i < sz; ++i)
  24.          for (int j = 0; j < sz; ++j)
  25.             M[i][j] = other.M[i][j];
  26.    }
  27.  
  28.    void set(int val)
  29.    {
  30.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  31.             M[i][j] = val;
  32.    }
  33.  
  34.    void make_I()
  35.    {
  36.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  37.          M[i][j] = (i == j ? 1 : 0);
  38.    }
  39.  
  40.    Mat operator+(const Mat &other)  // O(n^2)
  41.    {
  42.       Mat sum(sz);
  43.       for (int i = 0; i < sz; ++i)
  44.          for (int j = 0; j < sz; ++j)
  45.             sum.M[i][j] = add(M[i][j], other.M[i][j]);
  46.       return sum;
  47.    }
  48.  
  49.    Mat operator*(const Mat &other) // O(n^3)
  50.    {
  51.       Mat prod(sz);
  52.       for (int i = 0; i < sz; ++i)
  53.          for (int j = 0; j < sz; ++j)
  54.             for (int k = 0; k < sz; ++k)
  55.                prod.M[i][j] =
  56.                   add(prod.M[i][j], mul(M[i][k], other.M[k][j]));
  57.       return prod;
  58.    }
  59.  
  60.    Mat pow(ll k) // O(n^3*logk)
  61.    {
  62.       assert(k >= 0);
  63.       Mat M_cp(*this), res(sz);
  64.       res.make_I();
  65.  
  66.       while (k)
  67.       {
  68.          if (k & 1) res = res * M_cp;
  69.          M_cp = M_cp * M_cp;
  70.          k >>= 1;
  71.       }
  72.       return res;
  73.    }
  74.  
  75.    Mat mx_op (const Mat &other)
  76.    {
  77.       Mat res(sz, -inf);
  78.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  79.          for (int k = 0; k < sz; ++k)
  80.             res.M[i][j] = max(res.M[i][j], M[i][k] + other.M[k][j]);
  81.  
  82.       return res;
  83.    }
  84.  
  85.    Mat mn_op (const Mat &other)
  86.    {
  87.       Mat res(sz, inf);
  88.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  89.          for (int k = 0; k < sz; ++k)
  90.             res.M[i][j] = min(res.M[i][j], M[i][k] + other.M[k][j]);
  91.  
  92.       return res;
  93.    }
  94.  
  95.  
  96.    friend istream& operator >> (istream &is, Mat& o)
  97.    {
  98.       for (int i = 0; i < o.sz; ++i) for (int j = 0; j < o.sz; ++j)
  99.             is >> o.M[i][j];
  100.       return is;
  101.    }
  102.    friend ostream& operator << (ostream &os, const Mat& o)
  103.    {
  104.       for (int i = 0; i < o.sz; ++i) for (int j = 0; j < o.sz; ++j)
  105.          os << o.M[i][j] << (j == o.sz -1 ? '\n' : ' ');
  106.       return os;
  107.    }
  108.  
  109. };
Add Comment
Please, Sign In to add comment