結果

問題 No.2770 Coupon Optimization
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-11-30 11:28:31
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,418 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 144,612 KB
最終ジャッジ日時 2024-02-29 22:21:19
合計ジャッジ時間 1,078 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 39 ms
53,460 KB
testcase_02 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

ROOT1 = 3
MOD1 = 998244353
roots1  = [pow(ROOT1,(MOD1-1)>>i,MOD1) for i in range(30)] # 1 の 2^i 乗根
iroots1 = [pow(x,MOD1-2,MOD1) for x in roots1] # 1 の 2^i 乗根の逆元

ROOT2 = 5
MOD2 = 924844033
roots2  = [pow(ROOT2,(MOD2-1)>>i,MOD2) for i in range(30)] # 1 の 2^i 乗根
iroots2 = [pow(x,MOD2-2,MOD2) for x in roots2] # 1 の 2^i 乗根の逆元

def untt(a,n, MOD, roots):
    for i in range(n):
        m = 1<<(n-i-1)
        for s in range(1<<i):
            w_N = 1
            s *= m*2
            for p in range(m):
                a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m])%MOD, (a[s+p]-a[s+p+m])*w_N%MOD
                w_N = w_N*roots[n-i]%MOD

def iuntt(a,n, MOD, iroots):
    for i in range(n):
        m = 1<<i
        for s in range(1<<(n-i-1)):
            w_N = 1
            s *= m*2
            for p in range(m):
                a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m]*w_N)%MOD, (a[s+p]-a[s+p+m]*w_N)%MOD
                w_N = w_N*iroots[i+1]%MOD
            
    inv = pow((MOD+1)//2,n,MOD)
    for i in range(1<<n):
        a[i] = a[i]*inv%MOD

def convolution(a,b, MOD, roots, iroots):
    la = len(a)
    lb = len(b)
    if min(la, lb) <= 50:
        if la < lb:
            la,lb = lb,la
            a,b = b,a
        res = [0]*(la+lb-1)
        for i in range(la):
            for j in range(lb):
                res[i+j] += a[i]*b[j]
                res[i+j] %= MOD
        return res
        
    deg = la+lb-2
    n = deg.bit_length()
    N = 1<<n
    a += [0]*(N-len(a))
    b += [0]*(N-len(b))
    untt(a,n, roots)
    untt(b,n, roots)
    for i in range(N):
      a[i] = a[i]*b[i]%MOD
    iuntt(a,n, iroots)
    return a[:deg+1]
    
def extgcd(a, b):
    if b:
        d, y, x = extgcd(b, a % b)
        y -= (a // b) * x
        return d, x, y
    return a, 1, 0

# V = [(X_i, Y_i), ...]: X_i (mod Y_i)
def remainder(V):
    x = 0; d = 1
    for X, Y in V:
        g, a, b = extgcd(d, Y)
        x, d = (Y*b*x + d*a*X) // g, d*(Y // g)
        x %= d
    return x, d
        
N, M = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
for i in range(N):
  A[i] //= 100
for i in range(M):
  B[i] = 100-B[i]
  
for i in range(max(0, N-M)):
  B.append(100)
A.sort()
B.sort()

C = convolution(A,B, MOD1, roots1, iroots1)
D = convolution(A,B, MOD2, roots2, iroots2)
for i in range(N):
  x, d = remainder([[C[i], MOD1],[D[i], MOD2]])
  print(x)
0