zoukankan      html  css  js  c++  java
  • 1732 Fibonacci数列 2

    1732 Fibonacci数列 2

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 钻石 Diamond
     
     
     
    题目描述 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 点此展开 

     
    2017-03-24
    #include<cstdio>
    #include<cstring>
    #define m(s) memset(s,0,sizeof s)
    using namespace std;
    typedef long long ll;
    const ll mod=1e9+7;ll n;
    struct matrix{ll s[2][2];}A,F;
    matrix operator *(const matrix &a,const matrix &b){
        matrix c;
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                c.s[i][j]=0;
                for(int k=0;k<2;k++){
                    c.s[i][j]+=a.s[i][k]*b.s[k][j];
                    c.s[i][j]%=mod;
                }
            }
        }
        return c;
    }
    //Fi=A^i*F0;
    matrix fpow(matrix a,ll p){
        matrix ans;
        for(int i=0;i<2;i++)for(int j=0;j<2;j++) ans.s[i][j]=(i==j);
        for(;p;p>>=1,a=a*a) if(p&1) ans=ans*a;
        return ans;
    }
    int main(){
        while(scanf("%lld",&n)==1){
            F.s[0][0]=1;F.s[0][1]=F.s[1][0]=F.s[1][1]=0; 
            A.s[0][0]=A.s[0][1]=A.s[1][0]=1;A.s[1][1]=0;
            if(n>1) F=fpow(A,n-1)*F;
            printf("%lld
    ",F.s[0][0]);
        }
        return 0;
    }

    2016-09-17

    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('
    ');
        }
        return 0;
    }
  • 相关阅读:
    【转】 Linux Core Dump 介绍
    【转】 设定linux 系统可用资源
    Python for 循环 失效
    transition 平移属性实现 横向整屏 滚动
    vue 插槽的使用
    vue pc商城仿网易严选商品的分类效果
    干货-vue 中使用 rxjs 进行非父子组件中传值
    vue 2.0 脚手架项目中使用 cross-env 分环境打包
    什么是闭包,有哪些优缺点呢?
    滚动视差
  • 原文地址:https://www.cnblogs.com/shenben/p/5879177.html
Copyright © 2011-2022 走看看