結果
| 問題 |
No.1392 Don't be together
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-09-01 14:59:16 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,355 bytes |
| コンパイル時間 | 102 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 240,512 KB |
| 最終ジャッジ日時 | 2024-11-21 00:45:12 |
| 合計ジャッジ時間 | 56,341 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 3 WA * 9 TLE * 15 |
ソースコード
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)