結果

問題 No.1879 How many matchings?
ユーザー gew1fw
提出日時 2025-06-12 15:45:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 844 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 52,736 KB
最終ジャッジ日時 2025-06-12 15:45:31
合計ジャッジ時間 1,474 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def matrix_mult(a, b):
    return [
        [(a[0][0]*b[0][0] + a[0][1]*b[1][0]) % MOD,
         (a[0][0]*b[0][1] + a[0][1]*b[1][1]) % MOD],
        [(a[1][0]*b[0][0] + a[1][1]*b[1][0]) % MOD,
         (a[1][0]*b[0][1] + a[1][1]*b[1][1]) % MOD]
    ]

def matrix_pow(mat, power):
    result = [[1, 0], [0, 1]]  # Identity matrix
    while power > 0:
        if power % 2 == 1:
            result = matrix_mult(result, mat)
        mat = matrix_mult(mat, mat)
        power //= 2
    return result

def fibonacci(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    mat = [[1, 1], [1, 0]]
    result = matrix_pow(mat, n-1)
    return result[0][0]

n = int(input())
if n == 0:
    print(0)
else:
    if n % 2 == 0:
        k = (n // 2) + 1
    else:
        k = ((n + 1) // 2) + 1
    print(fibonacci(k) % MOD)
0