結果
問題 | No.1392 Don't be together |
ユーザー | convexineq |
提出日時 | 2021-02-12 22:50:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 381 ms / 2,000 ms |
コード長 | 1,205 bytes |
コンパイル時間 | 859 ms |
コンパイル使用メモリ | 82,280 KB |
実行使用メモリ | 78,560 KB |
最終ジャッジ日時 | 2024-07-19 23:55:01 |
合計ジャッジ時間 | 7,804 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
MOD = 998244353 n,m = map(int,input().split()) *p, = map(int,input().split()) for i in range(n): p[i] -= 1 #from collections import Counter #d = Counter() 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) #d[c] += 1 dp = [0]*(m+1) dp[0] = 1 for c in res: #print(c,dp) 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 #print(dp,"now") dp2 = [0]*(m+1) for _ in range(c-2): 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 #print(dp,dp2) ndp = [0]*(m+1) for i in range(m+1): ndp[i] += (i-1)*dp[i] ndp[i] += (i-2)*dp2[i] ndp[i] %= MOD if i < m: ndp[i+1] += dp[i] + dp2[i] ndp[i+1] %= MOD dp = ndp print(dp[-1])