(hdu step 2.2.1)Fibonacci(求当n很大时的斐波那契数)

题目:

Fibonacci

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

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

Happy 2007


Recommend

8600

题目分析:

这道题和那些斐波那契水题不一样。这里的n很大(n<100000000)。在这道题中直接开数组打表是行不通的,那样会超时。所以这道题采用以下思路来解决:

1)当n较小时,直接从打好的表中输出相应的斐波那契数。

2)当n较大时,使用公式并结合对数的性质来解决。

以下是用到的一些推导:

(1)我们要知道斐波那契数列的通项公式:F[N]=(1/√5) * [((1+√5)/2)^N-((1-√5)/2)^N].

(2)对数log的强悍(以10为底):对两边取对数

logF[N]=-0.5*log5+log [((1+√5)/2)^N-((1-√5)/2)^N].

我们知道当N小于21的时候,斐波那契的数值不超过四位,而当N超过21时,((1-√5)/2)^N的值已经趋向于0了,我们可以不管 这项。那么原式就可以化为:

logF[N]=-0.5*log5+N*log (1+√5)/2

把后面的记为K=-0.5*log5+N*log (1+√5)/2

那么  10^K=F[N];!!!

举个例子: 10^2.3=199.5262314.......

10^0.3=1.995262314.......

这样具体的数字很直观,对映到 10^K=F[N],取K的小数部分后,10^K就变为了科学计数的形式,那么此时你要取多少位就可以取 多少位,就像要是你知道了10^0.3,那么你想得到1.995262314......的几位就几位!!

代码如下:

/*
 * a1.cpp
 *
 *  Created on: 2015年2月2日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 21;//斐波那契的地21项是1094
int f[maxn];

/**
 * 用于处理不是很大的斐波那契数。
 * 在这里如果n小于21,就直接从大号的斐波那契表中输出
 */
void init(){
	int i;
	f[1] = 1;
	f[2] = 1;
	for(i = 3 ; i < maxn ; ++i){
		f[i] = f[i-1] + f[i-2];
	}
}

/**
 * 朱勇用于处理斐波那契数很大的情况
 * 在这里主要是用到了公式来解决这个问题
 */
int solve(int n){
	double k = -0.5*log10(5) + n*log10((1+sqrt(5*1.0))/2);
	k -= (int)k;//讲幂数变成0.X的形式方便后面处理
	double ans = pow(10,k);

	while(ans < 1000){//只输出前4位
		ans *= 10;
	}

//	cout << ans << endl;
	return (int)ans;//取整,去掉小数点后面的数
}

int main(){
	int n;
	init();
	while(scanf("%d",&n)!=EOF){
		if(n < maxn){//如果n小于21则直接从大号的表中输出
			printf("%d\n",f[n]);
		}else{//如果n>21则用公式计算好后在输出相应的斐波那契数
			printf("%d\n",solve(n));
//			solve(n);
		}
	}

	return 0;
}
时间: 2024-07-30 13:47:53

(hdu step 2.2.1)Fibonacci(求当n很大时的斐波那契数)的相关文章

poj 3070 Fibonacci 【矩阵快速幂 求第N个斐波那契数%1000】

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11123   Accepted: 7913 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequenc

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

hdu1568&amp;&amp;hdu3117 求斐波那契数前四位和后四位

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=20的时候,不足四位,所以直接打表. 当n>20的时候,大于四位的时候,ans满足这个公式:ans=-0.5*log10(5.0)+num*1.0*log10((1+sqrt(5.0))/2.0); 这个公式是怎么来的呢?我们可以对an取10的对数,根据对数的性质. log10(ans)=log10(1/

Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1] + F[n-2], where F[1] = 1 and F[2] = 1. It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digi

hdu 1316 How many Fibs?(高精度斐波那契数)

//  大数继续 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n >= 3) Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b]. Input The input contains several test cas

C语言用递归求斐波那契数,让你发现递归的缺陷和效率瓶颈

递归是一种强有力的技巧,但和其他技巧一样,它也可能被误用. 一般需要递归解决的问题有两个特点: 存在限制条件,当符合这个条件时递归便不再继续: 每次递归调用之后越来越接近这个限制条件. 递归使用最常见的一个例子就是求阶乘,具体描述和代码请看这里:C语言递归和迭代法求阶乘 但是,递归函数调用将涉及一些运行时开销--参数必须压到堆栈中,为局部变量分配内存空间(所有递归均如此,并非特指求阶乘这个例子),寄存器的值必须保存等.当递归函数的每次调用返回时,上述这些操作必须还原,恢复成原来的样子.所以, 基

「递归」求第n个斐波纳契数

用「递归」方法求第n个斐波纳契数 1 #include<stdio.h> 2 long int dog(int p) 3 { 4 if(p>1) 5 return dog(p-1)+dog(p-2); 6 else if (p==1||p==0) 7 return 1; 8 } 9 int main() 10 { 11 printf("您要求第几个斐波纳契数:\n"); 12 int n; 13 scanf("%d",&n); 14 pri

HDU 5914 Triangle(打表——斐波那契数的应用)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whose lengths are 1,2, 3?n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides t

递归求斐波那契数

斐波那契数列主要思想是利用前两个数求和算出下一个数,利用函数的递归思想,F(n)=F(n-1)+F(n-2),F(n)先搁置,计算F(n-1),要计算F(n-1)就要先计算F(n-2)和F(n-3),依次递归下去,直到第一第二位数,这两个数是已知的,这样就可以回去一层一层的算出F(3).F(4).F(5)....F(n-2).F(n-1),最后得到F(n)的值. 1 using System; 2 using System.Collections.Generic; 3 using System.