-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixexpo.cpp
More file actions
68 lines (58 loc) · 1.16 KB
/
matrixexpo.cpp
File metadata and controls
68 lines (58 loc) · 1.16 KB
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
65
66
67
68
#include<bits/stdc++.h>
#define mod 1000000007
#define maxl 2
#define ll long long
using namespace std;
struct matrix
{
ll a[maxl][maxl];
matrix()
{
memset(a,0,sizeof a);
}
void init()
{
a[0][0]=1, a[0][1]=1;
a[1][0]=1, a[1][1]=0;
}
matrix operator* ( matrix temp )
{
matrix res;
for(int i=0; i<maxl; i++)
{
for(int j=0; j<maxl; j++)
{
res.a[i][j]=0;
for(int k=0; k<maxl; k++)
{
res.a[i][j] =( res.a[i][j]+ (temp.a[i][k]*a[k][j]) )%mod;
}
}
}
return res;
}
};
matrix Power(matrix mat, ll n)
{
if(n == 1) return mat;
matrix res = Power(mat,n/2);
res = res*res;
if( (n%2) == 1 ) res= res*mat;
return res;
}
ll Matrix_Expo(matrix mat, ll n)
{
matrix res = Power(mat,n);
return res.a[0][0];
}
int main()
{
ll n;
struct matrix mat;
mat.init();
while( scanf("%lld",&n) != EOF )
{
printf("%lld\n",Power(mat,n-1));
}
return 0;
}