結果

問題 No.1392 Don't be together
ユーザー shotoyooshotoyoo
提出日時 2021-02-12 22:59:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,028 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 85,132 KB
最終ジャッジ日時 2024-07-20 00:19:17
合計ジャッジ時間 6,963 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,608 KB
testcase_01 AC 42 ms
52,608 KB
testcase_02 AC 38 ms
52,480 KB
testcase_03 AC 39 ms
52,480 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 38 ms
52,480 KB
testcase_06 AC 626 ms
77,440 KB
testcase_07 AC 211 ms
77,448 KB
testcase_08 AC 546 ms
79,104 KB
testcase_09 AC 1,028 ms
79,360 KB
testcase_10 AC 164 ms
79,752 KB
testcase_11 AC 224 ms
85,132 KB
testcase_12 AC 157 ms
79,432 KB
testcase_13 AC 193 ms
82,048 KB
testcase_14 AC 201 ms
82,560 KB
testcase_15 AC 160 ms
79,952 KB
testcase_16 AC 218 ms
83,428 KB
testcase_17 AC 160 ms
78,208 KB
testcase_18 AC 196 ms
80,380 KB
testcase_19 AC 178 ms
78,716 KB
testcase_20 AC 127 ms
80,620 KB
testcase_21 AC 94 ms
77,964 KB
testcase_22 AC 158 ms
80,424 KB
testcase_23 AC 125 ms
78,464 KB
testcase_24 AC 101 ms
77,432 KB
testcase_25 AC 131 ms
79,872 KB
testcase_26 AC 92 ms
77,312 KB
testcase_27 AC 96 ms
78,000 KB
testcase_28 AC 140 ms
78,592 KB
testcase_29 AC 112 ms
80,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
n,m = list(map(int, input().split()))
p = list(map(lambda x: int(x)-1, input().split()))
M = 998244353
N = n+m+100 #
g1 = [0] * (N+1) #
g2 = [0] * (N+1) #
inverse = [0] * (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] % M) * g2[n-r]) % M
def perm(n, r, M):
if (r<0 or r>n):
return 0
return (g1[n] * g2[n-r]) % M
def part(p):
n = len(p)
done = [0]*n
ans = []
for i in range(n):
if done[i]:
continue
l = [i]
i0 = i
done[i] = 1
while p[i]!=i0:
i = p[i]
l.append(i)
done[i] = 1
ans.append(l)
return ans
d = {}
def sub(i,j):
if (i,j) in d:
return d[i,j]
dp0 = dp1 = 0
dp0 = j
for ii in range(i-1):
dp0, dp1 = dp1, dp1*(j-2) + dp0*(j-1)
dp0 %= M
dp1 %= M
ans = dp1
if i==1:
ans = j
d[i,j] = ans
return ans
l = part(p)
nums = [len(item) for item in l]
ans = 0
for i in range(m):
s = 1 if i%2==0 else -1
val = s
for num in nums:
val *= sub(num, m-i)
val %= M
ans += val * cmb(m, i, M)
# ans += val * pow(m-i, len(l), M)
# print(i,val%M,s)
ans *= g2[m]
print(ans%M)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0