結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー kamomekamome
提出日時 2019-07-02 00:20:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 676 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 8,404 KB
最終ジャッジ日時 2023-09-25 12:29:57
合計ジャッジ時間 1,761 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,224 KB
testcase_01 AC 16 ms
8,344 KB
testcase_02 AC 16 ms
8,296 KB
testcase_03 AC 16 ms
8,228 KB
testcase_04 AC 16 ms
8,268 KB
testcase_05 AC 16 ms
8,340 KB
testcase_06 AC 16 ms
8,404 KB
testcase_07 AC 16 ms
8,204 KB
testcase_08 AC 16 ms
8,300 KB
testcase_09 AC 16 ms
8,304 KB
testcase_10 AC 16 ms
8,148 KB
testcase_11 AC 16 ms
8,384 KB
testcase_12 AC 16 ms
8,152 KB
testcase_13 AC 16 ms
8,372 KB
testcase_14 AC 16 ms
8,200 KB
testcase_15 AC 16 ms
8,344 KB
testcase_16 AC 16 ms
8,160 KB
testcase_17 AC 16 ms
8,360 KB
testcase_18 AC 16 ms
8,216 KB
testcase_19 AC 16 ms
8,252 KB
testcase_20 AC 16 ms
8,380 KB
testcase_21 AC 17 ms
8,212 KB
testcase_22 AC 16 ms
8,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python3
#!coding: utf-8
from itertools import product
N=int(input())
def mat2_mul(X, Y):
    Z = [ [0, 0], 
          [0, 0] ]
    for (i,j,k) in product(range(2),range(2),range(2)):
        Z[i][j] += X[i][k] * Y[k][j]%1000000007
    return Z

def mat2_pow(X, n):
    if n == 0:
        return [ [1, 0],
                 [0, 1] ]
    elif n % 2:
        return mat2_mul(X, mat2_pow(X, n-1))  
    else:
        half_pow = mat2_pow(X, n/2)
        return mat2_mul(half_pow, half_pow)

def fib(n):
    if n == 0:
        return 0
    else:
        F = [ [0, 1],
              [1, 1] ]
        return mat2_pow(F, n-1)[1][1]


print(fib(N)*fib(N+1)%1000000007)
exit()
0