【BZOJ3453】【TYVJ1858】—XLkxc(拉格朗日插值)
即求
拉格朗日插值套拉格朗日插值套拉格朗日插值
其实发现不需要拉格朗日插值求
直接求出个点值
对做一次拉格朗日插值即可
注意模数极其恶心
加法会爆
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<21|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define bg begin
#define re register
const int mod=1234567891;
inline int add(int a,int b){return (ll)a+b>=mod?(ll)a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=a>=b?a-b:a-b+mod;}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline void chemx(int &a,int b){a>b?0:a=b;}
inline void chemn(int &a,int b){a<b?a=b:0;}
const int N=150;
int pf[N],fac[N],f[N],g[N],t[N];
inline void init(){
fac[0]=1;
for(int i=1;i<N;i++)fac[i]=mul(fac[i-1],i);
}
inline int calc(int m,int n,int *y){
if(m<=n+1)return y[m];
int res=0;
for(int i=1;i<=n+1;i++){
int tmp=mul(fac[i-1],fac[n-i+1]);Mul(tmp,m-i);
tmp=ksm(tmp,mod-2);if((n-i+1)&1)Mul(tmp,dec(0,1));
Mul(tmp,y[i]),Add(res,tmp);
}
for(int i=1;i<=n+1;i++)Mul(res,dec(m,i));return res;
}
int k,n,a,d;
signed main(){
init();
int T=read();
while(T--){
memset(f,0,sizeof(f)),memset(g,0,sizeof(g)),memset(t,0,sizeof(t));
k=read(),a=read(),n=read(),d=read();
for(int i=1;i<=k+3;i++)f[i]=add(ksm(i,k),f[i-1]);
for(int i=1;i<=k+3;i++)g[i]=add(f[i],g[i-1]);
t[0]=calc(a,k+2,g);
for(int i=1;i<=k+4;i++)t[i]=add(calc(add(a,mul(i,d)),k+2,g),t[i-1]);
cout<<calc(n,k+3,t)<<'\n';
}
}