-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmatrixexponentiation.sublime-snippet
executable file
·64 lines (61 loc) · 1.49 KB
/
matrixexponentiation.sublime-snippet
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
<snippet>
<content><![CDATA[
const int MOD = 1000000007;
template<typename T>
class Matrix{
int N , M;
vector < vector < T > > ar;
public:
Matrix(int N_) : N(N_) , M(N_){
ar.resize(N , vector <T>(N));
}
Matrix(int N_ , int M_) : N(N_) , M(M_){
ar.resize(N , vector <T>(M));
}
void input(){
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j < M ; j++){
cin >> ar[i][j];
}
}
}
void multiply(T a[2][2], T b[2][2]){
T ans[2][2];
for(int i = 0 ; i < 2 ; i++){
for(int j = 0 ; j < 2 ; j++){
ans[i][j]=0;
for(int k = 0 ; k < 2 ; k++){
ans[i][j] = (ans[i][j]+(a[i][k]*b[k][j])%MOD)%MOD;
}
}
for(int i = 0 ; i < 2 ; i++){
for(int j = 0 ; j < 2 ; j++){
a[i][j] = ans[i][j];
}
}
}
T matrixpower(ll b){
T a[2][2];
a[0][0] = ;
a[0][1] = ;
a[1][0] = ;
a[1][1] = ;
ll c[][2] = {{,},{,}};
while(b){
if(b&1) multiply(c,a);
multiply(a,a);
b >>= 1;
}
ll answer = ;
answer %= MOD;
return answer;
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>matrixexpo</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.cpp , source.c++ , source.c</scope>
<!-- Optional: Description to show in the menu -->
<description>Matrix Exponentiation</description>
</snippet>