結果

問題 No.1392 Don't be together
ユーザー shotoyooshotoyoo
提出日時 2021-02-12 22:59:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,081 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 86,236 KB
最終ジャッジ日時 2023-09-27 06:07:02
合計ジャッジ時間 8,814 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
71,440 KB
testcase_01 AC 66 ms
71,488 KB
testcase_02 AC 66 ms
71,416 KB
testcase_03 AC 63 ms
71,464 KB
testcase_04 AC 63 ms
71,620 KB
testcase_05 AC 68 ms
71,328 KB
testcase_06 AC 592 ms
78,548 KB
testcase_07 AC 226 ms
78,380 KB
testcase_08 AC 529 ms
80,488 KB
testcase_09 AC 1,081 ms
80,776 KB
testcase_10 AC 178 ms
80,616 KB
testcase_11 AC 241 ms
86,236 KB
testcase_12 AC 171 ms
80,680 KB
testcase_13 AC 212 ms
83,588 KB
testcase_14 AC 230 ms
84,084 KB
testcase_15 AC 170 ms
81,064 KB
testcase_16 AC 231 ms
84,028 KB
testcase_17 AC 178 ms
79,356 KB
testcase_18 AC 213 ms
81,608 KB
testcase_19 AC 197 ms
79,448 KB
testcase_20 AC 142 ms
81,644 KB
testcase_21 AC 110 ms
79,060 KB
testcase_22 AC 165 ms
81,664 KB
testcase_23 AC 142 ms
79,688 KB
testcase_24 AC 120 ms
78,876 KB
testcase_25 AC 147 ms
81,132 KB
testcase_26 AC 110 ms
78,788 KB
testcase_27 AC 118 ms
79,176 KB
testcase_28 AC 156 ms
79,956 KB
testcase_29 AC 129 ms
81,676 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)
0