結果

問題 No.1392 Don't be together
ユーザー chineristACchineristAC
提出日時 2020-09-09 11:16:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 4,476 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 126,884 KB
最終ジャッジ日時 2024-05-08 11:03:45
合計ジャッジ時間 12,664 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
73,280 KB
testcase_01 AC 62 ms
67,760 KB
testcase_02 AC 89 ms
79,556 KB
testcase_03 AC 61 ms
66,908 KB
testcase_04 AC 83 ms
78,784 KB
testcase_05 AC 71 ms
72,496 KB
testcase_06 AC 516 ms
124,732 KB
testcase_07 AC 526 ms
126,628 KB
testcase_08 AC 533 ms
124,712 KB
testcase_09 AC 532 ms
124,972 KB
testcase_10 AC 514 ms
126,492 KB
testcase_11 AC 531 ms
126,620 KB
testcase_12 AC 511 ms
126,624 KB
testcase_13 AC 511 ms
126,624 KB
testcase_14 AC 531 ms
126,620 KB
testcase_15 AC 535 ms
126,616 KB
testcase_16 AC 516 ms
126,728 KB
testcase_17 AC 523 ms
126,616 KB
testcase_18 AC 529 ms
126,884 KB
testcase_19 AC 523 ms
126,504 KB
testcase_20 AC 316 ms
91,668 KB
testcase_21 AC 227 ms
83,780 KB
testcase_22 AC 536 ms
126,056 KB
testcase_23 AC 320 ms
91,192 KB
testcase_24 AC 326 ms
91,680 KB
testcase_25 AC 326 ms
91,820 KB
testcase_26 AC 223 ms
83,820 KB
testcase_27 AC 319 ms
91,552 KB
testcase_28 AC 317 ms
91,704 KB
testcase_29 AC 318 ms
89,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
omega = pow(3,119,mod)
rev_omega = pow(omega,mod-2,mod)

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

N = 2*10**5
g1 = [1]*(N+1) # 元テーブル
g2 = [1]*(N+1) #逆元テーブル
inv = [1]*(N+1) #逆元テーブル計算用テーブル

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

def _ntt(f,L,reverse=False):
    F=[f[i] for i in range(L)]
    n = L.bit_length() - 1
    base = omega
    if reverse:
        base = rev_omega

    if not n:
        return F

    size = 2**n
    wj = pow(base,2**22,mod)
    res = [0]*2**n

    for i in range(n,0,-1):
        use_omega = pow(base,2**(22+i-n),mod)
        res = [0]*2**n
        size //= 2
        w = 1
        for j in range(0,L//2,size):
            for a in range(size):
                res[a+j] = (F[a+2*j] + w * F[a+size+2*j]) % mod
                t = (w * wj) % mod
                res[L//2+a+j] = (F[a+2*j] + t * F[a+size+2*j]) % mod
            w = (w * use_omega) % mod
        F = res

    return res

def ntt(f,L=0):
    l = len(f)
    if not L:
        L = 1<<((l-1).bit_length())
    while len(f)<L:
        f.append(0)
    f=f[:L]
    F = _ntt(f,L)
    return F

def intt(f,L=0):
    l = len(f)
    if not L:
        L = 1<<((l-1).bit_length())
    while len(f)<L:
        f.append(0)
    f=f[:L]
    F = _ntt(f,L,reverse=True)
    inv = pow(L,mod-2,mod)
    for i in range(L):
        F[i] *= inv
        F[i] %= mod
    return F

def convolve(f,g,limit):
    l = len(f)+len(g)-1
    L = 1<<((l-1).bit_length())

    F = ntt(f,L)
    G = ntt(g,L)

    H = [(F[i] * G[i]) % mod for i in range(L)]

    h = intt(H,L)

    return h[:limit]

def inverse(f,limit):
    assert(f[0]!=0)
    l = len(f)
    L = 1<<((l-1).bit_length())
    n = L.bit_length()-1
    f = f[:L]
    f+=[0]*(L-len(f))

    res = [pow(f[0],mod-2,mod)]
    for i in range(1,n+1):
        h = convolve(res,f[:2**i],2**i)
        h = [(-h[i]) % mod for i in range(2**i)]
        h[0] = (h[0]+2) % mod
        res = convolve(res,h,2**i)
    return res[:limit]

def integral(f,limit):
    res = [0]+[(f[i] * inv[i+1]) % mod for i in range(len(f)-1)]
    return res[:limit]

def diff(f,limit):
    res = [(f[i+1] * (i+1)) % mod for i in range(len(f)-1)]+[0]
    return res[:limit]

def log(f,limit):
    res = convolve(diff(f,limit),inverse(f,limit),limit)
    return integral(res,limit)

def exp(f,limit):
    l = len(f)
    L = 1<<((l-1).bit_length())
    n = L.bit_length()-1
    f = f[:L]
    f+=[0]*(L-len(f))

    res = [1]
    for i in range(1,n+1):
        res += [0]*2**(i-1)
        g = log(res,2**i)
        h = [(f[j]-g[j])%mod for j in range(2**i)]
        h[0] = (h[0]+1) % mod
        res =convolve(res,h,2**i)
    return res[:limit]

def poly_pow_exp(f,k,limit):
    l = len(f)
    L = 1<<((l-1).bit_length())
    n = L.bit_length()-1
    f = f[:L]
    f+=[0]*(L-len(f))

    g = log(f,limit)
    g = [(k * g[i]) % mod for i in range(len(g))]
    h = exp(g,limit)
    return h[:limit]

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)

a = [0]*(N+1)
for c in cycle:
    a[c-1] += 1

f = [0 for i in range(N+1)]
for i in range(1,N+1):
    for j in range(1,(N//i)+1):
        f[i*j] += - a[i] * inv[j]
        f[i*j] %= mod

f = exp(f,N+1)

nf = [0 for i in range(N+1)]
sign = pow(-1,n)
for i in range(N+1-n):
    nf[i+n] = f[i] * sign
    nf[i+n] %= mod

f = nf

g = [(f[N-i] * g1[N-i]) % mod for i in range(N+1)]
e_x = [g2[i] for i in range(N+1)]

g_e_x = convolve(g,e_x,N+1)

f = [(g_e_x[N-i] * g2[i]) % mod for i in range(N+1)]

for i in range(1,N+1,2):
    f[i] = (-f[i]) % mod

f = [f[i+n] for i in range(N-n+1)]

g = [g2[i+1] for i in range(N+1)]
g = poly_pow_exp(g,M,N+1)

poly_Stirling = [0 for i in range(N+1)]
for i in range(N-M+1):
    poly_Stirling[i+M] = (g[i] * g1[i+M]) % mod
    poly_Stirling[i+M] = (g2[M] * poly_Stirling[i+M]) % mod

ans = 0
for j in range(N-n+1):
    ans += poly_Stirling[n+j] * f[j]
    ans %= mod
print((ans*(-1)**N)%mod)
0