题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568
从本题给的n的数据范围来看,这应该是一道推公式的数学题。。。。
用到了Fibonacci数列的通项公式
经过对数运算可得
因为最后一项log10(1-((1-sqrt(5))/(1+sqrt(5)))^n)趋近于0,可以不用计算。
之后取计算得到的小数部分做pow(10.0,小数)的运算,如果不是四位一直*10就ok了。
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int f[25];
int main()
{
int n;
while(cin>>n)
{
f[0]=0,f[1]=1;
for(int i=2;i<=20;i++)
f[i]=f[i-1]+f[i-2];
if(n<=20) cout<<f[n]<<endl;
else
{
double t=log10(1.0/sqrt(5))+(double)n*log10((1.0+sqrt(5))/2.0);
t=t-floor(t);
t=pow(10.0,t);
while(t<=1000)
t=t*10;
printf("%d\n",(int)t);
}
}
}