Fibonacci

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2087 Accepted Submission(s): 999

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
 
解答:
   1:  #include<iostream>
   2:  #include<cmath>
   3:  using namespace std;
   4:  int fib[21]={0,1};
   5:  int main(){
   6:      double a=-0.5*log10(5.0);
   7:      double b=log10(0.5+sqrt(5.0)/2);
   8:      double c=(1-sqrt(5.0))/(1+sqrt(5.0));
   9:      for(int i=2;i<=20;i++){
  10:          fib[i]=fib[i-1]+fib[i-2];    
  11:      }
  12:      for(int n;cin>>n;){
  13:          if(n<=20){
  14:              int ans=fib[n];
  15:              while(ans>=10000) ans/=10;
  16:              cout<<ans<<"\n";
  17:          }
  18:          else{
  19:              double ans=a+n*b+log10(1+pow(c,n));
  20:              ans-=(int)ans;
  21:              ans=pow(10.0,ans);
  22:              while(ans<1000) ans*=10;
  23:              cout<<(int)ans<<"\n";
  24:          }
  25:      }
  26:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

分析:

先看对数的性质,;
假设给出一个数10234432,那么;
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);
最后取其小数部分。

hdu acmsteps 2.2.1 Fibonacci的更多相关文章

  1. HDU ACM-Steps

    HDU ACM-Steps RECORD Chapter 1 Section 1 暖手题 1.1.1 A+B for Input-Output Practice (I) #include <st ...

  2. hdu 1250 Hat&#39;s Fibonacci

    pid=1250">点击此处就可以传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding ...

  3. HDU 1708 简单dp问题 Fibonacci String

    Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3306 Another kind of Fibonacci ---构造矩阵***

    Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  5. HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)

    Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...

  6. HDU 5451 Best Solver(fibonacci)

    感谢这道题让我复习了一遍线代,还学习了一些奇奇怪怪的数论. 令 二项展开以后根号部分抵消了 显然有 所以要求的答案是 如果n比较小的话,可以直接对二项式快速幂,但是这题n很大 这个问题和矩阵的特征值以 ...

  7. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  8. HDU 3306 Another kind of Fibonacci(快速幂矩阵)

    题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...

  9. hdu acmsteps 2.1.8 Leftmost Digit

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. javascript中的hasOwnProperty和isPrototypeOf

    hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.isPrototypeOf ...

  2. 09Spring_AOP介绍和java本身的动态代理以及cglib代理

    Aspect Oriented Programming 面向切面编程 1. 业界 AOP 实际上 OOP (面向对象编程 ) 延伸 ----  OOP编程语言. AOP设计思想,下面给出一张AOP的设 ...

  3. CentOS 7设置网络开机自动连接

    用root登陆系统 修改/etc/sysconfig/network-scripts/ifcfg-enpxxxxxx(xxx)文件,其内容原本如下 TYPE=Ethernet BOOTPROTO=dh ...

  4. JS 禁用和重新启用a标签的点击事件

    function changeHomePageModule(){ var css = $('#collapseExample').attr('class'); if(css=='collapse'){ ...

  5. MS-SQL SERVER单列合并的四种常用方法

    /* 原始数据 ID Name ----------- -------------------- 1 吕布 2 张飞 3 赵云 结果 ----------------- 吕布,张飞,赵云 */ )) ...

  6. memcached 介绍

    博客园相关文章功能中使用了memcached,在网上搜集了一些memcached方面的文章: memcached完全剖析 分布式缓存系统Memcached简介与实践 Memcached深度分析 自己实 ...

  7. liunx检查与安装软件包

    检查软件包# rpm -qa | grep 例如:# rpm -qa | grep make检查make包 安装软件包 yum install 例如:yum install  unixODBC安装un ...

  8. edgesForExtendedLayout、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets属性详解 )——转载

    edgesForExtendedLayout: 在ios7适配中,布局问题是一个很头痛也很重要的问题,因为在ios7中viewController使用了全屏布局的方式,也就是说导航栏和状态栏都是不占实 ...

  9. 实验箱FPGA部分测试报告及A8与FPGA链接测试报告

    其实,我一开始还以为实验箱不会有什么问题只是让我们多学习东西才做这个测试的,结果发现还真的有不少问题. 1.实验准备部分 安装驱动时,win10系统无法正确安装usb-blaster Windows ...

  10. Jenkins进阶系列之——04Publish Over FTP Plugin插件

    说明:这个插件可以将构建的产物(例如:Jar)发布到FTP中去. 官方说明:Publish Over FTP Plugin 安装步骤: 系统管理→管理插件→可选插件→Artifact Uploader ...