結果

問題 No.140 みんなで旅行
ユーザー shotoyooshotoyoo
提出日時 2020-11-21 00:10:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 496 ms / 5,000 ms
コード長 1,246 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 122,612 KB
最終ジャッジ日時 2023-09-30 20:19:56
合計ジャッジ時間 5,651 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,216 KB
testcase_01 AC 73 ms
71,388 KB
testcase_02 AC 115 ms
78,036 KB
testcase_03 AC 75 ms
71,196 KB
testcase_04 AC 74 ms
71,416 KB
testcase_05 AC 75 ms
71,408 KB
testcase_06 AC 76 ms
71,380 KB
testcase_07 AC 76 ms
71,272 KB
testcase_08 AC 77 ms
71,288 KB
testcase_09 AC 74 ms
71,300 KB
testcase_10 AC 75 ms
71,280 KB
testcase_11 AC 496 ms
122,424 KB
testcase_12 AC 107 ms
77,600 KB
testcase_13 AC 93 ms
76,352 KB
testcase_14 AC 494 ms
122,612 KB
testcase_15 AC 493 ms
122,352 KB
testcase_16 AC 225 ms
90,184 KB
testcase_17 AC 151 ms
82,008 KB
testcase_18 AC 376 ms
108,688 KB
testcase_19 AC 419 ms
113,700 KB
testcase_20 AC 135 ms
81,260 KB
testcase_21 AC 81 ms
75,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

n = int(input())
i = 0
dp = [[0]*(2*i+1) for _ in range(i+1)]
dp[0][0] = 1
M = 10**9+7
### 素数の逆元とCombination
N = 2*n+10 # 必要なテーブルサイズ
g1 = [None] * (N+1) # 元テーブル
g2 = [None] * (N+1) #逆元テーブル
inverse = [None] * (N+1) #逆元テーブル計算用テーブル
g1[0] = g1[1] = g2[0] = g2[1] = 1
inverse[0], inverse[1] = [0, 1] 
for i in range( 2, N + 1 ):
    g1[i] = ( g1[i-1] * i ) % M 
    inverse[i] = ( -inverse[M % i] * (M//i) ) % M # ai+b==0 mod M <=> i==-b*a^(-1) <=> i^(-1)==-b^(-1)*aより
    g2[i] = (g2[i-1] * inverse[i]) % M 
def cmb(n, r, M):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return (g1[n] * g2[r] * g2[n-r]) % M
def perm(n, r, M):
    if (r<0 or r>n):
        return 0
    return (g1[n] * g2[n-r]) % M

ans = 0
s = [[0]*(n+1) for _ in range(n+1)]
s[0][0] = 1
for i in range(1,n+1):
    for j in range(1,n+1):
        s[i][j] = s[i-1][j-1] + j*s[i-1][j]
for k in range(1, n+1):
    for i in range(k,n+1):
        ans += s[i][k]*pow(perm(k,2,M), n-i, M)*cmb(n,i,M)
        ans %= M
#     print(ans)
print(ans%M)
0