結果

問題 No.140 みんなで旅行
ユーザー mkawa2mkawa2
提出日時 2020-01-21 20:49:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 913 ms / 5,000 ms
コード長 2,040 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 11,140 KB
実行使用メモリ 43,716 KB
最終ジャッジ日時 2023-09-20 09:43:28
合計ジャッジ時間 6,429 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,376 KB
testcase_01 AC 17 ms
8,556 KB
testcase_02 AC 55 ms
10,272 KB
testcase_03 AC 17 ms
8,372 KB
testcase_04 AC 17 ms
8,400 KB
testcase_05 AC 17 ms
8,208 KB
testcase_06 AC 17 ms
8,504 KB
testcase_07 AC 17 ms
8,556 KB
testcase_08 AC 18 ms
8,436 KB
testcase_09 AC 17 ms
8,516 KB
testcase_10 AC 17 ms
8,500 KB
testcase_11 AC 907 ms
43,716 KB
testcase_12 AC 42 ms
9,676 KB
testcase_13 AC 25 ms
8,788 KB
testcase_14 AC 892 ms
43,680 KB
testcase_15 AC 913 ms
43,472 KB
testcase_16 AC 334 ms
22,404 KB
testcase_17 AC 156 ms
15,004 KB
testcase_18 AC 684 ms
36,684 KB
testcase_19 AC 768 ms
39,124 KB
testcase_20 AC 126 ms
13,116 KB
testcase_21 AC 20 ms
8,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class mint:
    def __init__(self, x):
        self.__x = x % md

    def __str__(self):
        return str(self.__x)

    def __add__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x + other)

    def __sub__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x - other)

    def __rsub__(self, other):
        return mint(other - self.__x)

    def __mul__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * other)

    __radd__ = __add__
    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * pow(other, md - 2, md))

    def __pow__(self, power, modulo=None):
        return mint(pow(self.__x, power, md))

def nCr(com_n, com_r):
    if com_n < com_r: return 0
    return fac[com_n] / fac[com_r] / fac[com_n - com_r]

memo = {}
def s(n, r):
    if n < r: return 0
    if n == r or r == 1: return mint(1)
    if (n, r) in memo: return memo[n, r]
    memo[n, r] = res = s(n - 1, r - 1) + r * s(n - 1, r)
    return res

# combinationの準備
md = 10 ** 9 + 7
n_max = 555
fac = [mint(1)]
for i in range(1, n_max + 1): fac.append(fac[-1] * i)

def main():
    n = int(input())
    # 1グループになるのは1通りと分かるので2グループ以上に分かれる場合を考える
    ans = mint(1)
    # 夫婦が同じ車に乗るa組と別の車に乗るb組を決める
    for a in range(2, n + 1):
        b = n - a
        # n組からa組選ぶのがnCa通り
        c = nCr(n, a)
        # a,bを固定したら、gグループ作るときの場合の数を求める
        for g in range(2, a + 1):
            # a組の夫婦をgグループに分ける方法がs(a,g)通り
            # bの1組の夫婦の別れ方がg*(g-1)通り
            if b:
                ans += c * s(a, g) * pow(g * (g - 1), b, md)
            else:
                ans += c * s(a, g)
    print(ans)

main()
0