結果

問題 No.140 みんなで旅行
ユーザー tonyu0tonyu0
提出日時 2021-01-28 17:07:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 315 ms / 5,000 ms
コード長 1,285 bytes
コンパイル時間 1,346 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 79,748 KB
最終ジャッジ日時 2023-09-08 08:07:42
合計ジャッジ時間 4,520 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,176 KB
testcase_01 AC 73 ms
71,324 KB
testcase_02 AC 92 ms
76,624 KB
testcase_03 AC 74 ms
71,076 KB
testcase_04 AC 73 ms
71,168 KB
testcase_05 AC 73 ms
71,072 KB
testcase_06 AC 73 ms
71,276 KB
testcase_07 AC 73 ms
71,328 KB
testcase_08 AC 74 ms
71,432 KB
testcase_09 AC 70 ms
71,324 KB
testcase_10 AC 70 ms
71,300 KB
testcase_11 AC 313 ms
79,564 KB
testcase_12 AC 87 ms
76,620 KB
testcase_13 AC 87 ms
76,516 KB
testcase_14 AC 315 ms
79,532 KB
testcase_15 AC 311 ms
79,748 KB
testcase_16 AC 147 ms
76,360 KB
testcase_17 AC 110 ms
76,420 KB
testcase_18 AC 247 ms
79,056 KB
testcase_19 AC 269 ms
79,204 KB
testcase_20 AC 104 ms
76,648 KB
testcase_21 AC 84 ms
76,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
# ペアをひと固まりと考えると、各グループ、少なくとも一つ以上の塊が必要
# 夫婦で一緒になる組xを固定して、xのスターリング数を求める。
# 残った人は夫婦同氏同じグループにならないように割り振る。

mod = 10 ** 9 + 7
fac=[1]*(n+1)
finv=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2, n + 1):
    # p = i*p // i + p % i -> i*p//i+p%i=0 mod p
    # inv[i] = -i*p//i * inv[p%i]
    inv[i] = mod - mod // i * inv[mod % i] % mod
for i in range(1, n + 1):
    fac[i] = fac[i - 1] * i % mod
    finv[i] = finv[i - 1] * inv[i] % mod

def comb(n, k):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * finv[n - k] % mod * finv[k] % mod
dp = [[0] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(1, n + 1):
    for j in range(1, n + 1):
        dp[i][j] = j * dp[i - 1][j] % mod + dp[i - 1][j - 1]
        if dp[i][j] >= mod:
            dp[i][j] -= mod
ans = 0
for g in range(1, n + 1):
    for x in range(g, n + 1):
        y = n - x
        z = dp[x][g]
        tmp = 1
        for w in range(y):
            tmp *= g * (g - 1) % mod
            tmp %= mod
        ans += tmp * dp[x][g] % mod * comb(n, x) % mod
        if ans >= mod:
            ans -= mod
print(ans)
0