Skip to content

Commit 43b10de

Browse files
committed
matrix
1 parent 393156b commit 43b10de

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ AtCoder解説放送ライブラリ集
2222
|:--|:--|:--|
2323
|GCD/LCM|[gcd.cpp](gcd.cpp)|最大公約数と最小公倍数|
2424
|Combination|[comb.cpp](comb.cpp)|nCkをmod素数で求める|
25+
|Matrix|[mat.cpp](mat.cpp)|行列|
2526

2627
### グラフ
2728
|名前|コード|説明|

mat.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
template<typename T>
2+
struct Matrix {
3+
int h, w;
4+
vector<vector<T>> d;
5+
Matrix() {}
6+
Matrix(int h, int w, T val=0): h(h), w(w), d(h, vector<T>(w,val)) {}
7+
Matrix& unit() {
8+
assert(h == w);
9+
rep(i,h) d[i][i] = 1;
10+
return *this;
11+
}
12+
const vector<T>& operator[](int i) const { return d[i];}
13+
vector<T>& operator[](int i) { return d[i];}
14+
Matrix operator*(const Matrix& a) const {
15+
assert(w == a.h);
16+
Matrix r(h, a.w);
17+
rep(i,h)rep(k,w)rep(j,a.w) {
18+
r[i][j] += d[i][k]*a[k][j];
19+
}
20+
return r;
21+
}
22+
Matrix pow(int t) const {
23+
assert(h == w);
24+
if (!t) return Matrix(h,h).unit();
25+
if (t == 1) return *this;
26+
Matrix r = pow(t>>1);
27+
r = r*r;
28+
if (t&1) r = r*(*this);
29+
return r;
30+
}
31+
};

0 commit comments

Comments
 (0)