51nod 1242 斐波那契數列的第N項

2021-08-14 12:00:00 字數 985 閱讀 3500

斐波那契數列的定義如下:

f(0) = 0

f(1) = 1

f(n) = f(n - 1) + f(n - 2) (n >= 2)

(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)

給出n,求f(n),由於結果很大,輸出f(n) % 1000000009的結果即可。

input

輸入1個數n(1 <= n <= 10^18)。
output

輸出f(n) % 1000000009的結果。
input示例

11
output示例

89
思路:

構造矩陣來根據f(n-1), f(n-2)得到f(n), f(n-1) 。然後利用矩陣快速冪來計算。

#include #include #include #include #include using namespace std;

typedef long long int ll;

const int mod = 1e9 + 9;

struct node;

ll n;

node mul(node a, node b)

node pow(ll n)

; node result = ;

while (n)

n >>= 1;

base = mul(base, base); }

return result;}

int main()

node a = pow(n - 1);

ll result = a.v[0][0];

cout << result << endl;

return 0;

}

51Nod 1242 斐波那契數列的第N項

1242 斐波那契數列的第n項 基準時間限制 1 秒 空間限制 131072 kb 分值 0 難度 基礎題 題目鏈結 斐波那契數列的定義如下 f 0 0 f 1 1 f n f n 1 f n 2 n 2 1,1,2,3,5,8,13,21,34,55,89,144,233,377,給出n,求f n...

51Nod 1242 斐波那契數列的第n項

1242 斐波那契數列的第n項 基準時間限制 1 秒 空間限制 131072 kb 分值 0 難度 基礎題 斐波那契數列的定義如下 f 0 0 f 1 1 f n f n 1 f n 2 n 2 1,1,2,3,5,8,13,21,34,55,89,144,233,377,給出n,求f n 由於結果...

51nod 1242 斐波那契數列的第N項

之前一直沒敢做矩陣一類的題目 其實還好吧 但是後面的斐波那契 推導不是很懂 前面講的挺好的 後來看到了 相當於 是乙個那個東西的k 1次方 而且由於 f 1 1 所以直接求k 1次方就可以了 includeusing namespace std const int mod 1e9 9 typedef...