Olympiad in Informatics

Polynomial Inverse

Prerequisites

FFT, NTT, polynomial multiplication.

Deriving the Principle

Now we have an equation like this:

\[ A(x)B(x) \equiv C(x) \ (mod \ x^n) \]

Now we know \(B(x), C(x)\), but when computing the answer we need to work with \(A(x)\). At this point, we need to find the inverse of \(B(x)\). To invert a polynomial, we generally solve it by doubling. Suppose we have the inverse of \(B(x)\) modulo \(x^{\lceil \frac{x}{2} \rceil}\), \(D(x)\):

\[ B(x)D(x) \equiv 1 \ (mod \ x^{\lceil \frac{x}{2} \rceil}) \]

Now we need the inverse modulo \( x^n \), and we now have two equations:

\[ \begin{cases} B(x)D(x) \equiv 1 \ (mod \ x^{\lceil \frac{x}{2} \rceil}) \\ B(x)B^{-1}(x) \equiv 1 \ (mod \ x^{\lceil \frac{x}{2} \rceil}) \end{cases} \]

Subtract the bottom from the top:

\[ \begin{gathered} B(x)(D(x) - B^{-1}(x)) \equiv 0 \ (mod \ x^{\lceil \frac{x}{2} \rceil}) \\ D(x) - B^{-1}(x) \equiv 0 \ (mod \ x^{\lceil \frac{x}{2} \rceil}) \end{gathered} \]

Square both sides:

\[ D^2(x) + B^{-2}(x) - 2D(x)B^{-1}(x) \equiv 0 \ (mod \ n) \]

Here, the reason why the modulus suddenly becomes \(n\) is as follows:

I might put it this way: since \(B(x) - B^{-1}(x) \equiv 0 \pmod {x^{\lceil n/2 \rceil}}\), the left side must be a polynomial of the form \(k\cdot x^{\lceil n/2 \rceil}\), and its square (i.e. \((B(x) - B^{-1}(x))^2\)) is \(k^2 x^n\) (\(n\) even) or \(k^2 x^{n+1}\) (\(n\) odd); clearly both are divisible by \(x_n\)

src: http://blog.miskcoo.com/2015/05/polynomial-inverse, PLANET6174

The remaining multiplication can just be handled with NTT.

Code

// P4238.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 3e5 + 2000, mod = 998244353, G = 3, Gi = 332748118;

int n, rev[MAX_N], mx_pow, mx_bit;
ll ai[MAX_N], ans[MAX_N], tmp[MAX_N];

ll quick_pow(ll bas, ll tim)
{
    ll ans = 1;
    bas %= mod;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}

inline void ntt(ll *arr, int limit, int dft)
{
    for (int i = 0; i < limit; i++)
        if (i < rev[i])
            swap(arr[i], arr[rev[i]]);
    for (int step = 1; step < limit; step <<= 1)
    {
        ll omega_n = quick_pow(dft == 1 ? G : Gi, (mod - 1) / (step << 1));
        for (int j = 0; j < limit; j += (step << 1))
        {
            ll omega_nk = 1;
            for (int k = j; k < j + step; k++, omega_nk = (omega_nk * omega_n % mod))
            {
                ll x = arr[k], y = omega_nk * arr[k + step] % mod;
                arr[k] = (x + y) % mod, arr[k + step] = (x - y + mod) % mod;
            }
        }
    }
    if (dft != 1)
    {
        ll inv = quick_pow(limit, mod - 2);
        for (int i = 0; i < limit; i++)
            arr[i] = (arr[i] * inv % mod);
    }
}

void poly_inverse(int deg, ll *a, ll *b)
{
    if (deg == 1)
        return (void)(b[0] = quick_pow(a[0], mod - 2));
    poly_inverse((deg + 1) >> 1, a, b);

    ll limit = 2, mx_bit = 1;
    while ((deg << 1) > limit)
        limit <<= 1, mx_bit++;
    for (int i = 1; i < limit; i++)
        rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (mx_bit - 1)));

    for (int i = 0; i < deg; i++)
        tmp[i] = a[i];
    for (int i = deg; i < limit; i++)
        tmp[i] = 0;
    ntt(tmp, limit, 1), ntt(b, limit, 1);

    for (int i = 0; i < limit; i++)
        b[i] = 1LL * ((2LL - tmp[i] * b[i] % mod + mod) % mod) * b[i] % mod;

    ntt(b, limit, -1);
    for (int i = deg; i < limit; i++)
        b[i] = 0;
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i <= n - 1; i++)
        scanf("%lld", &ai[i]);
    poly_inverse(n, ai, ans);
    for (int i = 0; i < n; i++)
        printf("%lld ", ans[i]);
    return 0;
}