Fibonacci number’s ratio (the hard version)

本文探讨了计算特定形式的Fibonacci数列的极限值的方法,包括特殊情况和极限存在的条件。

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;
}

 

转载于:https://www.cnblogs.com/111qqz/p/4725561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值