1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
#include <bits/stdc++.h> #define all(vec) vec.begin(),vec.end() #define PB push_back #define PF push_front #define EB emplace_back #define EF emplace_front #define EM emplace #define FI first #define SE second #define pct __builtin_popcountll #define ctz __builtin_ctzll #define SZ(a) ((long long) a.size()) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define ROF(i, a, b) for (int i = (a); i >= (b); --i) #define debug(var) cerr << #var <<":"<<var<<"\n"; #define cend cerr<<"\n-----------\n" #define fsp(x) fixed<<setprecision(x) using namespace std; using ll = long long;using ull = unsigned long long; using DB=double;using LD=long double;
using CD=complex<double>; static constexpr ll MAXN=(ll)1e6+10,INF=(1ll<<61)-1; static constexpr ll mod=(ll)1e9+7; static constexpr double eps=1e-8;const double pi=acos(-1.0); ll lT;
template <class T, T MOD, class Wide = ull> struct modint { T v; modint(ll x = 0) { ll t = x % (ll)MOD; if (t < 0) t += MOD; v = (T)t; } T val() const { return v; } modint& operator+=(const modint &o) { T x = v + o.v; if (x >= MOD) x -= MOD; v = x; return *this; } modint& operator-=(const modint &o) { T x = v >= o.v ? v - o.v : (T)(v + MOD - o.v); v = x; return *this; } modint& operator*=(const modint &o) { Wide t = (Wide)v * (Wide)o.v; v = (T)(t % MOD); return *this; } static modint powmod(modint a, ll e) { modint r = 1; while (e) { if (e & 1) r *= a; a *= a; e >>= 1; } return r; } static modint inv(modint a) { return powmod(a, (ll)MOD - 2); } modint& operator/=(const modint &o) { return *this *= inv(o); } modint operator+() const { return *this; } modint operator-() const { return modint(0) - *this; } friend modint operator+(modint a, const modint &b) { return a += b; } friend modint operator-(modint a, const modint &b) { return a -= b; } friend modint operator*(modint a, const modint &b) { return a *= b; } friend modint operator/(modint a, const modint &b) { return a /= b; } friend bool operator==(const modint &a, const modint &b) { return a.v == b.v; } friend bool operator!=(const modint &a, const modint &b) { return a.v != b.v; } friend ostream& operator<<(ostream &os, const modint &x) { return os << x.v; } friend istream& operator>>(istream &is, modint &x) { ll t; if (!(is >> t)) return is; x = modint(t); return is; } }; using modi=modint<uint32_t,mod>;
template <class T> struct Matrix { int n, m; vector<vector<T>> a; Matrix(): n(0), m(0) {} Matrix(int n_, int m_, const T &val = T()) { assign(n_, m_, val); } Matrix(const vector<vector<T>> &vv) { load(vv); } void load(const vector<vector<T>> &vv) { n = (int)vv.size() - 1; m = n ? (int)vv[1].size() - 1 : 0; a = vv; } void assign(int n_, int m_, const T &val = T()) { n = n_; m = m_; a.assign(n + 1, vector<T>(m + 1, val)); } vector<T>& operator[](int i) { return a[i]; } const vector<T>& operator[](int i) const { return a[i]; } Matrix operator+(const Matrix &rhs) const { assert(n == rhs.n && m == rhs.m); Matrix res(n, m); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) res.a[i][j] = a[i][j] + rhs.a[i][j]; return res; } Matrix& operator+=(const Matrix &rhs) { return *this = *this + rhs; } Matrix operator-(const Matrix &rhs) const { assert(n == rhs.n && m == rhs.m); Matrix res(n, m); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) res.a[i][j] = a[i][j] - rhs.a[i][j]; return res; } Matrix& operator-=(const Matrix &rhs) { return *this = *this - rhs; } Matrix operator*(const Matrix &rhs) const { assert(m == rhs.n); Matrix res(n, rhs.m, T()); for (int i = 1; i <= n; ++i) { for (int k = 1; k <= m; ++k) { if (a[i][k] == T()) continue; for (int j = 1; j <= rhs.m; ++j) { res.a[i][j] = res.a[i][j] + a[i][k] * rhs.a[k][j]; } } } return res; } Matrix& operator*=(const Matrix &rhs) { return *this = *this * rhs; } static Matrix identity(int n) { Matrix I(n, n, T()); FOR(i,1,n) I.a[i][i] = T(1); return I; } friend ostream& operator<<(ostream &os,const Matrix& ma) { FOR(i,1,ma.n) { FOR(j,1,ma.m) { os<<ma[i][j]<<" "; } os<<"\n"; } return os; } };
template <class T> Matrix<T> mat_pow(Matrix<T> base, long long exp) { assert(base.n == base.m); Matrix<T> res = Matrix<T>::identity(base.n); while (exp > 0) { if (exp & 1) res = res * base; base = base * base; exp >>= 1; } return res; } void Solve(){ ll n,k; cin>>n>>k; vector<vector<modi> > A(n+1,vector<modi>(n+1)); FOR(i,1,n) { FOR(j,1,n) { cin>>A[i][j]; } } Matrix mat(A); mat=mat_pow(mat,k); cout<<mat; } signed main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); return 0; }
|