Multiplying Large Polynomials
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Two polynomials are given by their coefficient arrays (a[i] is the coefficient of ). Return the coefficients of
modulo 998,244,353, from upward. The result always has length a.length + b.length - 1
(zero coefficients are kept).
Examples
| a | b | result |
|---|---|---|
[1, 2] | [3, 4] | [3, 10, 8] |
[1, 1, 1] | [1, 998244352] | [1, 0, 0, 998244352] |
[5] | [0, 0, 7] | [0, 0, 35] |
Example 1 — .
Example 2 — , so the product is .
Example 3 — low-order zero coefficients stay in the result.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(a: int[], b: int[]) → int[]
Constraints
-
a.length,b.length -
a[i],b[i]
Expected complexity
- Time
- O(N log N)
- Space
- O(N)
Examples
| a | b | Expected |
|---|---|---|
[1,2] | [3,4] | [3,10,8] |
[1,1,1] | [1,998244352] | [1,0,0,998244352] |
[5] | [0,0,7] | [0,0,35] |
Hint
Hint
Schoolbook multiplication is . Instead evaluate both polynomials at the -th roots of unity ( a power of two
a.length + b.length - 1), multiply pointwise, and interpolate back; divide and conquer on even/odd terms makes both
transforms . Because is prime with primitive root , exact integer roots
of unity exist modulo it, so use the number-theoretic transform (multiply by after the inverse transform). The
schoolbook method exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 3370 ms | derived |
| Java 25 | 1520 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 4 | verdict only |