結果

問題 No.1136 Four Points Tour
ユーザー lam6er
提出日時 2025-03-20 21:09:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 771 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 53,816 KB
最終ジャッジ日時 2025-03-20 21:09:46
合計ジャッジ時間 2,818 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def multiply(a, b):
    """Multiply two 2x2 matrices."""
    res = [[0] * 2 for _ in range(2)]
    for i in range(2):
        for j in range(2):
            res[i][j] = (a[i][0] * b[0][j] + a[i][1] * b[1][j]) % MOD
    return res

def matrix_power(mat, power):
    """Compute the matrix exponent using binary exponentiation."""
    result = [[1, 0], [0, 1]]  # Identity matrix
    while power > 0:
        if power % 2 == 1:
            result = multiply(result, mat)
        mat = multiply(mat, mat)
        power //= 2
    return result

n = int(input())

# Handle the case when n is 0 (though per problem statement, n >=1)
if n == 0:
    print(1)
else:
    matrix = [[0, 1], [3, 2]]
    mat_pow = matrix_power(matrix, n)
    print(mat_pow[0][0] % MOD)
0