zoukankan      html  css  js  c++  java
  • HDU 1568 Fibonacci

    Fibonacci

    http://acm.hdu.edu.cn/showproblem.php?pid=1568

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2292    Accepted Submission(s): 1075


    Problem Description
    2007年到来了。经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列
    (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来。
    接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了。所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住。于是他决定编写一个程序来测验zouyu说的是否正确。
     
    Input
     
    输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾。
     
    Output
     
    输出f[n]的前4个数字(若不足4个数字,就全部输出)。
     
    Sample Input
     
    0
    1
    2
    3
    4
    5
    35
    36
    37
    38
    39
    40
     
    Sample Output
     
    0
    1
    1
    2
    3
    5
    9227
    1493
    2415
    3908
    6324
    1023
     
    Author
    daringQQ
     
    Source
     
    Recommend
    8600

    这题要利用到数列的公式:an=(1/√5) * [((1+√5)/2)^n-((1-√5)/2)^n](n=1,2,3.....)

    完对数


    忽略最后一项(--->0)

    初一想,这里的每一位在相乘的过程中对可能对最高位的值有贡献,所以相对求位数求余的求商肯定是不行了。   这里应该对 一个数有这样的理解,一个数是由每一位的基数乘以相对应的权值,例如 123456 , 基数"1"的权值为 10^5,  基数 "2" 的权值为 10^4......所以该题要求的就是最高位的基数。
        对 x^x 取对数,得 x* ln( x )/ ln( 10 ), 现假设这个值为 X.abcdeefg   那么 10^X 就是 最高位对应的权值,10^ 0.abcdefg 就是最高位的基数。注意这里得到的并不是一个整数,为什么呢? 因为这里是强行将后面位的值也转化到最高位上来了,这有点像大数中,如果不满进制却强行进位,显然那样会进给高位一个小数而不是一个天经地义的整数。得到 10^ 0.abcdefg 后,再用 double floor ( double ) 函数取下整就得到最高位的数值大小了
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    
    using namespace std;
    
    int n;
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int num[25];
        num[0]=0;num[1]=1;
        for(int i=2;i<20;i++)
            num[i]=num[i-1]+num[i-2];
        while(~scanf("%d",&n)){
            if(n<20)
                printf("%d\n",num[n]);
            else{
                double tmp=0.5*log10(0.2)+n*log10((1+sqrt(5.0))/2.0);
                tmp=tmp-floor(tmp);
                tmp=pow(10.0,tmp);
                while(tmp<1000)
                    tmp*=10;
                printf("%d\n",(int)(tmp));
            }
        }
        return 0;
    }
  • 相关阅读:
    Effective C++笔记_条款25考虑写出一个不抛出异常的swap函数
    SilverLight小游戏
    可分片数据持久层ShardingPL使用说明
    r.a.d controls 学习笔记 - radcombobox + RadWindowManager 组成的参照控件
    用 .Net Remoting 技术实现“定向广播”
    Silverlight中的资源文件
    一个丑陋的对Silverlight中的Grid无CellPadding的解决方案
    几个小问题
    .Net Remoting 排错笔记:An existing connection was forcibly closed by the remote host
    .Net Remoting 排错笔记:The constructor to deserialize an object of type 'xxx.xxxx.xxxxx' was not found.
  • 原文地址:https://www.cnblogs.com/jackge/p/2850879.html
Copyright © 2011-2022 走看看