結果

問題 No.1392 Don't be together
ユーザー 👑 tatyamtatyam
提出日時 2020-09-02 00:22:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 587 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 72,308 KB
最終ジャッジ日時 2024-05-01 01:34:29
合計ジャッジ時間 4,344 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
59,940 KB
testcase_01 AC 43 ms
59,744 KB
testcase_02 AC 43 ms
59,280 KB
testcase_03 AC 44 ms
58,620 KB
testcase_04 AC 45 ms
58,796 KB
testcase_05 AC 44 ms
59,720 KB
testcase_06 AC 312 ms
68,500 KB
testcase_07 AC 60 ms
66,696 KB
testcase_08 AC 235 ms
68,548 KB
testcase_09 AC 587 ms
69,456 KB
testcase_10 AC 66 ms
69,332 KB
testcase_11 AC 75 ms
71,228 KB
testcase_12 AC 66 ms
67,952 KB
testcase_13 AC 72 ms
71,436 KB
testcase_14 AC 75 ms
71,368 KB
testcase_15 AC 67 ms
68,356 KB
testcase_16 AC 77 ms
72,308 KB
testcase_17 AC 68 ms
71,140 KB
testcase_18 AC 71 ms
70,516 KB
testcase_19 AC 65 ms
68,420 KB
testcase_20 AC 66 ms
68,304 KB
testcase_21 AC 64 ms
67,272 KB
testcase_22 AC 66 ms
68,992 KB
testcase_23 AC 66 ms
68,292 KB
testcase_24 AC 63 ms
67,392 KB
testcase_25 AC 65 ms
68,188 KB
testcase_26 AC 62 ms
67,548 KB
testcase_27 AC 61 ms
65,916 KB
testcase_28 AC 70 ms
70,956 KB
testcase_29 AC 65 ms
66,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353
MAX = 5000
fac = [1] * (MAX + 1)
for i in range(MAX):
    fac[i + 1] = fac[i] * (i + 1) % MOD
inv = [1] * (MAX + 1)
inv[MAX] = pow(fac[MAX], MOD - 2, MOD)
for i in range(MAX, 0, -1):
    inv[i - 1] = inv[i] * i % MOD
def comb(n,r):
    return fac[n] * inv[r] * inv[n - r] % MOD

n, m = map(int, input().split(' '))
p = [*map(int, input().split(' '))]
assert 2 <= n <= 5000
assert 1 <= m <= n
assert len(p) == n
for i in range(n):
    assert 1 <= p[i] <= n
    p[i] -= 1
    assert p[i] != i

# 順列をサイクルに分解
cycle = []
used = [False] * n
for i in range(n):
    if used[i]:
        continue
    l = 0
    while not used[i]:
        used[i] = True
        l += 1
        i = p[i]
    cycle.append(l)

# 包除原理 a[i] := 1 ~ N を i 個の区別するグループに分ける分け方 整数が入っていないグループがあってもOK
a = [1] * (m + 1)
for i in range(2, m + 1):
    for l in cycle:
        a[i] *= pow(i-1, l, MOD) + (1 - i if l & 1 else i - 1)
        a[i] %= MOD

ans = 0
for i in range(2, m + 1):
    if (m ^ i) & 1:
        ans -= a[i] * comb(m, i)
    else:
        ans += a[i] * comb(m, i)
# 区別しないので M! で割る
ans *= inv[m]
print(ans % MOD)
0