結果

問題 No.2770 Coupon Optimization
ユーザー 👑 rin204rin204
提出日時 2024-05-31 22:08:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 864 ms / 3,000 ms
コード長 12,710 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 82,668 KB
実行使用メモリ 151,844 KB
最終ジャッジ日時 2024-12-20 23:39:55
合計ジャッジ時間 12,852 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

class NTT998:
# fmt: off
rate2=(0, 911660635, 509520358, 369330050, 332049552, 983190778, 123842337, 238493703, 975955924, 603855026, 856644456, 131300601, 842657263,
        730768835, 942482514, 806263778, 151565301, 510815449, 503497456, 743006876, 741047443, 56250497, 867605899, 0)
irate2=(0, 86583718, 372528824, 373294451, 645684063, 112220581, 692852209, 155456985, 797128860, 90816748, 860285882, 927414960, 354738543,
        109331171, 293255632, 535113200, 308540755, 121186627, 608385704, 438932459, 359477183, 824071951, 103369235, 0)
rate3=(0, 372528824, 337190230, 454590761, 816400692, 578227951, 180142363, 83780245, 6597683, 70046822, 623238099, 183021267, 402682409,
        631680428, 344509872, 689220186, 365017329, 774342554, 729444058, 102986190, 128751033, 395565204, 0)
irate3=(0, 509520358, 929031873, 170256584, 839780419, 282974284, 395914482, 444904435, 72135471, 638914820, 66769500, 771127074, 985925487,
        262319669, 262341272, 625870173, 768022760, 859816005, 914661783, 430819711, 272774365, 530924681, 0)
