zoukankan      html  css  js  c++  java
  • 51Nod——T 1242 斐波那契数列的第N项

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242

    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     收藏
     关注
    斐波那契数列的定义如下:
     
    F(0) = 0
    F(1) = 1
    F(n) = F(n - 1) + F(n - 2) (n >= 2)
     
    (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)
    给出n,求F(n),由于结果很大,输出F(n) % 1000000009的结果即可。
     
    Input
    输入1个数n(1 <= n <= 10^18)。
    Output
    输出F(n) % 1000000009的结果。
    Input示例
    11
    Output示例
    89

     1 #include <cstdio>
     2 
     3 const int mod(1000000009);
     4 #define LL long long
     5 inline void read(LL &x)
     6 {
     7     x=0; register char ch=getchar();
     8     for(; ch>'9'||ch<'0'; ) ch=getchar();
     9     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
    10 }
    11 LL n;
    12 
    13 struct Matrix {
    14     LL e[2][2];
    15     Matrix operator * (const Matrix x) const
    16     {
    17         Matrix tmp;
    18         for(int i=0; i<2; ++i)
    19             for(int j=0; j<2; ++j)
    20             {
    21                 tmp.e[i][j]=0;
    22                 for(int k=0; k<2; ++k)
    23                     tmp.e[i][j]+=e[i][k]*x.e[k][j],tmp.e[i][j]%=mod;
    24             }
    25         return tmp;
    26     }
    27 }ans,base;
    28 
    29 int Presist()
    30 {
    31     read(n);
    32     ans.e[0][0]=ans.e[1][1]=1;
    33     base.e[0][0]=base.e[0][1]=base.e[1][0]=1;
    34     for(; n; n>>=1, base=base*base)
    35         if(n&1) ans=ans*base;
    36     printf("%lld",ans.e[0][1]);
    37     return 0;
    38 }
    39 
    40 int Aptal=Presist();
    41 int main(){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    Python基础-序列化模块
    dubbox
    小型供销系统
    MyBatis与SpringBoot整合案例(一)
    SpringBoot第二节
    SpringBoot第一节
    Dubbo案例SSM整合
    Dubbo生产者和消费者
    Zookeeper实战分布式锁
    Zookeeper Watcher和选举机制
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7529929.html
Copyright © 2011-2022 走看看