結果
問題 | No.931 Multiplicative Convolution |
ユーザー | neterukun |
提出日時 | 2021-09-22 04:53:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 261 ms / 2,000 ms |
コード長 | 2,334 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 82,160 KB |
実行使用メモリ | 114,036 KB |
最終ジャッジ日時 | 2024-07-04 15:08:21 |
合計ジャッジ時間 | 4,651 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,432 KB |
testcase_01 | AC | 40 ms
53,828 KB |
testcase_02 | AC | 44 ms
54,348 KB |
testcase_03 | AC | 42 ms
53,124 KB |
testcase_04 | AC | 38 ms
52,944 KB |
testcase_05 | AC | 45 ms
61,316 KB |
testcase_06 | AC | 68 ms
71,900 KB |
testcase_07 | AC | 89 ms
79,144 KB |
testcase_08 | AC | 261 ms
109,764 KB |
testcase_09 | AC | 246 ms
114,036 KB |
testcase_10 | AC | 249 ms
108,340 KB |
testcase_11 | AC | 237 ms
110,820 KB |
testcase_12 | AC | 240 ms
106,656 KB |
testcase_13 | AC | 255 ms
108,096 KB |
testcase_14 | AC | 259 ms
109,292 KB |
testcase_15 | AC | 256 ms
109,356 KB |
testcase_16 | AC | 257 ms
108,048 KB |
ソースコード
def primitive_root(p): if p == 2: return 1 x = p - 1 factors = [2] while x % 2 == 0: x //= 2 for k in range(3, int(p ** 0.5) + 1, 2): if x % k == 0: factors.append(k) while x % k == 0: x //= k if x != 1: factors.append(x) g = 2 while True: ok = True for val in factors: if pow(g, (p - 1) // val, p) == 1: ok = False break if ok: return g g += 1 MOD = 998244353 ROOT = 5 def _ntt(a, h): roots = [pow(ROOT, (MOD - 1) >> i, MOD) for i in range(h + 1)] for i in range(h): m = 1 << (h - i - 1) for j in range(1 << i): w = 1 j *= 2 * m for k in range(m): a[j + k], a[j + k + m] = \ (a[j + k] + a[j + k + m]) % MOD, \ (a[j + k] - a[j + k + m]) * w % MOD w *= roots[h - i] w %= MOD def _intt(a, h): roots = [pow(ROOT, (MOD - 1) >> i, MOD) for i in range(h + 1)] iroots = [pow(r, MOD - 2, MOD) for r in roots] for i in range(h): m = 1 << i for j in range(1 << (h - i - 1)): w = 1 j *= 2 * m for k in range(m): a[j + k], a[j + k + m] = \ (a[j + k] + a[j + k + m] * w) % MOD, \ (a[j + k] - a[j + k + m] * w) % MOD w *= iroots[i + 1] w %= MOD inv = pow(1 << h, MOD - 2, MOD) for i in range(1 << h): a[i] *= inv a[i] %= MOD def ntt_convolve(a, b): len_ab = len(a) + len(b) n = 1 << (len(a) + len(b) - 1).bit_length() h = n.bit_length() - 1 a = list(a) + [0] * (n - len(a)) b = list(b) + [0] * (n - len(b)) _ntt(a, h), _ntt(b, h) a = [va * vb % MOD for va, vb in zip(a, b)] _intt(a, h) return a[:len_ab - 1] p = int(input()) a = [0] + list(map(int, input().split())) b = [0] + list(map(int, input().split())) rt = primitive_root(p) aa = [0] * p bb = [0] * p v = 1 for i in range(p - 1): aa[i] = a[v] bb[i] = b[v] v = v * rt % p cc = ntt_convolve(aa, bb) c = [0] * p v = 1 for i in range(len(cc)): c[v] += cc[i] c[v] %= MOD v = v * rt % p print(*c[1:])