日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 1568 Fibonacci

發(fā)布時(shí)間:2024/4/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1568 Fibonacci 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

Problem Description

2007年到來了。經(jīng)過2006年一年的修煉,數(shù)學(xué)神童zouyu終于把0到100000000的Fibonacci數(shù)列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2] (i>=2))的值全部給背了下來。接下來,CodeStar決定要考考他,于是每問他一個(gè)數(shù)字,他就要把答案說出來,不過有的數(shù)字太長了。所以規(guī)定超過4位的只要說出前4位就可以了,可是CodeStar自己又記不住。于是他決定編寫一個(gè)程序來測驗(yàn)zouyu說的是否正確。

Input

輸入若干數(shù)字n(0 <= n <= 100000000),每個(gè)數(shù)字一行。讀到文件尾。

Output

輸出f[n]的前4個(gè)數(shù)字(若不足4個(gè)數(shù)字,就全部輸出)。

Sample Input

0
1
2
3
4
5
35
36
37
38
39
40

Sample Output

0
1
1
2
3
5
9227
1493
2415
3908
6324
1023

AC

  • 求第n項(xiàng)的斐波那契值,可以用公式和矩陣快速冪,但是這個(gè)題讓輸出答案的前4位,只能把所有的值求出來才能得到前四位
  • 就用這個(gè)公式求第n項(xiàng)
  • 如果直接套用公式是的話會(huì)超時(shí),這里可以兩邊求對數(shù)(log10),得到答案的小數(shù)部分,然后在求小數(shù)對應(yīng)的10的次方,這樣就得到了原答案除以n個(gè)10的結(jié)果舉個(gè)例子:
    x = 127
    log10(x) = 2.103803720955957
    10的0.103803720955957次方=1.27
    這樣求前4位,只用乘以1000就行
  • 先對公式進(jìn)行化簡,然后取對數(shù)
  • 當(dāng)n比較小的時(shí)候,公式計(jì)算不準(zhǔn)確,需要把前25項(xiàng)打表出來
#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <map> #include <bitset> #include <set> #include <string.h> #include <cmath> #include <queue> #include <algorithm> #define N 100005 #define P pair<int,int> #define ll long long #define lowbit(a) a&(-a) #define mk(a, b) make_pair(a, b) #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; int f[40]; int main(){ #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout); #endiff[0] = 0;f[1] = 1;for (int i = 2; i <= 20; ++i) {f[i] = f[i - 1] + f[i - 2]; // cout << f[i] << endl;}int n;while (scanf("%d", &n) != EOF) {if (n <= 20)printf("%d\n", f[n]);else {double temp1 = log10(1 / sqrt(5.0));double temp2 = log10((1 + sqrt(5.0)) / 2);double temp = temp1 + n * temp2;double zhi = temp - floor(temp);double ans_x = pow(10, zhi);int ans = ans_x * 1000;printf("%d\n", ans);}}#ifndef ONLINE_JUDGEfclose(stdin);fclose(stdout);system("out.txt"); #endifreturn 0; } 與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的HDU 1568 Fibonacci的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。