結果

問題 No.1667 Forest
ユーザー 👑 tamatotamato
提出日時 2021-09-03 22:27:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,705 ms / 3,000 ms
コード長 1,200 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 82,212 KB
最終ジャッジ日時 2023-08-21 23:05:56
合計ジャッジ時間 19,370 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,705 ms
82,212 KB
testcase_01 AC 2,626 ms
81,992 KB
testcase_02 AC 2,586 ms
82,104 KB
testcase_03 AC 120 ms
80,824 KB
testcase_04 AC 2,544 ms
82,140 KB
testcase_05 AC 1,521 ms
81,928 KB
testcase_06 AC 946 ms
81,992 KB
testcase_07 AC 606 ms
81,568 KB
testcase_08 AC 334 ms
81,452 KB
testcase_09 AC 222 ms
81,188 KB
testcase_10 AC 165 ms
81,312 KB
testcase_11 AC 108 ms
80,916 KB
testcase_12 AC 91 ms
80,052 KB
testcase_13 AC 90 ms
80,464 KB
testcase_14 AC 90 ms
80,472 KB
testcase_15 AC 87 ms
80,496 KB
testcase_16 AC 88 ms
80,536 KB
testcase_17 AC 88 ms
80,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


def main():
    import sys
    input = sys.stdin.readline

    def f(x, mod):
        if x == 1:
            return 1
        return pow(x, x-2, mod)

    N, mod = map(int, input().split())

    # comb init
    # mod = 1000000007
    nmax = 2 * 10 ** 5 + 10  # change here
    fac = [0] * nmax
    finv = [0] * nmax
    inv = [0] * nmax
    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1
    for i in range(2, nmax):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

    def comb(n, r):
        if n < r:
            return 0
        else:
            return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod

    dp = [[0] * N for _ in range(N+1)]
    dp[N][0] = 1
    for i in range(N, 0, -1):
        for j in range(N):
            for j_use in range(1, N):
                if j + j_use < N and i >= j_use + 1:
                    dp[i-(j_use + 1)][j + j_use] = (dp[i-(j_use + 1)][j + j_use] + ((dp[i][j] * f(j_use + 1, mod))%mod * comb(i-1, j_use))%mod)%mod
            dp[i-1][j] = (dp[i-1][j] + dp[i][j])%mod

    print(*dp[0], sep="\n")


if __name__ == '__main__':
    main()
0