HDU 1568 快速求斐波那契前四位

思路:

把斐波那契通項公式轉化成log的形式,高中數學...

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
int f[21],n;
int main(){
    f[1]=f[2]=1;
    for(int i=3;i<=20;i++)f[i]=f[i-1]+f[i-2];
    while(~scanf("%d",&n)){
        if(n<=20)printf("%d\n",f[n]);
        else{
            double s=log10(1/sqrt(5))+n*log10((1+sqrt(5))/2),t;
            s-=(int)s,t=pow(10,s);
            while(t<1000)t*=10;
            printf("%d\n",(int)t);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章