Repository navigation
Expand file tree
/
Copy pathmatrix.cpp
More file actions
111 lines (108 loc) · 4.66 KB
/
Copy pathmatrix.cpp
File metadata and controls
111 lines (108 loc) · 4.66 KB
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
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using mint = modint1000000007;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)
#define all(a) (a).begin(),(a).end()
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};
template<typename T>
struct mat {
// 行列m
vector<vector<T>> m;
// コンストラクタ:第1引数⇒行数、第2引数⇒列数、第3引数⇒初期値
mat():m(vector<vector<T>>()){}
mat(int h,int w):m(vector<vector<T>>(h,vector<T>(w))){}
mat(int h,int w, T d):m(vector<vector<T>>(h,vector<T>(w,d))){}
// 添字演算子
vector<T> operator[](const int i) const {return m[i];} //読み取り
vector<T>& operator[](const int i){return m[i];} //書き込み
// 行数・列数
int nrow = sz(m);
int ncol = sz(m[0]);
// 行列同士の演算
mat& operator=(const mat& a){return *a;}
mat& operator+=(const mat& a){assert(ncol == a.ncol && nrow == a.nrow);rep(i,nrow)rep(j,ncol)m[i][j] += a[i][j]; return *this;}
mat& operator-=(const mat& a){assert(ncol == a.ncol && nrow == a.nrow);rep(i,nrow)rep(j,ncol)m[i][j] -= a[i][j]; return *this;}
mat& operator*=(const mat& a){assert(ncol == a.nrow);mat<T> m2(nrow, a.ncol, 0);rep(i,nrow)rep(j,a.ncol)rep(k,ncol)m2[i][j] += m[i][k]*a[k][j];ncol = a.ncol;rep(i,nrow)m[i].resize(ncol);rep(i,nrow)rep(j,ncol)m[i][j] = m2[i][j]; return *this;}
mat operator+(const mat& a) const { return mat(*this) += a;}
mat operator-(const mat& a) const { return mat(*this) -= a;}
mat operator*(const mat& a) const { return mat(*this) *= a;}
bool operator==(const mat& a){assert(ncol == a.ncol && nrow == a.nrow);bool flg = true;rep(i,nrow)rep(j,ncol)if(m[i][j] != a[i][j])flg = false; return flg;}
// 行列とスカラの演算
mat& operator+=(const T& a){rep(i,nrow)rep(j,ncol)m[i][j] += a;return *this;}
mat& operator-=(const T& a){rep(i,nrow)rep(j,ncol)m[i][j] -= a;return *this;}
mat& operator*=(const T& a){rep(i,nrow)rep(j,ncol)m[i][j] *= a;return *this;}
mat& operator/=(const T& a){rep(i,nrow)rep(j,ncol)m[i][j] /= a;return *this;}
mat operator+(const T& a) const { return mat(*this) += a;}
mat operator-(const T& a) const { return mat(*this) -= a;}
mat operator*(const T& a) const { return mat(*this) *= a;}
mat operator/(const T& a) const { return mat(*this) /= a;}
// 回転(degの数だけ時計回りに90度回転)
mat& rotate(int deg){
mat<T> m2(ncol, nrow);
if(deg == 1 || deg == 3){
if(deg == 1)rep(i,nrow)rep(j,ncol)m2[j][nrow -i -1] = m[i][j];
if(deg == 3)rep(i,nrow)rep(j,ncol)m2[ncol -j -1][i] = m[i][j];
swap(ncol,nrow); // 列数と行数を入れ替える
m.resize(nrow);rep(i,nrow)m[i].resize(ncol); //リサイズ
}
if(deg == 2)rep(i,nrow)rep(j,ncol)m2[nrow -i -1][ncol -j -1] = m[i][j];
rep(i,nrow)rep(j,ncol)m[i][j] = m2[i][j];
return *this;
}
// 標準出力
void show(){
rep(i,nrow)rep(j,ncol){
if(j != 0)cout << " ";
cout << m[i][j];
if(j==ncol-1)cout << endl;
}
return ;
}
};
template<typename T>
struct Matrix{
int nrow, ncol;
T id;
vector<vector<T>> mat;
Matrix(int nrow, int ncol, T id=1) : nrow(nrow), ncol(ncol), mat(vector<vector<T>>(nrow, vector<T>(ncol, T()))){}
vector<T> &operator[](const int i){ return mat[i]; }
Matrix &operator+=(const Matrix &m){ for(int i=0; i<nrow; i++)for(int j=0; j<ncol; j++)mat[i][j]+=m[i][j]; return *this; }
Matrix &operator*=(const Matrix &m){ Matrix ret(nrow, m.ncol); for(int i=0; i<nrow; i++)for(int j=0; j<m.ncol; j++)for(int k=0; k<ncol; k++)ret[i][j]+=mat[i][k]*mat[k][j]; *this=ret; return *this; }
Matrix operator+(const Matrix &m){ return Matrix(*this)+=m; }
Matrix operator*(const Matrix &m){ return Matrix(*this)*=m; }
Matrix pow(long long k){
Matrix<T> res(nrow, ncol), c=*this;
for(int i=0; i<nrow; i++) res[i][i] = id;
while(k){
if(k&1) res *= c;
c*=c;
c >>= 1;
}
return res;
}
// 反時計回り
Matrix &rotate(){
Matrix ret(ncol, nrow);
for(int i=0; i<nrow; i++){
for(int j=0; j<ncol; j++){
ret[j][i] = mat[i][ncol-j-1];
}
}
*this = ret;
return *this;
}
};
int main(){
}