結果

問題 No.137 貯金箱の焦り
ユーザー maspymaspy
提出日時 2020-03-26 22:17:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,353 ms / 5,000 ms
コード長 1,866 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 37,908 KB
最終ジャッジ日時 2023-08-30 13:21:07
合計ジャッジ時間 19,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
30,000 KB
testcase_01 AC 134 ms
29,924 KB
testcase_02 AC 151 ms
29,960 KB
testcase_03 AC 132 ms
29,772 KB
testcase_04 AC 192 ms
30,580 KB
testcase_05 AC 143 ms
30,172 KB
testcase_06 AC 162 ms
30,216 KB
testcase_07 AC 151 ms
30,136 KB
testcase_08 AC 152 ms
30,116 KB
testcase_09 AC 166 ms
30,184 KB
testcase_10 AC 152 ms
30,012 KB
testcase_11 AC 143 ms
29,760 KB
testcase_12 AC 2,353 ms
37,908 KB
testcase_13 AC 278 ms
33,984 KB
testcase_14 AC 1,176 ms
34,028 KB
testcase_15 AC 1,156 ms
34,028 KB
testcase_16 AC 1,201 ms
33,872 KB
testcase_17 AC 613 ms
31,804 KB
testcase_18 AC 1,180 ms
33,852 KB
testcase_19 AC 1,214 ms
33,804 KB
testcase_20 AC 175 ms
29,980 KB
testcase_21 AC 1,117 ms
33,116 KB
testcase_22 AC 348 ms
30,692 KB
testcase_23 AC 320 ms
30,496 KB
testcase_24 AC 615 ms
31,380 KB
testcase_25 AC 1,196 ms
33,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
MOD = 1234567891

N, M, *A = map(int, read().split())

f = np.array([1], np.int64)
for x in A:
    g = np.zeros(len(f) + x, np.int64)
    g[: len(f)] += f
    g[x:] -= f
    f = g % MOD


def fft_convolve(f, g, MOD=MOD):
    """
    数列 (多項式) f, g の畳み込みの計算.上下 15 bitずつ分けて計算することで,
    30 bit以下の整数,長さ 250000 程度の数列での計算が正確に行える.
    """
    fft = np.fft.rfft
    ifft = np.fft.irfft
    Lf = len(f)
    Lg = len(g)
    L = Lf + Lg - 1
    fft_len = 1 << L.bit_length()
    fl = f & (1 << 15) - 1
    fh = f >> 15
    gl = g & (1 << 15) - 1
    gh = g >> 15

    def conv(f, g):
        return ifft(fft(f, fft_len) * fft(g, fft_len))[: L]
    x = conv(fl, gl) % MOD
    y = conv(fl + fh, gl + gh) % MOD
    z = conv(fh, gh) % MOD
    a, b, c = map(lambda x: (x + .5).astype(np.int64), [x, y, z])
    return (a + ((b - a - c) << 15) + (c << 30)) % MOD


def coef_of_generating_function(P, Q, N):
    """compute the coefficient [x^N] P/Q of rational power series.

    Parameters
    ----------
    P : np.ndarray
        numerator.
    Q : np.ndarray
        denominator
        Q[0] == 1 and len(Q) == len(P) + 1 is assumed.
    N : int
        The coefficient to compute.
    """
    def convolve(f, g):
        return fft_convolve(f, g, MOD)
    while N:
        Q1 = Q.copy()
        Q1[1:: 2] = np.negative(Q1[1:: 2])
        if N & 1:
            P = convolve(P, Q1)[1:: 2]
        else:
            P = convolve(P, Q1)[:: 2]
        Q = convolve(Q, Q1)[:: 2]
        N >>= 1
    return P[0]


P = np.zeros(len(f) - 1, np.int64)
P[0] = 1
answer = coef_of_generating_function(P, f, M)
print(answer)
0