多项式全家桶

namespace Poly{
const int N=1<<20;
const int mod=998244353;
const int G=3;
int n,rev[N],f[N],g[N],all;
int ksm(int x,int y){
int rt=1;
for(;y;x=(1llxx)%mod,y>>=1)if(y&1)rt=(1llrtx)%mod;
return rt;
}
void NTT(int a,int tp,int LG){
int lim=(1<<LG),invlim=ksm(lim,mod-2);
for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)<<(LG-1));
for(int i=0;i<lim;i++)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int md=1;md<lim;md<<=1){
int rt=ksm(G,(mod-1)/(md<<1));
if(tp==-1)rt=ksm(rt,mod-2);
for(int stp=md<<1,pos=0;pos<lim;pos+=stp){
int w=1;
for(int i=0;i<md;i++,w=(1ll
wrt)%mod){
int x=a[pos+i],y=(1ll
wa[pos+md+i])%mod;
a[pos+i]=(x+y)%mod;
a[pos+md+i]=(x-y+mod)%mod;
}
}
}
if(tp==-1)for(int i=0;i<lim;i++)a[i]=(1ll
a[i]invlim)%mod;
}
int A[N],B[N],C[N],D[N],E[N];
void mul(int a,int b,int c,int LG){//using: Array A and B
int lim=(1<<LG);
for(int i=0;i<lim;i++)A[i]=B[i]=0;
for(int i=0;i<(lim>>1);i++)A[i]=a[i],B[i]=b[i];
NTT(A,1,LG),NTT(B,1,LG);
for(int i=0;i<lim;i++)A[i]=1ll
A[i]
B[i]%mod;
NTT(A,-1,LG);
for(int i=0;i<lim;i++)c[i]=A[i];
}
void inv(int a,int b,int LG){//using: Array C
b[0]=ksm(a[0],mod-2);
for(int k=1;k<=LG+1;k++){
mul(b,a,C,k);
for(int i=0;i<(1<<k);i++)C[i]=(mod-C[i])%mod;
(C[0]+=2)%=mod;
mul(C,b,b,k);
}
}
void diff(int a,int b,int lim){
for(int i=0;i<lim;i++)b[i]=1ll
a[i+1]
(i+1)%mod;
b[lim-1]=0;
}
void inte(int a,int b,int lim){
for(int i=lim-1;i;i--)b[i]=1ll
a[i-1]
ksm(i,mod-2)%mod;
b[0]=0;
}
void ln(int a,int b,int LG){//using: Array C
inv(a,b,LG);
diff(a,C,1<<LG);
mul(b,C,b,LG+1);
inte(b,b,1<<LG);
}
void exp(int a,int b,int LG){//using: Array D
b[0]=1;
for(int k=1;k<=LG+1;k++){
ln(b,D,k-1);
for(int i=0;i<(1<<(k-1));i++)D[i]=(a[i]-D[i]+mod)%mod;
D[0]=(D[0]+1)%mod;
mul(b,D,b,k);
}
}
namespace Residue{//Can be commented if the sqrt part has a0=1
int sqri;
struct cp{
int x,y;
cp(int a=0,int b=0){x=a,y=b;}
friend cp operator (const cp &a,const cp &b){
return cp((1ll
a.x
b.x%mod+1ll
a.y
b.y%mod
sqri%mod)%mod,(1ll
a.y
b.x%mod+1ll
a.x
b.y%mod)%mod);
}
};
bool che(int x){
return ksm(x,mod>>1)==1;
}
cp ksm(cp x,int y){
cp z=cp(1,0);
for(;y;y>>=1,x=xx)if(y&1)z=zx;
return z;
}
int Cipolla(int x){
int a=rand()%mod;
while(true){
sqri=(1llaa%mod-x+mod)%mod;
if(!a||che(sqri))a=rand()%mod;
else break;
}
cp tmp=cp(a,1);
tmp=ksm(tmp,(mod+1)>>1);
return min(mod-tmp.x,tmp.x);
}
}
using namespace Residue;
void sqrt(int *a,int *b,int LG){//using:arrays D&E
b[0]=Cipolla(a[0]);
for(int k=1;k<=LG+1;k++){
for(int i=0;i<(1<<(k-1));i++)D[i]=(b[i]<<1)%mod;
inv(D,E,k-1);
mul(b,b,b,k);
for(int i=0;i<(1<<(k-1));i++)(b[i]+=a[i])%=mod;
mul(b,E,b,k);
}
}
void rever(int *a,int *b,int lim){
for(int i=0;i<lim;i++)b[i]=a[i];
reverse(b,b+lim);
}
void div(int *a,int *b,int *q,int *r,int lima,int limb){//using:Array D
rever(b,D,limb);
for(int i=lima-limb+1;i<limb;i++)D[i]=0;
int all=0;
while((1<<all)<lima-limb+1)all++;
inv(D,q,all);
rever(a,D,lima);
all=0;
while((1<<all)<lima)all++;
for(int i=lima;i<(1<<all);i++)D[i]=0;
for(int i=lima-limb+1;i<(1<<all);i++)q[i]=0;
mul(D,q,q,all+1);
rever(q,q,lima-limb+1);
for(int i=lima-limb+1;i<(1<<all);i++)q[i]=0;
mul(q,b,D,all+1);
for(int i=0;i<limb-1;i++)r[i]=(f[i]-D[i]+mod)%mod;
}
void ksm(int a,int k,int b,int LG){
ln(a,b,LG);
for(int i=0;i<(1<<LG);i++)a[i]=1ll
b[i]
k%mod;
exp(a,b,LG);
}
}

上一篇:【LG P2286】宠物收养场


下一篇:Android:缩放不影响传递给onPreviewFrame()的预览帧数据