您的位置:首页 > 其它

Hdu 1568 Fibonacci

2012-12-13 16:09 369 查看
大意不再赘述。

思路:师兄给我的资料上的推荐例题,YY了一下,这一题主要是利用Fibonacci的通项公式与积分的思想去求解的,虽然代码丑了点,但还是AC了。





当n比较大时,据我估计应该是31左右,由于只求前四项,最后一项可以忽略掉,通项公式变为:F(n) = -0.5*log10(5.0)+((double)n)*log10(f); f = (1.0+sqrt(5.0))/2.0;

另外,当n = 20时,F(n) = 6765,n = 21,F(n) = 10946,所以可以通过预处理得出结果。

待会去写写 HIT 1864

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

int f[22];

void init()
{
	f[0] = 0, f[1] = 1;
	for(int i = 2; i <= 21; i++) f[i] = f[i-1]+f[i-2];
}

int cal(int n)
{
	double f = (1.0+sqrt(5.0))/2.0;
	double a = -0.5*log10(5.0)+((double)n)*log10(f);
	double b = a-(int)a;
	double ans = pow(10.0, b);
	ans *= 1000;
	return (int)ans;
}

int main()
{
	int n;
	init();
	while(~scanf("%d", &n))
	{
		if(n <= 20) { printf("%d\n", f
); continue;}
		int ans = cal(n);
		printf("%d\n", ans);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: