Interpolation is a method of estimating new data points within the range of a discrete set of known data points. Interpolation is often used in function fitting.
For example, given the data points:
\(x\)
\(0\)
\(1\)
\(2\)
\(3\)
\(4\)
\(5\)
\(6\)
\(f(x)\)
\(0\)
\(0.8415\)
\(0.9093\)
\(0.1411\)
\(-0.7568\)
\(-0.9589\)
\(-0.2794\)
where \(f(x)\) is unknown, interpolation can estimate unknown data points by fitting \(f(x)\) in a certain form.
For instance, we can fit \(f(x)\) with a piecewise linear function:
The general form of polynomial interpolation is as follows:
Polynomial Interpolation
Given \(n+1\) known points \((x_0,y_0),(x_1,y_1),\dots,(x_n,y_n)\), find a polynomial \(f(x)=\sum_{i=0}^n a_ix^i\) that satisfies
\[
f(x_i)=y_i,\qquad\forall i=0,1,\dots,n
\]
Below, two methods for polynomial interpolation are introduced: Lagrange interpolation and Newton interpolation. It can be shown that the results obtained by these two methods are equivalent.
Lagrange Interpolation
We need to construct a function \(f(x)\) passing through points \(P_1(x_1, y_1), P_2(x_2,y_2),\cdots,P_n(x_n,y_n)\). First, let the projection of the \(i\)-th point onto the \(x\)-axis be \(P_i^{\prime}(x_i,0)\).
Consider constructing \(n\) functions \(f_1(x), f_2(x), \cdots, f_n(x)\), such that for the \(i\)-th function \(f_i(x)\), its graph passes through \(\begin{cases}P_j^{\prime}(x_j,0),(j\neq i)\\P_i(x_i,y_i)\end{cases}\). Then the desired function \(f(x)=\sum\limits_{i=1}^nf_i(x)\).
We can set \(f_i(x)=a\cdot\prod_{j\neq i}(x-x_j)\). Substituting point \(P_i(x_i,y_i)\) gives \(a=\dfrac{y_i}{\prod_{j\neq i} (x_i-x_j)}\), so
Given \(n\) point pairs \((x_i,y_i)\) and \(k\), with \(\forall i,j\) satisfying \(i\neq j \iff x_i\neq x_j\), \(f(x_i)\equiv y_i\pmod{998244353}\), and \(\deg(f(x)) < n\) (defining \(\deg(0)=-\infty\)), find \(f(k)\bmod{998244353}\).
Solution
In this problem, we only need to compute the value of \(f(k)\), so we can directly substitute \(k\) into the formula. Sometimes, multiple evaluations or more complex operations are required, in which case we need to find all coefficients of \(f\). The code provides an implementation for obtaining the coefficients.
\[
f(k)=\sum_{i=1}^{n}y_i\prod_{j\neq i }\frac{k-x_j}{x_i-x_j}
\]
In this problem, modular inverses also need to be computed. If we first compute the numerator and denominator separately, then multiply the numerator by the modular inverse of the denominator and accumulate into the final answer, the bottleneck of time complexity will not be in computing inverses, resulting in \(O(n^2)\) time complexity.
Since operations are under the fixed modulus \(998244353\), we consider the time complexity of computing modular inverses as constant here.
#include<iostream>#include<vector>constexprintMOD=998244353;usingLL=longlong;intinv(intk){intres=1;for(inte=MOD-2;e;e/=2){if(e&1)res=(LL)res*k%MOD;k=(LL)k*k%MOD;}returnres;}// Returns f such that f(x_i) = y_i// Without considering the time for modular inverse, complexity is O(n^2)std::vector<int>lagrange_interpolation(conststd::vector<int>&x,conststd::vector<int>&y){constintn=x.size();std::vector<int>M(n+1),px(n,1),f(n);M[0]=1;// Compute M(x) = prod_(i=0..n-1)(x - x_i)for(inti=0;i<n;++i){for(intj=i;j>=0;--j){M[j+1]=(M[j]+M[j+1])%MOD;M[j]=(LL)M[j]*(MOD-x[i])%MOD;}}// Compute px_i = prod_(j=0..n-1, j!=i) (x_i - x_j)for(inti=0;i<n;++i){for(intj=0;j<n;++j)if(i!=j){px[i]=(LL)px[i]*(x[i]-x[j]+MOD)%MOD;}}// Combine to get f(x) = sum_(i=0..n-1)(y_i / px_i)(M(x) / (x - x_i))for(inti=0;i<n;++i){LLt=(LL)y[i]*inv(px[i])%MOD,k=M[n];for(intj=n-1;j>=0;--j){f[j]=(f[j]+k*t)%MOD;k=(M[j]+k*x[i])%MOD;}}returnf;}intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn,k;std::cin>>n>>k;std::vector<int>x(n),y(n);for(inti=0;i<n;++i)std::cin>>x[i]>>y[i];constautof=lagrange_interpolation(x,y);intv=0;for(inti=n-1;i>=0;--i)v=((LL)v*k+f[i])%MOD;std::cout<<v<<'\n';return0;}
Lagrange Interpolation with Consecutive Integer x-coordinates
If the x-coordinates of the known points are consecutive integers, we can achieve \(O(n)\) interpolation.
Let the desired polynomial be \(f(x)\), and we know \(f(1),\cdots,f(n+1)\) (\(1\le i\le n+1\)). Substituting into the interpolation formula:
Given \(n,k\), find \(\sum\limits_{i=1}^ni^k\) modulo \(10^9+7\).
Solution
In this problem, the answer is a polynomial of degree \(k+1\). Therefore, we can linearly sieve the values of \(1^i,\cdots,(k+2)^i\) and then perform \(O(n)\) interpolation.
Alternatively, the following formula can be derived from the difference method using combinatorial mathematics:
Newton Interpolation with Consecutive Integer x-coordinates
For example: Find the coefficients of polynomial \(f(x)=\sum_{i=0}^{3} a_ix^i\), given that the values of \(f(1)\) to \(f(6)\) are \(1, 5, 14, 30, 55, 91\) respectively.
The first row contains the first \(n\) consecutive values of \(f(x)\); each subsequent row contains the differences between adjacent terms of the previous row. It can be observed that if this operation is repeated enough times (provided \(f(x)\) is a polynomial), it will eventually yield a constant value.
The first term of the \((i-1)\)-th order difference is \(\sum_{j=1}^{i}(-1)^{i+j}\binom{i-1}{j-1}f(j)\). The contribution of the first term of the \((i-1)\)-th order difference to \(f(k)\) is \(\binom{k-1}{i-1}\) times.