#P4245. Polynomial Multiplication Under Modulo

    ID: 17492 Type: Default 1000ms 256MiB

Polynomial Multiplication Under Modulo

Polynomial Multiplication Under Modulo

You are given two polynomials \( F(x) \) and \( G(x) \). Your task is to compute the product \( F(x) \times G(x) \) modulo a given integer \( p \). Specifically, if the product is \( H(x)=F(x)\times G(x)=\sum_{i=0}^{n+m} h_i x^i \), then each coefficient \( h_i \) should be computed modulo \( p \).

Note: The modulus \( p \) is arbitrary and is not guaranteed to be of the form \( p = a \cdot 2^k + 1 \). Therefore, algorithms that depend on that structure (such as FFT based on that property) may not be applicable.

Input format: See the input description below.

inputFormat

The input contains three lines:

  1. The first line contains three integers \( n \), \( m \), and \( p \), where \( n \) and \( m \) represent the degrees of the two polynomials \( F(x) \) and \( G(x) \) respectively, and \( p \) is the modulus.
  2. The second line contains \( n+1 \) integers which are the coefficients of \( F(x) \) in increasing order (i.e., from the constant term up to the \( x^n \) term).
  3. The third line contains \( m+1 \) integers which are the coefficients of \( G(x) \) in increasing order.

outputFormat

Output a single line containing \( n+m+1 \) integers: the coefficients of the resulting polynomial \( H(x) = F(x) \times G(x) \) modulo \( p \), in increasing order of powers of \( x \).

sample

1 1 7
1 2
3 4
3 3 1