题目描述
Description
在“1250 Fibonacci数列”中,我们求出了第n个Fibonacci数列的值。但是1250中,n<=109。现在,你的任务仍然是求出第n个Fibonacci数列的值,但是注意:n为整数,且1 <= n <= 100000000000000
输入描述
Input Description
输入有多组数据,每组数据占一行,为一个整数n(1 <= n <= 100000000000000)
输出描述
Output Description
输出若干行。每行输出第(对应的输入的)n个Fibonacci数(考虑到数会很大,mod 1000000007)
样例输入
Sample Input
3
4
5
样例输出
Sample Output
2
3
5
数据范围及提示
Data Size & Hint
1 <= n <= 100000000000000
分类标签 Tags 点此展开
AC代码:
#include<cstdio> #include<cstring> #define ll long long #ifdef unix #define LL "%lld" #else #define LL "%I64d" #endif using namespace std; const ll mod=1e9+7; struct node{ ll a[2][2]; }ans,ss; ll n; inline node mul(node &a,node &b){ node c; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ c.a[i][j]=0; for(int k=0;k<2;k++){ c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod; } } } return c; } void fpow(ll p){ for(;p;p>>=1,ss=mul(ss,ss)) if(p&1) ans=mul(ans,ss); } int main(){ while(scanf(LL,&n)==1){ if(n==1){puts("1");continue;} if(n==2){puts("1");continue;} if(n==3){puts("2");continue;} ans.a[0][0]=ans.a[1][0]=1;ans.a[0][1]=ans.a[1][1]=0; ss.a[0][0]=ss.a[1][0]=ss.a[0][1]=1;ss.a[1][1]=0; n--; fpow(n); printf(LL,ans.a[0][0]);putchar('\n'); } return 0; }