結果

問題 No.1392 Don't be together
ユーザー chineristACchineristAC
提出日時 2020-09-01 14:59:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,355 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 240,796 KB
最終ジャッジ日時 2024-05-01 01:32:26
合計ジャッジ時間 49,888 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 447 ms
45,024 KB
testcase_01 AC 444 ms
45,016 KB
testcase_02 AC 450 ms
44,888 KB
testcase_03 AC 449 ms
44,904 KB
testcase_04 AC 448 ms
44,764 KB
testcase_05 AC 447 ms
44,900 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = 5000
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

import numpy as np

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)

binom_poly = np.array([1],np.int64)
for i in range(n):
    c = [cmb(cycle[i],j,mod)*pow(-1,j,mod) for j in range(1,cycle[i]+1)]
    c[0] = (c[0] + 1)
    c = np.array(c,np.int64) % mod
    binom_poly = np.convolve(binom_poly,c) % mod

Stirling = np.zeros((N+1,N+1),np.int64)
Stirling[:,1] = 1
for i in range(2,N+1):
    prod = np.array([j+2 for j in range(i-1)],np.int64)
    Stirling[i,2:i+1] = Stirling[i-1,1:i] + prod * Stirling[i-1,2:i+1]
    Stirling[i] %= mod


ans = 0
for i in range(N-n+1):
    ans += Stirling[n+i,M] * binom_poly[i]
    ans %= mod

ans *= (-1)**N
ans %= mod

print(ans)
0