zoukankan      html  css  js  c++  java
  • Fibonacci number’s ratio (the hard version)

    Problem C. Fibonacci number’s ratio (the hard version)
    Input file:
    Output file:
    Time limit:
    Memory limit:
    stdin
    stdout
    2s
    512 Mb
    Fibonacci numbers have many remarkable properties. For example, the ratio of two successive Fibonacci numbers
    tends to the golden ratio:

    F n+1
    1+ 5
    lim
    =
    .
    n→∞ F n
    2
    You are going to test this property for generalized Fibonacci numbers
    G n = aG n−1 + bG n−2 ,
    n ≥ 2.
    For given a, b, G 0 and G 1 calculate the limit
    G n+1
    ,
    n→∞ G n
    lim
    if it exists.
    Input
    The first line contains four integers a, b, G 0 , G 1 from −1000 to 1000.
    Output
    Print “YES” in the first line, if the limit exists, and “NO” otherwise. In the case of the positive result, print the
    limit itself in the second line. Absolute or relative error must not exceed 10 −6 .
    Examples
    stdin
    1 1 1 1
    0 0 1 1
    stdout
    YES
    1.6180339887
    NO
    Note
    If the sequence G n contains an infinite number of zeros, the limit does not exist.

    求极限。

    细节比较坑,主要是可能为0

    为不乱,可以把a,b,g0,g1为0 的情况分别列出来。

    g[n]=a*g[n-1]+b*g[n-2];

    g[n]/g[n-1] = a + b *(g[n-2]/g[n-1])

    假设 g[n]/g[n-1]的极限存在,社为k

    那么有 k = a + b * (1/k)

    k*k-a*k-b=0;

    delta = sqrt(a*a+4(b)

    lim = (1+delta)/2.0  or (1-delta)/2.0;

    /*************************************************************************
        > File Name: code/whust/##1/A.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com 
        > Created Time: 2015年08月12日 星期三 13时38分02秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    #define y0 abc111qqz
    #define y1 hust111qqz
    #define yn hez111qqz
    #define j1 cute111qqz
    #define tm crazy111qqz
    #define lr dying111qqz
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)  
    typedef long long LL;
    typedef unsigned long long ULL;
    const int inf = 0x7fffffff;
    const long double C = 1.6180339887;
    const double eqs = 1E-6;
    int a,b,g0,g1;
    int main()
    {
        cin>>a>>b>>g0>>g1;
        double delta = a*a*1.0+4*b*1.0;
        if (delta<0)
        {
    
        cout<<"NO"<<endl;
        return 0;
        }
    
        if (a*g1==0&&b*g0==0)  //前两项为0,就会有无数个0,也是仅有的有无数个0的情况,根据提议,极限不存在。
        {
        cout<<"NO"<<endl;
        return 0;
        }
        if (b==0)   //根据公式,此时极限为常数a
        {
        cout<<"YES"<<endl;
        cout<<fixed<<setprecision(10)<<a*1.0<<endl;
        return 0;
        }
        if (a==0)
        {
            if (b*g0*g0==g1*g1)  //(sqrt(b)==(g1/g0))
            {
            if (g0*g1<0)   //初始值囧丁了极限的符号
            {
                cout<<"YES"<<endl;
                cout<<fixed<<setprecision(10)<<-1.0*sqrt(b)<<endl;
                return 0;
            }
            else
            {
                cout<<"YES"<<endl;
                cout<<fixed<<setprecision(10)<<1.0*sqrt(b)<<endl;
                return 0;
            }
    
            }
        }
        double lim1 = (a + sqrt(delta*1.0))/2.0;//两个可能的极限值
        double lim2 = (a - sqrt(delta*1.0))/2.0;
        double c1 = (g1-lim2*g0)/(lim1-lim2);
        double c2 = (lim1*g0-g1)/(lim1-lim2);
        if (fabs(c1)<eqs)
        {
        cout<<"YES"<<endl;
        cout<<fixed<<setprecision(10)<<lim2<<endl;
        return 0;
        }
        if (fabs(c2)<eqs)
        {
        cout<<"YES"<<endl;
        cout<<fixed<<setprecision(10)<<lim1<<endl;
        return 0;
        }
        double r = lim2/lim1;
        if (fabs(r)<1)
        {
        cout<<"YES"<<endl;
        cout<<fixed<<setprecision(10)<<lim1<<endl;
        }
        else
        {
        cout<<"YES"<<endl;
        cout<<fixed<<setprecision(10)<<lim2<<endl;
        }
    
    
        return 0;
    }
  • 相关阅读:
    Hadoop之MapReduce
    Hadoop之序列化
    Hadoop之mapreduce 实例五
    Hadoop之hive和hadoop的交互流程
    protel总结1
    将正数十进制转换为2~16进制数
    基于verilog分频器总结
    MATLAB设计FIR滤波器
    centos7+hadoop完全分布式集群搭建
    Linux下常用配置文件
  • 原文地址:https://www.cnblogs.com/111qqz/p/4725561.html
Copyright © 2011-2022 走看看