結果

問題 No.2877 Gunegune Hyperion
ユーザー 寝癖
提出日時 2024-08-30 00:46:35
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,470 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 437,136 KB
最終ジャッジ日時 2024-09-10 18:01:37
合計ジャッジ時間 102,600 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np


def convolve(f, g):
    fft_len = 1
    while 2 * fft_len < len(f) + len(g) - 1:
        fft_len *= 2
    fft_len *= 2
    Ff = np.fft.rfft(f, fft_len, axis=0)
    Fg = np.fft.rfft(g, fft_len, axis=0)
    Fh = Ff @ Fg
    h = np.fft.irfft(Fh, fft_len, axis=0)
    h = np.rint(h).astype(np.int64)
    return h[: len(f) + len(g) - 1]


def convolution(f, g, p=998244353):
    f1, f2 = np.divmod(f, 1 << 15)
    g1, g2 = np.divmod(g, 1 << 15)
    a = convolve(f1, g1) % p
    c = convolve(f2, g2) % p
    b = (convolve(f1 + f2, g1 + g2) - a - c) % p
    return ((a << 30) + (b << 15) + c) % p


def poly_pow(f, n, p=998244353):
    res = np.eye(3, dtype=np.int64).reshape(1, 3, 3)
    while n:
        if n & 1:
            res = convolution(res, f, p)
        f = convolution(f, f, p)
        n >>= 1
    return res


mod = 998244353
inv2, inv3, inv5 = pow(2, mod - 2, mod), pow(3, mod - 2, mod), pow(5, mod - 2, mod)
P = np.array(
    [
        [[inv2, 0, 0], [inv3, 0, 0], [0, 0, 0]],
        [[0, inv2, 0], [0, inv3, inv3], [0, 2 * inv3, inv3]],
    ]
)
v = np.array([[2 * inv5 % mod, 0, 0], [0, 2 * inv5 % mod, inv5]])

if __name__ == "__main__":
    N, H = map(int, input().split())
    P = poly_pow(P, N - 1, mod)
    A = [v[i] @ P[H - i :] % mod for i in range(2)]

    ans = 0
    for a in A:
        for row in a:
            for x in row:
                ans += x
                if ans >= mod:
                    ans -= mod
    print(ans)
0