Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

[TUTORIAL] Basic Lagrange Interpolation

Revision en8, by grhkm, 2020-09-23 20:20:39

Lagrange Interpolation by grhkm

No polynomial stuff because I don't know O(nlogn) polynomial multiplication :(

Main results:

Given f(x1)=y1,f(x2)=y2,...,f(xn)=yn we can calculate f(X) where f is the unique (n+1)-degree polynomial

For general {xi} we can do so in O(n2)

For consecutive {xi} i.e. xj=x1+(j1), we can do so in O(n) excluding binary exponentiation

Why useful?

Calculate 1n+2n+...+Xn in O(n)

DP optimization

With polynomials you can do cool things but not gonna cover this blog

Let's get started :)

Assume all polynomials below are (n+1) degree unless specified.

disclaimer
Why (n+1) degree?

Idea:

To start let's deal with first problem:

Given f(x1)=y1,f(x2)=y2,...,f(xn)=yn and an integer X we can calculate f(X) where f is the unique (n+1)-degree polynomial

Let's consider an easier version:

Find polynomial satisfying f(x1)=yi,f(x2)=0,...,f(xn)=0

As we learn in school, f(r)=0(xr) is divisor

We can write this down:

f(x)=(xx2)(xx3)(xxn)

Now notice that f(x1)=ni=2(x1xi) (a constant), but we want it to be y1

We can simply scale the polynomial by a correct factor i.e.

f1(x)=ni=2(xxi)y1ni=2(x1xi)=y1ni=2xxix1xi

Similarly, we can find a similar polynomial such that fi(xi)=yi and fi(xj)=0ji

fi(x)=yinj=1,jixxixixi

Okay now finally to answer the original question, we can notice that f(x)=ni=1fi(x) satisfies the constraints. So there we have it! Lagrange interpolation:

f(x)=ni=1yinj=1,jixxjxixj

O(n2) excluding inverse for division

Optimization:

Let's try to optimise it now! Assume that we're given f(1),f(2),,f(n), how can we calculate f(X) quickly?

Notice that here, xi=i and yi=f(i). Substitute!

f(x)=ni=1f(i)nj=1,jixjij=ni=1f(i)nj=1,jixjnj=1,jiij

Let's consider the numerator and denominator separately

Numerator

nj=1,jixj=[(x1)(x2)(x(i1))][(x(i+1))(x(i+2))(xn)]

We can pre-calculate prefix and suffix product of x, then we can calculate the numerator (for each i) in O(1)

Denominator

nj=1,jiij=[(i1)(i2)(i3)(i(i1))][(i(i+1))(i(i+2))(in)]
=(i1)!(1)n(i+1)+1(ni)!
=(1)ni(ni)!(i1)!

So we can precompute factorials (preferably their inverse directly) then O(1) calculation

So now we can calculate f(X) in O(n)

Example 0

Find_ Ni=1ik, k1e6,N1e12

Solution: Notice that the required sum will be a degree (k+1) polynomial, and thus we can interpolate the answer with (k+2) data points (Remember, we need deg(f)+1 points)

To find the data points simply calculate f(0)=0,f(x)=f(x1)+xk

Here is my code for 622F: here

Example 1

(BZOJ 3453, judge dead): Find_ \sum_{i=0}^n \sum_{j=1}^{a+id} \sum_{k=1}^j k^x \mod 1234567891, x \leq 123, a, n, d \leq 123456789

Hint

Example 2 (ADVANCED)

(Luogu P4463 submit here): Given n \leq 500, k \leq 1e9, we call a sequence {a_n} nice if 1\leq a_i \leq k \forall i and (a_i\neq a_j) \forall i\neq j. Find the sum of (products of elements in the sequence) over all nice sequences._

Hint
Hint2
Hint3
Hint4

Example 3 (ADVANCED TOO)

Problem https://www.codechef.com/problems/XETF : Find \sum_{i=1,gcd(i,N)=1}^N i^k, N \leq 1e12, k \leq 256

Hint

If you have any questions feel free to ask below

Tags lagrange-interpolation, math, number-theory

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en18 English grhkm 2020-09-27 16:45:16 69
en17 English grhkm 2020-09-24 15:08:26 246
en16 English grhkm 2020-09-24 04:36:04 61
en15 English grhkm 2020-09-24 04:32:30 90 :)
en14 English grhkm 2020-09-24 02:58:51 81
en13 English grhkm 2020-09-23 20:53:46 187
en12 English grhkm 2020-09-23 20:52:53 610
en11 English grhkm 2020-09-23 20:31:15 69
en10 English grhkm 2020-09-23 20:29:53 145
en9 English grhkm 2020-09-23 20:28:58 351
en8 English grhkm 2020-09-23 20:20:39 62
en7 English grhkm 2020-09-23 20:02:14 11
en6 English grhkm 2020-09-23 19:54:48 197
en5 English grhkm 2020-09-23 19:53:27 494 no comment idk why im capping
en4 English gupta_samarth 2020-09-23 19:50:29 65 Tiny change: 'calculate f(X) where f is the un' -> 'calculate $f(X)$ where $f$ is the un'
en3 English grhkm 2020-09-23 19:22:27 211
en2 English grhkm 2020-09-23 19:20:52 78
en1 English grhkm 2020-09-23 19:14:05 5510 Initial revision (published)