Description
给定 k,a,n,d,p
f(i)=1^k+2^k+3^k+......+i^k
g(x)=f(1)+f(2)+f(3)+....+f(x)
求(g(a)+g(a+d)+g(a+2d)+......+g(a+nd))mod p
Input
第一行数据组数,(保证小于6)
以下每行四个整数 k,a,n,d
Output
每行一个结果。
Sample Input
5
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Sample Output
5
5
5
5
5
HINT
1<=k<=123
0<=a,n,d<=123456789
p==1234567891
来自:https://www.cnblogs.com/BearChild/p/6424353.html
昨天牛客多校F题就是一个利用插值法求值,这题就是与那道类似的,分析因为插值到,第一次见拉格朗日插值在这出现(数值分析学过),但关于上面g再差分k+3次误差为0是怎么推得我还得好好琢磨琢磨。但大致思想已经理解了,但还有些问题就是关于龙格现象,和误差分析的地方我还是得找老师在问一下。
代码来自:https://www.cnblogs.com/fengzhiyuan/p/8646464.
#include<iostream>
#include<string.h>
#include<string>
#include<vector>
#include<iomanip>
#include<math.h>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<time.h>
#include<map>
#define PI acos(-1.0)
#define ll long long
using namespace std;
#define p 1234567891
#define N 157
ll a,n,d,m,k;
ll s1[N],s2[N];
ll g[N],f[N],inv[N<<1];
ll fast_pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if (b&1) (ans*=a)%=p;
(a*=a)%=p;
b>>=1;
}
return ans;
}
inline ll Lagrange(ll *a,int n,ll pos)
{
if (pos<=n) return a[pos];
ll ans=0;
for (int i=1;i<=n;i++)
{
ll s1=1,s2=1;
for (int j=1;j<=n;j++)
if (i!=j)
{
(s1*=(pos-j))%=p;
(s2*=(i-j))%=p;
}
(ans+=a[i]*s1%p*fast_pow(s2,p-2))%=p;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&k,&a,&n,&d);
for (int i=1;i<=k+3;i++) g[i]=fast_pow(i,k);
for (int i=2;i<=k+3;i++) (g[i]+=g[i-1])%=p;
for (int i=2;i<=k+3;i++) (g[i]+=g[i-1])%=p;
f[0]=Lagrange(g,k+3,a);
for (int i=1;i<=k+5;i++) f[i]=Lagrange(g,k+3,(i*d+a)%p),(f[i]+=f[i-1])%=p;
printf("%lld\n",(Lagrange(f,k+5,n)+p)%p);
}
}