mr_dot_convict

R-Walk-Atcoder-dpcontest-mr.convict

Jul 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 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. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #ifndef CONVICTION
  11. #pragma GCC      optimize ("Ofast")
  12. #pragma GCC      optimize ("unroll-loops")
  13. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  14. #endif
  15.  
  16. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  17. #define PREC     cout.precision (10); cout << fixed
  18. #define bg(x)    " [ " << #x << " : " << (x) << " ] "
  19. #define x        first
  20. #define y        second
  21. using ll = long long;
  22. using ff = long double;
  23. using pii = pair<int,int>;
  24.  
  25. #define debug(args...) { \
  26. /* WARNING : do NOT compile this debug func calls with following flags: // \
  27.  * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  28.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  29.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  30. }
  31. void err(istream_iterator<string> it) { it->empty();
  32.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  33. }
  34. template<typename T, typename... Args>
  35. void err(istream_iterator<string> it, T a, Args... args) {
  36.     cerr << fixed << setprecision(15) << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  37.     err(++it, args...);
  38. }
  39.  
  40. const int N = 55, MOD = (int)1e9 + 7;
  41. inline int add (int a, int b)
  42. {
  43.    return (a + b) % MOD;
  44. }
  45. inline int mul (int a, int b) {
  46.    return int(1ll*a*b % MOD);
  47. }
  48.  
  49. struct Mat
  50. {
  51.    int sz;
  52.    static const int mxn = 55, inf = (int)1e9;
  53.    int M[mxn][mxn];
  54.  
  55.    Mat(int n_, int val = 0)
  56.    {
  57.       sz = n_;
  58.       set(val);
  59.    }
  60.  
  61.    Mat(const Mat& other)
  62.    {
  63.       sz = other.sz;
  64.       for (int i = 0; i < sz; ++i)
  65.          for (int j = 0; j < sz; ++j)
  66.             M[i][j] = other.M[i][j];
  67.    }
  68.  
  69.    void set(int val)
  70.    {
  71.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  72.             M[i][j] = val;
  73.    }
  74.  
  75.    void make_I()
  76.    {
  77.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  78.          M[i][j] = (i == j ? 1 : 0);
  79.    }
  80.  
  81.    Mat operator+(const Mat &other)  // O(n^2)
  82.    {
  83.       Mat sum(sz);
  84.       for (int i = 0; i < sz; ++i)
  85.          for (int j = 0; j < sz; ++j)
  86.             sum.M[i][j] = add(M[i][j], other.M[i][j]);
  87.       return sum;
  88.    }
  89.  
  90.    Mat operator*(const Mat &other) // O(n^3)
  91.    {
  92.       Mat prod(sz);
  93.       for (int i = 0; i < sz; ++i)
  94.          for (int j = 0; j < sz; ++j)
  95.             for (int k = 0; k < sz; ++k)
  96.                prod.M[i][j] =
  97.                   add(prod.M[i][j], mul(M[i][k], other.M[k][j]));
  98.       return prod;
  99.    }
  100.  
  101.    Mat pow(ll k) // O(n^3*logk)
  102.    {
  103.       assert(k >= 0);
  104.       Mat M_cp(*this), res(sz);
  105.       res.make_I();
  106.  
  107.       while (k)
  108.       {
  109.          if (k & 1) res = res * M_cp;
  110.          M_cp = M_cp * M_cp;
  111.          k >>= 1;
  112.       }
  113.       return res;
  114.    }
  115.  
  116.    Mat mx_op (const Mat &other)
  117.    {
  118.       Mat res(sz, -inf);
  119.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  120.          for (int k = 0; k < sz; ++k)
  121.             res.M[i][j] = max(res.M[i][j], M[i][k] + other.M[k][j]);
  122.  
  123.       return res;
  124.    }
  125.  
  126.    Mat mn_op (const Mat &other)
  127.    {
  128.       Mat res(sz, inf);
  129.       for (int i = 0; i < sz; ++i) for (int j = 0; j < sz; ++j)
  130.          for (int k = 0; k < sz; ++k)
  131.             res.M[i][j] = min(res.M[i][j], M[i][k] + other.M[k][j]);
  132.  
  133.       return res;
  134.    }
  135.  
  136.  
  137.    friend istream& operator >> (istream &is, Mat& o)
  138.    {
  139.       for (int i = 0; i < o.sz; ++i) for (int j = 0; j < o.sz; ++j)
  140.             is >> o.M[i][j];
  141.       return is;
  142.    }
  143.  
  144.    friend ostream& operator << (ostream &os, const Mat& o)
  145.    {
  146.       for (int i = 0; i < o.sz; ++i) for (int j = 0; j < o.sz; ++j)
  147.          os << o.M[i][j] << (j == o.sz -1 ? '\n' : ' ');
  148.       return os;
  149.    }
  150.  
  151. };
  152.  
  153. int n;
  154. ll k;
  155. int edge[N][N];
  156.  
  157. void solve()
  158. {
  159.    Mat G(n);
  160.    cin >> G;
  161.    G = G.pow(k);
  162.  
  163.    int total = 0;
  164.    for (int i = 0; i < n; ++i)
  165.       for (int j = 0; j < n; ++j)
  166.          total = add(total, G.M[i][j]);
  167.    cout << total << '\n';
  168. }
  169.  
  170. signed main()
  171. {
  172.    IOS; PREC;
  173.    cin >> n >> k;
  174.    solve();
  175.  
  176.    return EXIT_SUCCESS;
  177. }
Add Comment
Please, Sign In to add comment