Data analysis method ----- Lagrangian interpolation method

In numerical analysis , Lagrange interpolation is a polynomial interpolation method named after the eighteenth-century French mathematician Joseph Lagrange . In many practical problems, functions are used to express certain internal connections or laws, and many functions can only be understood through experiments and observations. For example, if a certain physical quantity is observed in practice and the corresponding observed values ​​are obtained in several different places, the Lagrangian interpolation method can find a polynomial , which just takes the observed value at each observed point. Such polynomials are called Lagrangian (interpolating) polynomials. Mathematically speaking, the Lagrange interpolation method can give a polynomial function that passes through several known points on a two-dimensional plane. Lagrangian interpolation was first discovered by the English mathematician Edward Waring in 1779 and rediscovered shortly thereafter (1783) by Leonhard Euler . In 1795, Lagrange published this interpolation method in his book "Basic Course of Mathematics for Normal Schools", and his name has been associated with this method since then.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=9999991;
ll a[1010],sum[1010],fact[1010],pre[1010],suff[1010],finv[1010];
ll quickpow(ll a,ll b)
{
    ll res=1;
	while(b)
	{
		if(b&1)
		res=(res*a)%mod;
		b>>=1;
		a=(a*a)%mod; 
	}
	return res%mod;
}
void init()
{
	fact[0]=1;
	int i,j,k;
	for(i=1;i<1010;i++)
	{
		fact[i]=fact[i-1]*i%mod;
	}	
	finv[1009]=quickpow(fact[1009],mod-2);
	for(i=1008;i>=0;i--)
	{
		finv[i]=finv[i+1]*(i+1)%mod;
	}
}
ll solve(ll hh[],ll n,ll x)
{
	if(x<=n)
	return hh[x];
	int i,j,k;
	pre[0]=1,suff[n]=1;
	   int ans=0;
    for(int i=1;i<=n;i++)
        pre[i]=ll(pre[i-1]*(x-(i-1)))%mod;
    for(int i=n-1;i>=0;i--)
        suff[i]=ll(suff[i+1]*(x-(i+1)))%mod;
    for(int i=0;i<=n;i++)
	{
        int fuhao=(n-i)&1?-1:1;
        ans+=hh[i]%mod*fuhao*pre[i]%mod*suff[i]%mod*finv[i]%mod*finv[n-i]%mod;
        ans=(ans%mod+mod)%mod;
    }
    return ans;
}
int main()
{
	init();
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m,i,j;
		scanf("%d %d",&n,&m);
		for(i=0;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		a[n+1]=solve(a,n,n+1);
		sum[0]=a[0];
		for(i=1;i<=n+1;i++)
		{
			sum[i]=(sum[i-1]+a[i])%mod;
		}
		while(m--)
		{
			int left,right;
			scanf("%d %d",&left,&right);
			ll ans=(solve(sum,n+1,right)-solve(sum,n+1,left-1));
			ans=(ans%mod+mod)%mod;
			printf("%lld\n",ans);
		}
	}
	return 0;
} 

references:

Lagrangian interpolation - Wikipedia

Application of Lagrange interpolation (ACM, 2019 Nanchang Invitational Question B)_hash[072]'s blog-CSDN Blog

Niuke.com summer ACM multi-school training camp (first session) F Sum of Maximum (combinatorial mathematics, Lagrange interpolation)_czdb's blog-CSDN blog

Guess you like

Origin blog.csdn.net/qq_41050642/article/details/128297693