• hdu 1568关于斐波那契数列的公式及其思维技巧


    先看对数的性质,loga(b^c)=c*loga(b),loga(b*c)=loga(b)+loga(c);
    假设给出一个数10234432,那么log10(10234432)=log10(1.0234432*10^7)=log10(1.0234432)+7;
    
    log10(1.0234432)就是log10(10234432)的小数部分.
    
    log10(1.0234432)=0.010063744
    10^0.010063744=1.023443198
    那么要取几位就很明显了吧~
    先取对数(对10取),然后得到结果的小数部分bit,pow(10.0,bit)以后如果答案还是<1000那么就一直乘10。
    注意偶先处理了0~20项是为了方便处理~
    
    这题要利用到数列的公式:an=(1/√5) * [((1+√5)/2)^n-((1-√5)/2)^n](n=1,2,3.....)
    
    取完对数
    
    log10(an)=-0.5*log10(5.0)+((double)n)*log(f)/log(10.0)+log10(1-((1-√5)/(1+√5))^n)其中f=(sqrt(5.0)+1.0)/2.0;
    log10(1-((1-√5)/(1+√5))^n)->0
    所以可以写成log10(an)=-0.5*log10(5.0)+((double)n)*log(f)/log(10.0);
    最后取其小数部分。
    
    #include<iostream>
    #include<cmath>
    using namespace std;
    int fac[21]={0,1,1};
    const double f=(sqrt(5.0)+1.0)/2.0;
    int main()
    {
        double bit;
        int n,i;
        for(i=3;i<=20;i++)fac[i]=fac[i-1]+fac[i-2];//求前20项
        while(cin>>n)
        {
            if(n<=20)
            {
                cout<<fac[n]<<endl;
                continue;
            }
            bit=-0.5*log(5.0)/log(10.0)+((double)n)*log(f)/log(10.0);//忽略最后一项无穷小
            bit=bit-floor(bit);
            bit=pow(10.0,bit);
            while(bit<1000)bit=bit*10.0;
            printf("%d
    ",(int)bit);
        }
        return 0;
    }

  • 相关阅读:
    XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--B.Petrol
    XVI Open Cup named after E.V. Pankratiev. GP of Eurasia
    Petrozavodsk Winter Camp, Warsaw U, 2014, A The Carpet
    训练日志4
    训练日志3
    训练日志2
    多校中期总结
    训练日志
    计算几何学习12 + 组队训练
    计算几何学习11
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410709.html
Copyright © 2020-2023  润新知