zoukankan      html  css  js  c++  java
  • 洛谷—— P1962 斐波那契数列

    https://www.luogu.org/problem/show?pid=1962

    题目背景

    大家都知道,斐波那契数列是满足如下性质的一个数列:

    • f(1) = 1

    • f(2) = 1

    • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数)

    题目描述

    请你求出 f(n) mod 1000000007 的值。

    输入输出格式

    输入格式:

    ·第 1 行:一个整数 n

    输出格式:

    第 1 行: f(n) mod 1000000007 的值

    输入输出样例

    输入样例#1:
    5
    输出样例#1:
    5
    输入样例#2:
    10
    输出样例#2:
    55

    说明

    对于 60% 的数据: n ≤ 92

    对于 100% 的数据: n在long long(INT64)范围内。

    矩阵乘法优化。、

     1 #include <cstdio>
     2 
     3 #define LL long long
     4 const LL mod(1000000007);
     5 
     6 inline void read(LL &x)
     7 {
     8     x=0; register char ch=getchar();
     9     for(; ch>'9'||ch<'0'; ) ch=getchar();
    10     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
    11 }
    12 
    13 LL n,m;
    14 struct Matrix_fb {
    15     LL e[2][2];
    16     void init_base()
    17     {
    18         e[0][0]=1;
    19         e[0][1]=1;
    20         e[1][0]=1;
    21         e[1][1]=0;
    22     }
    23     void init_ans()
    24     {
    25         e[0][0]=e[0][1]=1;
    26     }
    27     Matrix_fb operator * (Matrix_fb x) const
    28     {
    29         Matrix_fb tmp;
    30         for(int i=0; i<2; ++i)
    31             for(int j=0; j<2; ++j)
    32             {
    33                 tmp.e[i][j]=0;
    34                 for(int k=0; k<2; ++k)
    35                     tmp.e[i][j]+=e[i][k]*x.e[k][j],tmp.e[i][j]%=mod;
    36             }
    37         return tmp;
    38     }
    39 }ans,base;
    40 
    41 int AC()
    42 {
    43 //    freopen("spfa.in","r",stdin);
    44 //    freopen("spfa.out","w",stdout);
    45     read(n);
    46     if(n==1||n==2) { puts("1"); return 0; }
    47     ans.init_ans(); base.init_base();
    48     for( n-=2; n; n>>=1,base=base*base)
    49         if(n&1) ans=ans*base;
    50     printf("%lld
    ",ans.e[0][0]);
    51     return 0;
    52 }
    53 
    54 int Aptal=AC();
    55 int main(){;}
    单位矩阵 在第一行
     1 #include <cstdio>
     2 
     3 #define LL long long
     4 const LL mod(1000000007);
     5 
     6 inline void read(LL &x)
     7 {
     8     x=0; register char ch=getchar();
     9     for(; ch>'9'||ch<'0'; ) ch=getchar();
    10     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
    11 }
    12 
    13 LL n,m;
    14 struct Matrix_fb {
    15     LL e[2][2];
    16     void init_base()
    17     {
    18         e[0][0]=1;
    19         e[0][1]=1;
    20         e[1][0]=1;
    21         e[1][1]=0;
    22     }
    23     void init_ans()
    24     {
    25         e[0][0]=e[1][1]=1;
    26     }
    27     Matrix_fb operator * (Matrix_fb x) const
    28     {
    29         Matrix_fb tmp;
    30         for(int i=0; i<2; ++i)
    31             for(int j=0; j<2; ++j)
    32             {
    33                 tmp.e[i][j]=0;
    34                 for(int k=0; k<2; ++k)
    35                     tmp.e[i][j]+=e[i][k]*x.e[k][j],tmp.e[i][j]%=mod;
    36             }
    37         return tmp;
    38     }
    39 }ans,base;
    40 
    41 LL GCD(LL a,LL b)
    42 {
    43     return !b ? a : GCD(b,a%b);
    44 }
    45 
    46 int AC()
    47 {
    48 //    freopen("spfa.in","r",stdin);
    49 //    freopen("spfa.out","w",stdout);gcd-=2
    50     read(n);
    51     if(n==1||n==2) { puts("1"); return 0; }
    52     ans.init_ans(); base.init_base();
    53     for( ; n; n>>=1,base=base*base)
    54         if(n&1) ans=ans*base;
    55     printf("%lld
    ",ans.e[0][1]);
    56     return 0;
    57 }
    58 
    59 int Aptal=AC();
    60 int main(){;}
    单位矩阵,在对角线
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    机器学习算法及应用领域相关的中国大牛[转]
    Awesome (and Free) Data Science Books[转]
    机器学习算法之旅【翻译】【转】
    const 引用的分析
    c++ 引用的分析
    读取BMP图像size的时候与操作和左移的原因
    java的equal和==问题
    mac10.9下安装Android
    c++设计模式系列----builder模式
    c++设计模式系列----单例模式(Singleton模式
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7510866.html
Copyright © 2011-2022 走看看