結果

問題 No.1621 Sequence Inversions
ユーザー 👑 rin204rin204
提出日時 2022-04-15 15:08:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 238 ms / 3,000 ms
コード長 3,532 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 81,788 KB
最終ジャッジ日時 2023-08-26 05:39:24
合計ジャッジ時間 7,196 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,284 KB
testcase_01 AC 91 ms
71,652 KB
testcase_02 AC 127 ms
77,532 KB
testcase_03 AC 90 ms
71,492 KB
testcase_04 AC 94 ms
71,296 KB
testcase_05 AC 92 ms
71,748 KB
testcase_06 AC 105 ms
77,600 KB
testcase_07 AC 119 ms
77,396 KB
testcase_08 AC 238 ms
81,176 KB
testcase_09 AC 204 ms
81,788 KB
testcase_10 AC 204 ms
81,560 KB
testcase_11 AC 238 ms
81,020 KB
testcase_12 AC 193 ms
79,620 KB
testcase_13 AC 162 ms
78,700 KB
testcase_14 AC 156 ms
78,132 KB
testcase_15 AC 153 ms
78,268 KB
testcase_16 AC 155 ms
77,916 KB
testcase_17 AC 161 ms
78,428 KB
testcase_18 AC 154 ms
78,360 KB
testcase_19 AC 125 ms
78,352 KB
testcase_20 AC 147 ms
78,252 KB
testcase_21 AC 164 ms
78,408 KB
testcase_22 AC 157 ms
78,236 KB
testcase_23 AC 161 ms
78,220 KB
testcase_24 AC 93 ms
72,232 KB
testcase_25 AC 95 ms
72,160 KB
testcase_26 AC 92 ms
71,604 KB
testcase_27 AC 93 ms
71,292 KB
testcase_28 AC 91 ms
71,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
MOD = 998244353

class FFT:
    def __init__(self, MOD=998244353):
        FFT.MOD = MOD
        g = self.primitive_root_constexpr()
        ig = pow(g, FFT.MOD - 2, FFT.MOD)
        FFT.W = [pow(g, (FFT.MOD - 1) >> i, FFT.MOD) for i in range(30)]
        FFT.iW = [pow(ig, (FFT.MOD - 1) >> i, FFT.MOD) for i in range(30)]

    def primitive_root_constexpr(self):
        if FFT.MOD == 998244353:
            return 3
        elif FFT.MOD == 200003:
            return 2
        elif FFT.MOD == 167772161:
            return 3
        elif FFT.MOD == 469762049:
            return 3
        elif FFT.MOD == 754974721:
            return 11
        divs = [0] * 20
        divs[0] = 2
        cnt = 1
        x = (FFT.MOD - 1) // 2
        while x % 2 == 0:
            x //= 2
        i = 3
        while i * i <= x:
            if x % i == 0:
                divs[cnt] = i
                cnt += 1
                while x % i == 0:
                    x //= i
            i += 2
        if x > 1:
            divs[cnt] = x
            cnt += 1
        g = 2
        while 1:
            ok = True
            for i in range(cnt):
                if pow(g, (FFT.MOD - 1) // divs[i], FFT.MOD) == 1:
                    ok = False
                    break
            if ok:
                return g
            g += 1

    def fft(self, k, f):
        for l in range(k, 0, -1):
            d = 1 << l - 1
            U = [1]
            for i in range(d):
                U.append(U[-1] * FFT.W[l] % FFT.MOD)
            
            for i in range(1 << k - l):
                for j in range(d):
                    s = i * 2 * d + j
                    f[s], f[s + d] = (f[s] + f[s + d]) % FFT.MOD, U[j] * (f[s] - f[s + d]) % FFT.MOD

    def ifft(self, k, f):
        for l in range(1, k + 1):
            d = 1 << l - 1
            for i in range(1 << k - l):
                u = 1
                for j in range(i * 2 * d, (i * 2 + 1) * d):
                    f[j+d] *= u
                    f[j], f[j + d] = (f[j] + f[j + d]) % FFT.MOD, (f[j] - f[j + d]) % FFT.MOD
                    u = u * FFT.iW[l] % FFT.MOD

    def convolve(self, A, B):
        n0 = len(A) + len(B) - 1
        k = (n0).bit_length()
        n = 1 << k
        A += [0] * (n - len(A))
        B += [0] * (n - len(B))
        self.fft(k, A)
        self.fft(k, B)
        A = [a * b % FFT.MOD for a, b in zip(A, B)]
        self.ifft(k, A)
        inv = pow(n, FFT.MOD - 2, FFT.MOD)
        A = [a * inv % FFT.MOD for a in A]
        del A[n0:]
        return A

n, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = {}
for a in A:
    cnt[a] = cnt.get(a, 0) + 1

queue = deque()
tot = 0
for k in sorted(set(A)):
    v = cnt[k]
    dp = [[0] * (tot + 1)]
    dp[0][0] = 1
    l = 1
    for _ in range(v):
        ndp = [[0] * (tot + 1) for _ in range(l + tot)]
        for i in range(l):
            for j in range(tot + 1):
                ndp[i + j][j] += dp[i][j]
                ndp[i + j][j] %= MOD
        l += tot
        for i in range(1, l):
            for j in range(1, tot + 1):
                ndp[i][j] += ndp[i - 1][j - 1]
                ndp[i][j] %= MOD
        dp = ndp
    tot += v
    poly = [sum(ndp[i]) for i in range(l)]
    del poly[K+1:]
    queue.append(poly)

fft = FFT()
while len(queue) > 1:
    A = queue.popleft()
    B = queue.popleft()
    C = fft.convolve(A, B)
    del C[K+1:]
    queue.append(C)
A = queue[0]
if len(A) <= K:
    print(0)
else:
    print(A[K])
    

0