求fibonacci数列第N项的前四位
想着高精度+快速幂+矩阵太麻烦了....
数学通项公式要用log优化处理(防止overflow)
#include <stdio.h>
#include <math.h>
int a[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393};
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
if (n<20)
{
printf("%d\n",a[n]);
continue;
}
double s=log10(1.0/sqrt(5))+n*log10((1+sqrt(5))/2);
s=s-(int)s;
s=pow(10,s);
while (s<1000) s*=10;
printf("%d\n",(int)s);
}
}