結果

問題 No.1392 Don't be together
ユーザー convexineqconvexineq
提出日時 2021-02-13 14:38:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 874 bytes
コンパイル時間 1,521 ms
コンパイル使用メモリ 86,516 KB
実行使用メモリ 80,096 KB
最終ジャッジ日時 2023-09-27 23:04:20
合計ジャッジ時間 8,813 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
71,100 KB
testcase_01 AC 61 ms
71,432 KB
testcase_02 AC 63 ms
71,312 KB
testcase_03 AC 62 ms
71,296 KB
testcase_04 AC 62 ms
71,216 KB
testcase_05 AC 60 ms
71,252 KB
testcase_06 AC 193 ms
77,788 KB
testcase_07 AC 345 ms
79,008 KB
testcase_08 AC 307 ms
78,888 KB
testcase_09 AC 349 ms
80,096 KB
testcase_10 AC 238 ms
77,764 KB
testcase_11 AC 324 ms
78,516 KB
testcase_12 AC 228 ms
77,552 KB
testcase_13 AC 289 ms
78,352 KB
testcase_14 AC 313 ms
78,560 KB
testcase_15 AC 238 ms
77,916 KB
testcase_16 AC 332 ms
78,640 KB
testcase_17 AC 249 ms
77,876 KB
testcase_18 AC 312 ms
78,308 KB
testcase_19 AC 282 ms
78,140 KB
testcase_20 AC 167 ms
77,608 KB
testcase_21 AC 113 ms
77,032 KB
testcase_22 AC 227 ms
77,564 KB
testcase_23 AC 181 ms
77,712 KB
testcase_24 AC 139 ms
77,212 KB
testcase_25 AC 188 ms
77,564 KB
testcase_26 AC 109 ms
77,448 KB
testcase_27 AC 127 ms
77,220 KB
testcase_28 AC 195 ms
77,604 KB
testcase_29 AC 140 ms
77,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353
n,m = map(int,input().split())
*p, = map(int,input().split())
for i in range(n):
    p[i] -= 1

res = []
for i in range(n):
    if p[i] == -1: continue
    c = 0
    while p[i] != -1:
        ni = p[i]
        p[i] = -1
        i = ni
        c += 1
    res.append(c)

dp = [0]*(m+1)
dp[0] = 1
for c in res:
    ndp = [0]*(m+1)
    for i in range(m+1):
        ndp[i] += i*dp[i]
        ndp[i] %= MOD
        if i < m: ndp[i+1] += dp[i]

    dp = ndp
    dp2 = [0]*(m+1)
    for _ in range(c-1):
        ndp = [0]*(m+1)
        ndp2 = [0]*(m+1)
        for i in range(1,m+1):
            ndp2[i] += dp[i]*(i-1)
            ndp2[i] += dp2[i]*(i-2)
            ndp2[i] %= MOD
            ndp[i] += dp2[i]
            ndp[i] %= MOD
            if i < m:
                ndp2[i+1] += dp[i] + dp2[i]
        dp = ndp
        dp2 = ndp2
    dp = dp2

print(dp[-1])
0