# fmt: on
def butterfly(A):
n = len(A)
h = (n - 1).bit_length()
le = 0
while le < h:
if h - le == 1:
p = 1 << (h - le - 1)
rot = 1
for s in range(1 << le):
offset = s << (h - le)
for i in range(p):
l = A[i + offset]
r = A[i + offset + p] * rot
A[i + offset] = (l + r) % 998244353
A[i + offset + p] = (l - r) % 998244353
rot *= NTT998.rate2[(~s & -~s).bit_length()]
rot %= 998244353
le += 1
else:
p = 1 << (h - le - 2)
rot = 1
for s in range(1 << le):
rot2 = rot * rot % 998244353
rot3 = rot2 * rot % 998244353
offset = s << (h - le)
for i in range(p):
a0 = A[i + offset]
a1 = A[i + offset + p] * rot
a2 = A[i + offset + p * 2] * rot2
a3 = A[i + offset + p * 3] * rot3
a1na3imag = (a1 - a3) % 998244353 * 911660635
A[i + offset] = (a0 + a2 + a1 + a3) % 998244353
A[i + offset + p] = (a0 + a2 - a1 - a3) % 998244353
A[i + offset + p * 2] = (a0 - a2 + a1na3imag) % 998244353
A[i + offset + p * 3] = (a0 - a2 - a1na3imag) % 998244353
rot *= NTT998.rate3[(~s & -~s).bit_length()]
rot %= 998244353
le += 2
def butterfly_inv(A):
n = len(A)
h = (n - 1).bit_length()
le = h
while le:
if le == 1:
p = 1 << (h - le)
irot = 1
for s in range(1 << (le - 1)):
offset = s << (h - le + 1)
for i in range(p):
l = A[i + offset]
r = A[i + offset + p]
A[i + offset] = (l + r) % 998244353
A[i + offset + p] = (l - r) * irot % 998244353
irot *= NTT998.irate2[(~s & -~s).bit_length()]
irot %= 998244353
le -= 1
else:
p = 1 << (h - le)
irot = 1
for s in range(1 << (le - 2)):
irot2 = irot * irot % 998244353
irot3 = irot2 * irot % 998244353
offset = s << (h - le + 2)
for i in range(p):
a0 = A[i + offset]
a1 = A[i + offset + p]
a2 = A[i + offset + p * 2]
a3 = A[i + offset + p * 3]
a2na3iimag = (a2 - a3) * 86583718 % 998244353
A[i + offset] = (a0 + a1 + a2 + a3) % 998244353
A[i + offset + p] = (a0 - a1 + a2na3iimag) * irot % 998244353
A[i + offset + p * 2] = (a0 + a1 - a2 - a3) * irot2 % 998244353
A[i + offset + p * 3] = (
(a0 - a1 - a2na3iimag) * irot3 % 998244353
)
irot *= NTT998.irate3[(~s & -~s).bit_length()]
irot %= 998244353
le -= 2
def multiply(A, B):
n = len(A)
m = len(B)
if min(n, m) <= 60:
C = [0] * (n + m - 1)
for i in range(n):
if i % 8 == 0:
for j in range(m):
C[i + j] += A[i] * B[j]
C[i + j] %= 998244353
else:
for j in range(m):
C[i + j] += A[i] * B[j]
return [c % 998244353 for c in C]
A = A[:]
B = B[:]
z = 1 << (n + m - 2).bit_length()
A += [0] * (z - n)
B += [0] * (z - m)
NTT998.butterfly(A)
NTT998.butterfly(B)
for i in range(z):
A[i] *= B[i]
A[i] %= 998244353
NTT998.butterfly_inv(A)
A = A[: n + m - 1]
iz = pow(z, 998244353 - 2, 998244353)
return [a * iz % 998244353 for a in A]
class NTT:
def __init__(self, MOD=998244353):
self.MOD = MOD
self.make_info(MOD)
def make_info(self, MOD):
g = self.primitive_root(MOD)
m = MOD - 1
rank2 = (m & -m).bit_length() - 1
root = [0] * (rank2 + 1)
iroot = [0] * (rank2 + 1)
rate2 = [0] * (rank2 + 1)
irate2 = [0] * (rank2 + 1)
rate3 = [0] * (rank2)
irate3 = [0] * (rank2)
root[rank2] = pow(g, (MOD - 1) >> rank2, MOD)
iroot[rank2] = pow(root[rank2], MOD - 2, MOD)
for i in range(rank2 - 1, -1, -1):
root[i] = root[i + 1] * root[i + 1] % MOD
iroot[i] = iroot[i + 1] * iroot[i + 1] % MOD
prod = 1
iprod = 1
for i in range(1, rank2):
rate2[i] = root[i + 1] * prod % MOD
irate2[i] = iroot[i + 1] * iprod % MOD
prod = prod * iroot[i + 1] % MOD
iprod = iprod * root[i + 1] % MOD
prod = 1
iprod = 1
for i in range(1, rank2 - 1):
rate3[i] = root[i + 2] * prod % MOD
irate3[i] = iroot[i + 2] * iprod % MOD
prod = prod * iroot[i + 2] % MOD
iprod = iprod * root[i + 2] % MOD
self.IMAG = rate2[1]
self.IIMAG = irate2[1]
self.rate2 = rate2
self.irate2 = irate2
self.rate3 = rate3
self.irate3 = irate3
def primitive_root(self, MOD):
if MOD == 998244353:
return 3
elif MOD == 200003:
return 2
elif MOD == 167772161:
return 3
elif MOD == 469762049:
return 3
elif MOD == 754974721:
return 11
divs = [0] * 20
divs[0] = 2
cnt = 1
x = (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, (MOD - 1) // divs[i], MOD) == 1:
ok = False
break
if ok:
return g
g += 1
def butterfly(self, A):
n = len(A)
h = (n - 1).bit_length()
le = 0
while le < h:
if h - le == 1:
p = 1 << (h - le - 1)
rot = 1
for s in range(1 << le):
offset = s << (h - le)
for i in range(p):
l = A[i + offset]
r = A[i + offset + p] * rot
A[i + offset] = (l + r) % self.MOD
A[i + offset + p] = (l - r) % self.MOD
rot *= self.rate2[(~s & -~s).bit_length()]
rot %= self.MOD
le += 1
else:
p = 1 << (h - le - 2)
rot = 1
for s in range(1 << le):
rot2 = rot * rot % self.MOD
rot3 = rot2 * rot % self.MOD
offset = s << (h - le)
for i in range(p):
a0 = A[i + offset]
a1 = A[i + offset + p] * rot
a2 = A[i + offset + p * 2] * rot2
a3 = A[i + offset + p * 3] * rot3
a1na3imag = (a1 - a3) % self.MOD * self.IMAG
A[i + offset] = (a0 + a2 + a1 + a3) % self.MOD
A[i + offset + p] = (a0 + a2 - a1 - a3) % self.MOD
A[i + offset + p * 2] = (a0 - a2 + a1na3imag) % self.MOD
A[i + offset + p * 3] = (a0 - a2 - a1na3imag) % self.MOD
rot *= self.rate3[(~s & -~s).bit_length()]
rot %= self.MOD
le += 2
def butterfly_inv(self, A):
n = len(A)
h = (n - 1).bit_length()
le = h
while le:
if le == 1:
p = 1 << (h - le)
irot = 1
for s in range(1 << (le - 1)):
offset = s << (h - le + 1)
for i in range(p):
l = A[i + offset]
r = A[i + offset + p]
A[i + offset] = (l + r) % self.MOD
A[i + offset + p] = (l - r) * irot % self.MOD
irot *= self.irate2[(~s & -~s).bit_length()]
irot %= self.MOD
le -= 1
else:
p = 1 << (h - le)
irot = 1
for s in range(1 << (le - 2)):
irot2 = irot * irot % self.MOD
irot3 = irot2 * irot % self.MOD
offset = s << (h - le + 2)
for i in range(p):
a0 = A[i + offset]
a1 = A[i + offset + p]
a2 = A[i + offset + p * 2]
a3 = A[i + offset + p * 3]
a2na3iimag = (a2 - a3) * self.IIMAG % self.MOD
A[i + offset] = (a0 + a1 + a2 + a3) % self.MOD
A[i + offset + p] = (a0 - a1 + a2na3iimag) * irot % self.MOD
A[i + offset + p * 2] = (a0 + a1 - a2 - a3) * irot2 % self.MOD
A[i + offset + p * 3] = (
(a0 - a1 - a2na3iimag) * irot3 % self.MOD
)
irot *= self.irate3[(~s & -~s).bit_length()]
irot %= self.MOD
le -= 2
def multiply(self, A, B):
n = len(A)
m = len(B)
if min(n, m) <= 60:
C = [0] * (n + m - 1)
for i in range(n):
if i % 8 == 0:
for j in range(m):
C[i + j] += A[i] * B[j]
C[i + j] %= self.MOD
else:
for j in range(m):
C[i + j] += A[i] * B[j]
return [c % self.MOD for c in C]
A = A[:]
B = B[:]
z = 1 << (n + m - 2).bit_length()
A += [0] * (z - n)
B += [0] * (z - m)
self.butterfly(A)
self.butterfly(B)
for i in range(z):
A[i] *= B[i]
A[i] %= self.MOD
self.butterfly_inv(A)
A = A[: n + m - 1]
iz = pow(z, self.MOD - 2, self.MOD)
return [a * iz % self.MOD for a in A]
MOD = 998244353
n, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort()
B.sort(reverse=True)
B += [0] * (n - m)
inv100 = pow(100, -1, MOD)
B2 = B[:]
for i in range(n):
B2[i] = (100 - B2[i]) * inv100 % MOD
C = NTT998.multiply(A, B2)
MOD2 = 924844033
B2 = B[:]
inv100 = pow(100, -1, MOD2)
for i in range(n):
B2[i] = (100 - B2[i]) * inv100 % MOD2
C2 = NTT(MOD2).multiply(A, B2)
ans = []
times = pow(MOD, -1, MOD2)
for c, c2 in zip(C[:n], C2[:n]):
x = (c2 - c) * times % MOD2
r = c + MOD * x
ans.append(r)
print(*ans, sep="\n")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0