forked from manujgrover71/competitive_programming_codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatExpo
More file actions
32 lines (32 loc) · 711 Bytes
/
MatExpo
File metadata and controls
32 lines (32 loc) · 711 Bytes
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
void multiply(int a[3][3], int b[3][3])
{
int mul[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mul[i][j] = 0;
for (int k = 0; k < 3; k++)
mul[i][j] += a[i][k]*b[k][j];
}
}
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
a[i][j] = mul[i][j];
}
int power(int F[3][3], int n)
{
int M[3][3] = {{1,1,1}, {1,0,0}, {0,1,0}};
if (n==1)
return F[0][0] + F[0][1];
power(F, n/2);
multiply(F, F);
if (n%2 != 0)
multiply(F, M);
return F[0][0] + F[0][1] ;
}
int findNthTerm(int n)
{
int F[3][3] = {{1,1,1}, {1,0,0}, {0,1,0}} ;
return power(F, n-2);
}