Lagrange Interpolation
Overview
If you have a bunch of scattered points on hand, then you can fit a function with this formula:
\[ f(x) = \sum_{i = 1}^n y_i \prod_{j \neq i}^n \frac{x - x_j}{x_i - x_j} \]
The template can be written like this:
int evaluate(int x, int *xi, int *yi)
{
int ret = 0;
for (int i = 1; i <= n; i++)
{
int pans = yi[i];
for (int j = 1; j <= n; j++)
if (i != j)
pans = 1LL * pans * ((0LL + x + mod - xi[j]) % mod) % mod * fpow((0LL + xi[i] + mod - xi[j]) % mod, mod - 2) % mod;
ret = (0LL + ret + pans) % mod;
}
return ret;
}
Some Optimizations of Lagrange Interpolation
The case where \(x_1\) is consecutive
If \(x_1\) turns out to be consecutive, then it becomes like this:
\[ f(x) = \sum_{i = 1}^n y_i \prod_{j \neq i}^n \frac{x - j}{i - j} \]
Then both the top and bottom parts can be precomputed. Note that when \(i < j\), you need to check the length of the factorial to decide whether to give it a minus sign. This way the whole process becomes \(\Theta(n)\).
Barycentric Lagrange Interpolation
If you now need to interpolate over a bunch of points many times, how do you achieve a good complexity?
Consider the original formula:
\[ \begin{aligned} f(x) &= \sum_{i = 1}^n y_i \prod_{j \neq i}^n \frac{x - x_j}{x_i - x_j} \\ &= \prod_{i = 1}^n (x - x_i) \sum_{i = 1}^n \frac{y_i}{x - x_i} \prod_{j \neq i} \frac{1}{x_i - x_j} \end{aligned} \]
Define the following things:
\[ l(x) = \prod_{i = 1}^n (x - x_i), \omega_i = \prod_{j \neq i} \frac{1}{x_i - x_j} \]
The original formula becomes:
\[ f(x) = l(x) \sum_{i = 1}^n y_i \frac{\omega_i}{x - x_i} \]
A single evaluation drops to \(\Theta(n)\).
Example Problems
Common scenarios for optimizing with Lagrange interpolation are the following:
- Terms where the enumeration range is large but the degree is low
- The DP itself is the coefficients of a polynomial
- Counting polynomials produced by the matrix-tree theorem (although Gaussian elimination would be better)
BZOJ2655 - Calc
Consider a DP related to \(A\): let \(dp[i][j]\) be the answer for the first \(i\) numbers with \(j\) of them picked. The transition for this thing is simple:
\[ dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] \times i \]
Non-obviously, this thing ends up being a degree-\(2n+1\) polynomial. So just interpolate.
// BZ2655.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1010;
int A, n, mod, dp[MAX_N][MAX_N];
int fpow(int bas, int tim)
{
int ret = 1;
while (tim)
{
if (tim & 1)
ret = 1LL * ret * bas % mod;
bas = 1LL * bas * bas % mod;
tim >>= 1;
}
return ret;
}
int main()
{
scanf("%d%d%d", &A, &n, &mod), dp[0][0] = 1;
for (int i = 1; i <= 2 * n + 1; i++)
{
dp[i][0] = dp[i - 1][0];
for (int j = 1; j <= n; j++)
dp[i][j] = (0LL + dp[i - 1][j] + 1LL * dp[i - 1][j - 1] * i % mod) % mod;
}
int ans = 0;
if (A <= 2 * n + 1)
ans = dp[A][n];
else
for (int i = 1; i <= 2 * n + 1; i++)
{
// yi[i] = f[i][n];
int pans = 1;
for (int j = 1; j <= 2 * n + 1; j++)
if (i != j)
pans = 1LL * pans * ((0LL + A + mod - j) % mod) % mod * fpow((0LL + i + mod - j) % mod, mod - 2) % mod;
ans = (0LL + ans + 1LL * dp[i][n] * pans % mod) % mod;
}
for (int i = 1; i <= n; i++)
ans = 1LL * ans * i % mod;
printf("%lld\n", ans);
return 0;
}