結果

問題 No.140 みんなで旅行
ユーザー rlangevinrlangevin
提出日時 2023-09-28 12:22:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 172 ms / 5,000 ms
コード長 772 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 86,800 KB
実行使用メモリ 85,156 KB
最終ジャッジ日時 2023-09-28 12:22:06
合計ジャッジ時間 4,392 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
76,452 KB
testcase_01 AC 87 ms
76,448 KB
testcase_02 AC 110 ms
84,920 KB
testcase_03 AC 87 ms
76,552 KB
testcase_04 AC 88 ms
76,172 KB
testcase_05 AC 85 ms
76,376 KB
testcase_06 AC 88 ms
76,448 KB
testcase_07 AC 90 ms
76,348 KB
testcase_08 AC 87 ms
76,448 KB
testcase_09 AC 92 ms
76,288 KB
testcase_10 AC 87 ms
76,584 KB
testcase_11 AC 170 ms
84,996 KB
testcase_12 AC 108 ms
85,124 KB
testcase_13 AC 93 ms
76,464 KB
testcase_14 AC 170 ms
85,156 KB
testcase_15 AC 172 ms
84,728 KB
testcase_16 AC 129 ms
84,872 KB
testcase_17 AC 120 ms
84,936 KB
testcase_18 AC 155 ms
84,964 KB
testcase_19 AC 166 ms
84,948 KB
testcase_20 AC 116 ms
84,936 KB
testcase_21 AC 90 ms
76,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

M = 1000
mod = 10 ** 9 + 7
S = [[0] * M for _ in range(M)]
for i in range(1, M):
    S[i][1] = 1
    S[i][i] = 1

for n in range(3, M):
    for k in range(n):
        S[n][k] = S[n - 1][k - 1] + k * S[n - 1][k]
        S[n][k] %= mod

n = M
fact = [1] * (n + 1)
invfact = [1] * (n + 1)
for i in range(1, n):
    fact[i + 1] = ((i+1) * fact[i]) % mod
invfact[n] = pow(fact[n], mod - 2, mod)
for i in range(n - 1, -1, -1):
    invfact[i] = invfact[i + 1] * (i + 1) % mod

def comb(n, r):
    if n < 0 or r < 0 or n - r < 0:
        return 0
    return fact[n] * invfact[r] * invfact[n - r] % mod


N = int(input())
ans = 1
for g in range(1, N):
    for n in range(g, N + 1):
        ans += comb(N, n) * S[n][g] * pow(g * (g - 1), (N - n), mod)
        ans %= mod

print(ans)
0