Fibnacci

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
#define debug(x) cout<<#x<<"="<<x<<endl
#define mod 1000000007
ll n;
struct node
{
ll a[2][2];
node(){memset(a,0,sizeof(a));}
}a,ans;
node mul(node x,node y)
{
node ret;
fo(i,0,1)
fo(j,0,1)
fo(k,0,1)
ret.a[i][j]+=x.a[i][k]*y.a[k][j],ret.a[i][j]%=mod;
return ret;
}
node qpow(node base,ll v)
{
node ret=a;v--;
while(v)
{
if(v&1)ret=mul(ret,base);
v>>=1;
base=mul(base,base);
}
return ret;
}
int ha[4]={0,1,1,2};
int main()
{
a.a[0][0]=1;a.a[0][1]=1;a.a[1][0]=1;a.a[1][1]=0;
ans=a;
scanf("%lld",&n);
if(n<=3){cout<<ha[n];return 0;}
n-=2;
ans=qpow(ans,n);
printf("%lld\n",(ans.a[0][0]+ans.a[1][0])%mod);
return 0;
}
显示 Gitment 评论