結果
問題 | No.344 ある無理数の累乗 |
ユーザー |
![]() |
提出日時 | 2025-03-20 19:02:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 40 ms / 2,000 ms |
コード長 | 902 bytes |
コンパイル時間 | 435 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 54,244 KB |
最終ジャッジ日時 | 2025-03-20 19:03:02 |
合計ジャッジ時間 | 2,594 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
def multiply_matrix(a, b, mod): res = [[0]*2 for _ in range(2)] res[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % mod res[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % mod res[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % mod res[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % mod return res def matrix_power(mat, power, mod): result = [[1, 0], [0, 1]] # Identity matrix while power > 0: if power % 2 == 1: result = multiply_matrix(result, mat, mod) mat = multiply_matrix(mat, mat, mod) power = power // 2 return result n = int(input()) if n == 0: print(1) else: k = n - 1 M = [[2, 2], [1, 0]] mod = 1000 powered = matrix_power(M, k, mod) a_n = (powered[0][0] * 2 + powered[0][1] * 2) % mod if n % 2 == 0: ans = (a_n - 1) % mod else: ans = a_n % mod print(ans)