您的位置:首页 > 其它

HDU 1568 Fibonacci

2011-11-28 20:31 281 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1568

与 HDU 1060 Leftmost Digit 差不多

求后几位的话,可以构造矩阵相乘,二分求冪

求前几位,不需要,直接运用数学公式和对数的性质来求

斐波那契数列通项公式

代码

#include <stdio.h>
#include <math.h>
int main()
{
const int N=21;
int f
={0,1};
for (int i=2;i<N;i++) f[i]=f[i-1]+f[i-2];
int n;
while (scanf("%d",&n)!=EOF)
{
if (n<N)
{
printf("%d\n",f
);
continue;
}
double e,a;
e=log10(1/sqrt(5))+n*log10((1+sqrt(5))/2);
a=pow(10.0,e-floor(e));
printf("%d\n",(int)(a*1000));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: