-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmatrix_exponent.cpp
46 lines (39 loc) · 1.01 KB
/
matrix_exponent.cpp
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
/// *** Matrix Exponentiation [2^3*log(n)]
/// by calling exponent this will calculate (ara^p)%md;
/// here n is the dimention of the matrix ara
#define ll long long
#define DIM 5
ll tmp[DIM][DIM],result[DIM][DIM];
void cpy(ll ar1[][DIM],ll ar2[][DIM],ll n){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
ar1[i][j] = ar2[i][j];
}
void mult(ll ar1[][DIM], ll ar2[][DIM], ll n, ll mod){
int i,j,k;
memset(tmp,0,sizeof tmp);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
for(k=1;k<=n;k++){
tmp[i][j] = (tmp[i][j] + ar1[i][k]*ar2[k][j])%mod;
}
}
}
cpy(ar1,tmp,n);
}
void exponent(ll ara[][DIM], ll p,ll n, ll mod){
int i,j;
//initializing identity
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j) result[i][j] = 1;
else result[i][j] = 0;
while(p){
if(p&1){
mult(result,ara,n,mod);
}
mult(ara,ara,n,mod);
p >>= 1;
}
cpy(ara,result,n);
}