結果

問題 No.1392 Don't be together
ユーザー chineristACchineristAC
提出日時 2021-01-24 14:08:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,404 ms / 2,000 ms
コード長 1,355 bytes
コンパイル時間 1,074 ms
コンパイル使用メモリ 86,820 KB
実行使用メモリ 175,652 KB
最終ジャッジ日時 2023-08-30 21:37:03
合計ジャッジ時間 16,564 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
80,508 KB
testcase_01 AC 85 ms
80,616 KB
testcase_02 AC 87 ms
80,836 KB
testcase_03 AC 86 ms
80,492 KB
testcase_04 AC 86 ms
80,452 KB
testcase_05 AC 85 ms
80,504 KB
testcase_06 AC 521 ms
116,384 KB
testcase_07 AC 834 ms
148,700 KB
testcase_08 AC 779 ms
134,132 KB
testcase_09 AC 1,404 ms
175,652 KB
testcase_10 AC 623 ms
120,944 KB
testcase_11 AC 814 ms
141,104 KB
testcase_12 AC 539 ms
118,428 KB
testcase_13 AC 697 ms
131,952 KB
testcase_14 AC 761 ms
138,324 KB
testcase_15 AC 559 ms
119,664 KB
testcase_16 AC 820 ms
146,496 KB
testcase_17 AC 606 ms
124,464 KB
testcase_18 AC 759 ms
136,240 KB
testcase_19 AC 689 ms
135,300 KB
testcase_20 AC 356 ms
94,468 KB
testcase_21 AC 216 ms
85,440 KB
testcase_22 AC 502 ms
110,488 KB
testcase_23 AC 376 ms
97,040 KB
testcase_24 AC 269 ms
90,812 KB
testcase_25 AC 401 ms
100,504 KB
testcase_26 AC 194 ms
84,652 KB
testcase_27 AC 239 ms
88,336 KB
testcase_28 AC 436 ms
99,936 KB
testcase_29 AC 288 ms
89,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def cmb(n, r, mod):#コンビネーションの高速計算 
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353 #出力の制限
N = 2*10**5
g1 = [1]*(N+1) # 元テーブル
g2 = [1]*(N+1) #逆元テーブル
inverse = [1]*(N+1) #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1[i]=( ( g1[i-1] * i ) % mod )
    inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2[i]=( (g2[i-1] * inverse[i]) % mod )
inverse[0]=0

N,M = map(int,input().split())
P = list(map(int,input().split()))

P = [P[i]-1 for i in range(N)]
cycle = []
used = [False]*N
for i in range(N):
    if not used[i]:
        used[i] = True
        c = 1
        pos = i
        while not used[P[pos]]:
            pos = P[pos]
            used[pos] = True
            c += 1
        cycle.append(c)

n = len(cycle)

res = 0
for k in range(2,M+1):
    dp = [[0,0] for i in range(N+1)]
    dp[0][0] = 1
    for i in range(1,N+1):
        dp[i][0] += dp[i-1][1]
        dp[i][1] += dp[i-1][0] * max(0,k-1) + dp[i-1][1] * max(0,k-2)
        dp[i][0] %= mod
        dp[i][1] %= mod
    tmp = 1
    for l in cycle:
        tmp *= dp[l][0] * k
        tmp %= mod
    if (M-k)%2:
        res -= cmb(M,k,mod) * tmp
    else:
        res += cmb(M,k,mod) * tmp
    res %= mod

print(res*g2[M] % mod)
0