Skip to content

Commit

Permalink
Create Matrix Struct.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashishgup1 authored Jul 2, 2018
1 parent 925c101 commit f4a1235
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions Matrix Struct.cpp
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

0 comments on commit f4a1235

Please sign in to comment.