結果

問題 No.1392 Don't be together
ユーザー 👑 tatyamtatyam
提出日時 2020-09-02 00:15:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,103 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 18,720 KB
最終ジャッジ日時 2024-05-01 01:33:43
合計ジャッジ時間 3,759 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
18,332 KB
testcase_01 AC 29 ms
11,264 KB
testcase_02 AC 29 ms
11,264 KB
testcase_03 AC 30 ms
11,264 KB
testcase_04 AC 28 ms
11,264 KB
testcase_05 AC 28 ms
11,264 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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

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):
    ans += a[i] * fac[m] * inv[i] * inv[m - i] * (-1 if (m ^ i) & 1 else 1)
ans *= inv[m]
print(ans % MOD)
0