标签:cost ++i nsf set 自动机 init ret void wap
int ch[N*2][26],pre[N*2],len[N*2],last=1,k=1;
void Append(int c)
{
int p=last;last=++k;len[k]=len[p]+1;
for(;!ch[p][c]&&p;p=pre[p])
ch[p][c]=k;
if(!p){pre[k]=1;return;}
int np=ch[p][c];
if(len[np]==len[p]+1){pre[k]=np;return;}
int q=++k;
memcpy(ch[q],ch[np],sizeof(ch[q]));
pre[q]=pre[np];len[q]=len[p]+1;
pre[np]=pre[last]=q;
for(;ch[p][c]==np;p=pre[p])
ch[p][c]=q;
}
int dis[N],head[N],Next[E],adj[E],cap[E],k=1,S,T;
int que[N],now[N];
void addedge(int u,int v,int w)
{
Next[++k]=head[u],head[u]=k,adj[k]=v,cap[k]=w;
Next[++k]=head[v],head[v]=k,adj[k]=u,cap[k]=0;
}
bool BFS()
{
int i,l,r;
memset(dis,-1,sizeof(dis));
que[l=r=1]=S;
dis[S]=0;
while(l<=r)
{
for(i=head[que[l]];i;i=Next[i])
if(dis[adj[i]]==-1&&cap[i])
{
dis[adj[i]]=dis[que[l]]+1;
que[++r]=adj[i];
}
++l;
}
return (dis[T]!=-1);
}
int fl;
bool DFS(int i,int f)
{
if(i==T)
{
fl=f;
return true;
}
int j;
for(j=now[i];j;now[i]=j=Next[j])
if(dis[adj[j]]==dis[i]+1&&cap[j]&&DFS(adj[j],min(cap[j],f)))
{
cap[j]-=fl;
cap[j^1]+=fl;
return true;
}
return false;
}
long long Dinic()
{
long long ans=0;
while(BFS())
{
memcpy(now,head,sizeof(head));
while(DFS(S,inf))
ans+=fl;
}
return ans;
}
int head[N],Next[E],adj[E],cap[E],cost[E];
int q[N*20],pre[N],vis[N],now[N];
int flow,S,T,num=1;
long long d[N];
void addedge(int u,int v,int f,int w)
{
Next[++num]=head[u],head[u]=num,adj[num]=v,cap[num]=f,cost[num]=w;
Next[++num]=head[v],head[v]=num,adj[num]=u,cap[num]=0,cost[num]=-w;
}
bool SPFA()
{
int i,l,r;
q[l=r=1]=S;
vis[S]=1;
memset(d,0x7f,sizeof(d));
d[S]=0;
while(l<=r)
{
for(i=head[q[l]];i;i=Next[i])
if(cap[i]&&d[adj[i]]>d[q[l]]+cost[i])
{
d[adj[i]]=d[q[l]]+cost[i];
if(!vis[adj[i]])
{
vis[adj[i]]=1;
q[++r]=adj[i];
}
}
vis[q[l]]=0;
++l;
}
return (d[T]<1<<30);
}
bool dfs(int i,int f)
{
if(i==T)
{
flow=f;
return true;
}
if(vis[i])
return false;
int j;
vis[i]=1;
for(j=now[i];j;now[i]=j=Next[j])
if(cap[j]&&d[adj[j]]==d[i]+cost[j]&&dfs(adj[j],min(f,cap[j])))
{
cap[j]-=flow;
cap[j^1]+=flow;
vis[i]=0;
return true;
}
vis[i]=0;
return false;
}
long long ans,ansf;
void minmax()
{
while(SPFA())
{
memcpy(now,head,sizeof(now));
while(dfs(S,inf))
ans+=flow*d[T],ansf+=flow;
}
}
const int M=998244353;
const int N=100005,E=262144;
int R[N*4];
long long qpow(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%M;
a=a*a%M;
b>>=1;
}
return ans;
}
long long wn[N*4],iwn[N*4];
void init()
{
int i;
iwn[E/2]=wn[E/2]=1;
long long s1=qpow(3,(M-1)/E);
long long s2=qpow(s1,M-2);
for(i=E/2+1;i<E;++i)
{
wn[i]=wn[i-1]*s1%M;
iwn[i]=iwn[i-1]*s2%M;
}
for(i=E/2-1;i;--i)
{
wn[i]=wn[i<<1];
iwn[i]=iwn[i<<1];
}
}
void NTT(long long *f,int lim,int op)
{
int i,j,k;
for(i=0;i<lim;++i)
{
R[i]=(R[i>>1]>>1)|(i&1?lim>>1:0);
if(R[i]<i)
swap(f[R[i]],f[i]);
}
for(i=1;i<lim;i<<=1)
for(j=0;j<lim;j+=(i<<1))
for(k=j;k<j+i;++k)
{
long long a=f[k],b=f[k+i];
long long w=(op==1?wn[k-j+i]:iwn[k-j+i]);
f[k]=(a+b*w)%M;
f[k+i]=(a-b*w)%M;
}
if(op==-1)
{
long long inv=qpow(lim,M-2);
for(i=0;i<lim;++i)
f[i]=f[i]*inv%M;
}
}
void mult(long long *a,int n,long long *b,int m)
{
if(n+m<=300)
{
long long t[n+m-1];
memset(t,0,sizeof(t));
for(int i=0;i<n;++i)
{
int j;
for(j=0;j+4<=m;j+=4)
{
t[i+j]=(t[i+j]+a[i]*b[j])%M;
t[i+j+1]=(t[i+j+1]+a[i]*b[j+1])%M;
t[i+j+2]=(t[i+j+2]+a[i]*b[j+2])%M;
t[i+j+3]=(t[i+j+3]+a[i]*b[j+3])%M;
}
for(;j<m;++j)
t[i+j]=(t[i+j]+a[i]*b[j])%M;
}
for(int i=0;i<n+m-1;++i)
a[i]=t[i];
return;
}
int lim=1;
while(lim<n+m)
lim<<=1;
for(int i=n;i<lim;++i)
a[i]=0;
for(int i=m;i<lim;++i)
b[i]=0;
NTT(a,lim,1);
NTT(b,lim,1);
for(int i=0;i<lim;++i)
a[i]=a[i]*b[i]%M;
NTT(a,lim,-1);
}
咕咕咕……
标签:cost ++i nsf set 自动机 init ret void wap
原文地址:https://www.cnblogs.com/invisible-eyes/p/12512493.html