zoukankan      html  css  js  c++  java
  • 洛谷——P1306 斐波那契公约数

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

    题目描述

    对于Fibonacci数列:1,1,2,3,5,8,13......大家应该很熟悉吧~~~但是现在有一个很“简单”问题:第n项和第m项的最大公约数是多少?

    输入输出格式

    输入格式:

    两个正整数n和m。(n,m<=10^9)

    注意:数据很大

    输出格式:

    Fn和Fm的最大公约数。

    由于看了大数字就头晕,所以只要输出最后的8位数字就可以了。

    输入输出样例

    输入样例#1:
    4 7
    输出样例#1:
    1

    说明

    用递归&递推会超时

    用通项公式也会超时

    数据较水

    gcd( fb(n) , fb(m) )   ==   fb( gcd(n,m) )

     1 #include <cstdio>
     2 
     3 #define LL long long
     4 const LL mod(100000000);
     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 
    15 LL GCD(LL a,LL b)
    16 {
    17     return !b ? a : GCD(b,a%b);
    18 }
    19 
    20 LL fb(LL x)
    21 {
    22     if(x==2||x==1) return 1;
    23     LL f1=1,f2=1,tmp;
    24     for(LL i=3; i<=x; ++i)
    25     {
    26         tmp=f2%mod;
    27         f2=(f1%mod+f2%mod)%mod;
    28         f1=tmp%mod;
    29     }
    30     return f2%mod;
    31 }
    32 
    33 int AC()
    34 {
    35     read(n),read(m);
    36     LL gcd=GCD(n,m);
    37     printf("%lld
    ",fb(gcd));
    38     return 0;
    39 }
    40 
    41 int Aptal=AC();
    42 int main(){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    UICollectionView添加 HeaderView FooterView
    Swift-UIDynamic初见
    Swift归档
    通知NSNotication&通知中心NSNoticationCenter
    iOS沙盒
    lldb-320.4.152﹣Debugger commands:
    UIPickerView採摘控件的學習
    hive的优化
    zookeeper简易配置及hadoop高可用安装
    hadoop组件概念理解
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7510855.html
Copyright © 2011-2022 走看看