結果

問題 No.1810 RGB Biscuits
ユーザー H3PO4
提出日時 2022-01-14 21:46:11
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,321 ms / 2,000 ms
コード長 835 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 44,392 KB
最終ジャッジ日時 2024-11-20 09:13:42
合計ジャッジ時間 20,801 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

MOD = 10 ** 9 + 7


def pow_matrix_mod(x, n, mod=MOD):
    if not n:
        return np.eye(len(x), dtype=object)
    if n % 2 == 0:
        return pow_matrix_mod(x @ x % mod, n // 2) % mod
    else:
        return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod


A, B = map(int, input().split())
matr_odd = np.array([[1, 0, A],
                     [0, 1, B],
                     [0, 0, 1]],
                    dtype=object)
matr_even = np.array([[0, 1, 0],
                      [0, 0, 0],
                      [1, 0, 0]],
                     dtype=object)
matr = (matr_odd @ matr_even) % MOD
N = int(input())
for _ in range(N):
    T = int(input())
    p = pow_matrix_mod(matr, T // 2)
    if T % 2:
        p = p @ matr_odd
        p %= MOD
    ans = np.sum(np.array([1, 1, 0]) @ p) % MOD
    print(ans)
0