forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
925c101
commit f4a1235
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
int add(int a, int b) | ||
{ | ||
int res = a + b; | ||
if(res >= MOD) | ||
return res - MOD; | ||
return res; | ||
} | ||
|
||
int mult(int a, int b) | ||
{ | ||
long long res = a; | ||
res *= b; | ||
if(res >= MOD) | ||
return res % MOD; | ||
return res; | ||
} | ||
|
||
struct matrix | ||
{ | ||
int arr[SZ][SZ]; | ||
|
||
void reset() | ||
{ | ||
memset(arr, 0, sizeof(arr)); | ||
} | ||
|
||
void makeiden() | ||
{ | ||
reset(); | ||
for(int i=0;i<SZ;i++) | ||
{ | ||
arr[i][i] = 1; | ||
} | ||
} | ||
|
||
matrix operator + (const matrix &o) const | ||
{ | ||
matrix res; | ||
for(int i=0;i<SZ;i++) | ||
{ | ||
for(int j=0;j<SZ;j++) | ||
{ | ||
res.arr[i][j] = add(arr[i][j], o.arr[i][j]); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
matrix operator * (const matrix &o) const | ||
{ | ||
matrix res; | ||
for(int i=0;i<SZ;i++) | ||
{ | ||
for(int j=0;j<SZ;j++) | ||
{ | ||
res.arr[i][j] = 0; | ||
for(int k=0;k<SZ;k++) | ||
{ | ||
res.arr[i][j] = add(res.arr[i][j] , mult(arr[i][k] , o.arr[k][j])); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
}; | ||
|
||
matrix power(matrix a, int b) | ||
{ | ||
matrix res; | ||
res.makeiden(); | ||
while(b) | ||
{ | ||
if(b & 1) | ||
{ | ||
res = res * a; | ||
} | ||
a = a * a; | ||
b >>= 1; | ||
} | ||
return res; | ||
} | ||
|
||
//Matrix Exponentiation: | ||
//Sample Problem 1: http://codeforces.com/contest/954/problem/F | ||
//Sample Solution 1: http://codeforces.com/contest/954/submission/39865763 | ||
|
||
//Sample Problem 2: http://codeforces.com/contest/821/problem/E | ||
//Sample Solution 2: http://codeforces.com/contest/821/submission/39865878 |