結果

問題 No.3243 Multiplication 8 1
ユーザー amesyu
提出日時 2025-08-22 23:09:53
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,065 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 79,576 KB
最終ジャッジ日時 2025-08-22 23:10:42
合計ジャッジ時間 8,167 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 TLE * 2 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

def matmul(a, b):
    n = len(a)
    c = [[0] * n for _ in range(n)]

    for i in range(n):
        for j in range(n):
            for k in range(n):
                c[i][j] += a[i][k] * b[k][j]
                c[i][j] %= 998244353
    return c


def matpow(A, k):
    res = [[int(i == j) for j in range(len(A))] for i in range(len(A))]
    p = A
    while k:
        if k & 1:
            res = matmul(res, p)
        p = matmul(p, p)
        k >>= 1
    return res


T = int(input())

for _ in range(T):
    n = int(input())

    state = [1, 2, 4, -1, -2, -4, -8, 1, 2, 4, -1, -2, -4, -8]
    select = [1, -1, 2, -2]

    sz = len(state)
    mat = [[0] * sz for _ in range(sz)]

    for i in range(sz):
        for y in select:
            x = state[i]
            z = x * y

            if z == 8:
                j = 7
                assert state[j] == 1
            elif z not in state:
                continue
            else:
                j = state.index(z) + 7 * int(i >= 7)

            mat[i][j] += 1

    res = matpow(mat, n)
    print(res[0][7])
0