# 有标号的DAG计数

## 有标号的DAG计数I

$n$点带标号$DAG$的数量模$10007$$n\le5000$

$f_i=\sum_{j=1}^i(-1)^{j-1}\binom ij2^{j(i-j)}f_{i-j}$

## 有标号的DAG计数II

$n$点带标号$DAG$的数量模$998244353$$n\le100000$

$j(i-j)=\frac{i^2}{2}-\frac{j^2}{2}-\frac{(i-j)^2}{2}$

$f_i=\sum_{j=1}^i(-1)^{j-1}\frac{i!}{j!(i-j)!}\frac{\sqrt2^{i^2}}{\sqrt2^{j^2}\sqrt2^{(i-j)^2}}f_{i-j}$

$F(x)=\sum_{i=0}^n\frac{f_i}{i!\sqrt2^{i^2}}x^i,G(x)=\sum_{i=1}^n\frac{(-1)^{i-1}}{i!\sqrt2^{i^2}}x^i$

## 有标号的DAG计数III

$n$点带标号$DAG$的数量模$10007$，要求图弱连通。$n\le5000$

$g_i=f_i-\sum_{j=1}^{i-1}\binom{i-1}{j}f_jg_{i-j}$

## 有标号的DAG计数IV

$n$点带标号$DAG$的数量模$998244353$，要求图弱连通。$n\le100000$

$\frac{g_i}{(i-1)!}=\frac{f_i}{(i-1)!}-\sum_{j=1}^{i-1}\frac{f_j}{j!}\frac{g_{i-j}}{(i-j-1)!}$

$F(x)=\sum_{i=1}^n\frac{f_i}{i!}x^i,G(x)=\sum_{i=1}^n\frac{g_i}{(i-1)!}x^i,H(x)=\sum_{i=1}^n\frac{f_i}{(i-1)!}x^i$

## code

### 有标号的DAG计数I

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 5005;
const int mod = 10007;
int n,C[N][N],bin[N*N],f[N];
int main(){
scanf("%d",&n);
for (int i=C[0][0]=1;i<=n;++i)
for (int j=C[i][0]=1;j<=i;++j)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
for (int i=bin[0]=1;i<=n*n;++i) bin[i]=(bin[i-1]<<1)%mod;
f[0]=1;
for (int i=1;i<=n;++i)
for (int j=1;j<=i;++j)
f[i]=(f[i]+1ll*(j&1?1:mod-1)*bin[j*(i-j)]%mod*C[i][j]%mod*f[i-j])%mod;
printf("%d\n",f[n]);return 0;
}


### 有标号的DAG计数II

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 3e5+5;
const int mod = 998244353;
const int rem = 882049182;
int n,jc[N],jcn[N],g[N],f[N],rev[N],og[N];
int fastpow(int a,int b){
int res=1;
while (b) {if (b&1) res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}
return res;
}
void init(int len){
int l=0;while ((1<<l)<len) ++l;
for (int i=0;i<len;++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<l-1);
}
void ntt(int *P,int op,int len){
for (int i=0;i<len;++i) if (i<rev[i]) swap(P[i],P[rev[i]]);
for (int i=1;i<len;i<<=1){
int W=fastpow(3,(mod-1)/(i<<1));
if (op==-1) W=fastpow(W,mod-2);
og[0]=1;for (int j=1;j<i;++j) og[j]=1ll*og[j-1]*W%mod;
for (int p=i<<1,j=0;j<len;j+=p)
for (int k=0;k<i;++k){
int x=P[j+k],y=1ll*og[k]*P[j+k+i]%mod;
P[j+k]=(x+y)%mod;P[j+k+i]=(x-y+mod)%mod;
}
}
if (op==-1) for (int i=0,Inv=fastpow(len,mod-2);i<len;++i) P[i]=1ll*P[i]*Inv%mod;
}
int tmp[N];
void Get_Inv(int *a,int *b,int len){
if (len==1) {b[0]=fastpow(a[0],mod-2)%mod;return;}
Get_Inv(a,b,len>>1);
for (int i=0;i<len;++i) tmp[i]=a[i];
init(len<<1);ntt(tmp,1,len<<1);ntt(b,1,len<<1);
for (int i=0;i<len<<1;++i) tmp[i]=(2ll*b[i]-1ll*b[i]*b[i]%mod*tmp[i]%mod+mod)%mod;
ntt(tmp,-1,len<<1);
for (int i=0;i<len;++i) b[i]=tmp[i],b[i+len]=0;
}
int main(){
scanf("%d",&n);g[0]=jc[0]=jcn[0]=1;
for (int i=1,inv=fastpow(rem,mod-2);i<=n;++i){
jc[i]=1ll*jc[i-1]*i%mod,jcn[i]=fastpow(jc[i],mod-2);
g[i]=1ll*(i&1?mod-1:1)*fastpow(inv,1ll*i*i%(mod-1))%mod*jcn[i]%mod;
}
int len=1;while (len<=n) len<<=1;
Get_Inv(g,f,len);
printf("%lld\n",1ll*f[n]*fastpow(rem,1ll*n*n%(mod-1))%mod*jc[n]%mod);
return 0;
}


### 有标号的DAG计数III

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 5005;
const int mod = 10007;
int n,bin[N*N],C[N][N],f[N],g[N];
int main(){
scanf("%d",&n);
for (int i=C[0][0]=1;i<=n;++i)
for (int j=C[i][0]=1;j<=i;++j)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
for (int i=bin[0]=1;i<=n*n;++i) bin[i]=(bin[i-1]<<1)%mod;
f[0]=g[0]=1;
for (int i=1;i<=n;++i){
for (int j=1;j<=i;++j)
f[i]=(f[i]+1ll*(j&1?1:mod-1)*C[i][j]*bin[j*(i-j)]*f[i-j])%mod;
g[i]=f[i];
for (int j=1;j<i;++j)
g[i]=(g[i]-1ll*C[i-1][j]*f[j]*g[i-j]%mod+mod)%mod;
}
printf("%d\n",g[n]);return 0;
}


### 有标号的DAG计数IV

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 3e5+5;
const int mod = 998244353;
const int rem = 882049182;
int n,jc[N],jcn[N],g[N],f[N],h[N],rev[N],og[N];
int fastpow(int a,int b){
int res=1;
while (b) {if (b&1) res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}
return res;
}
void init(int len){
int l=0;while ((1<<l)<len) ++l;
for (int i=0;i<len;++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<l-1);
}
void ntt(int *P,int op,int len){
for (int i=0;i<len;++i) if (i<rev[i]) swap(P[i],P[rev[i]]);
for (int i=1;i<len;i<<=1){
int W=fastpow(3,(mod-1)/(i<<1));
if (op==-1) W=fastpow(W,mod-2);
og[0]=1;for (int j=1;j<i;++j) og[j]=1ll*og[j-1]*W%mod;
for (int p=i<<1,j=0;j<len;j+=p)
for (int k=0;k<i;++k){
int x=P[j+k],y=1ll*og[k]*P[j+k+i]%mod;
P[j+k]=(x+y)%mod;P[j+k+i]=(x-y+mod)%mod;
}
}
if (op==-1) for (int i=0,Inv=fastpow(len,mod-2);i<len;++i) P[i]=1ll*P[i]*Inv%mod;
}
int tmp[N];
void Get_Inv(int *a,int *b,int len){
if (len==1) {b[0]=fastpow(a[0],mod-2)%mod;return;}
Get_Inv(a,b,len>>1);
for (int i=0;i<len;++i) tmp[i]=a[i];
init(len<<1);ntt(tmp,1,len<<1);ntt(b,1,len<<1);
for (int i=0;i<len<<1;++i) tmp[i]=(2ll*b[i]-1ll*b[i]*b[i]%mod*tmp[i]%mod+mod)%mod;
ntt(tmp,-1,len<<1);
for (int i=0;i<len;++i) b[i]=tmp[i],b[i+len]=tmp[i]=tmp[i+len]=0;
}
int main(){
scanf("%d",&n);g[0]=jc[0]=jcn[0]=1;
for (int i=1,inv=fastpow(rem,mod-2);i<=n;++i){
jc[i]=1ll*jc[i-1]*i%mod,jcn[i]=fastpow(jc[i],mod-2);
g[i]=1ll*(i&1?mod-1:1)*fastpow(inv,1ll*i*i%(mod-1))%mod*jcn[i]%mod;
}
int len=1;while (len<=n) len<<=1;
Get_Inv(g,f,len);
for (int i=0;i<=n;++i){
f[i]=1ll*f[i]*fastpow(rem,1ll*i*i%(mod-1))%mod;
h[i]=1ll*f[i]*i%mod;g[i]=0;
}
for (int i=n+1;i<len;++i) f[i]=g[i]=0;
Get_Inv(f,g,len);
for (int i=n+1;i<len;++i) g[i]=0;
init(len);ntt(g,1,len);ntt(h,1,len);
for (int i=0;i<len;++i) g[i]=1ll*g[i]*h[i]%mod;
ntt(g,-1,len);
printf("%lld\n",1ll*g[n]*jc[n-1]%mod);return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 3e5+5;
const int mod = 998244353;
const int rem = 882049182;
int n,inv[N],jc[N],jcn[N],g[N],f[N],rev[N],og[N];
int fastpow(int a,int b){
int res=1;
while (b) {if (b&1) res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}
return res;
}
void init(int len){
int l=0;while ((1<<l)<len) ++l;
for (int i=0;i<len;++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<l-1);
}
void ntt(int *P,int op,int len){
for (int i=0;i<len;++i) if (i<rev[i]) swap(P[i],P[rev[i]]);
for (int i=1;i<len;i<<=1){
int W=fastpow(3,(mod-1)/(i<<1));
if (op==-1) W=fastpow(W,mod-2);
og[0]=1;for (int j=1;j<i;++j) og[j]=1ll*og[j-1]*W%mod;
for (int p=i<<1,j=0;j<len;j+=p)
for (int k=0;k<i;++k){
int x=P[j+k],y=1ll*og[k]*P[j+k+i]%mod;
P[j+k]=(x+y)%mod;P[j+k+i]=(x-y+mod)%mod;
}
}
if (op==-1) for (int i=0,Inv=fastpow(len,mod-2);i<len;++i) P[i]=1ll*P[i]*Inv%mod;
}
int s1[N];
void Get_Inv(int *a,int *b,int len){
if (len==1) {b[0]=fastpow(a[0],mod-2)%mod;return;}
Get_Inv(a,b,len>>1);
for (int i=0;i<len;++i) s1[i]=a[i];
init(len<<1);ntt(s1,1,len<<1);ntt(b,1,len<<1);
for (int i=0;i<len<<1;++i) s1[i]=(2ll*b[i]-1ll*b[i]*b[i]%mod*s1[i]%mod+mod)%mod;
ntt(s1,-1,len<<1);
for (int i=0;i<len;++i) b[i]=s1[i],b[i+len]=s1[i]=s1[i+len]=0;
}
void Get_Der(int *a,int *b,int len){
for (int i=1;i<len;++i) b[i-1]=1ll*i*a[i]%mod;
b[len-1]=0;
}
void Get_Int(int *a,int *b,int len){
for (int i=1;i<len;++i) b[i]=1ll*inv[i]*a[i-1]%mod;
b[0]=0;
}
int s2[N],s3[N];
void Get_ln(int *a,int *b,int len){
Get_Der(a,s2,len);Get_Inv(a,s3,len);
init(len<<1);ntt(s2,1,len<<1);ntt(s3,1,len<<1);
for (int i=0;i<len<<1;++i) s2[i]=1ll*s2[i]*s3[i]%mod;
ntt(s2,-1,len<<1);Get_Int(s2,b,len);
for (int i=0;i<len<<1;++i) s2[i]=s3[i]=0;
}
int main(){
scanf("%d",&n);g[0]=jc[0]=jcn[0]=1;
for (int i=1,Inv=fastpow(rem,mod-2);i<=n;++i){
inv[i]=i==1?1:1ll*inv[mod%i]*(mod-mod/i)%mod;
jc[i]=1ll*jc[i-1]*i%mod,jcn[i]=fastpow(jc[i],mod-2);
g[i]=1ll*(i&1?mod-1:1)*fastpow(Inv,1ll*i*i%(mod-1))%mod*jcn[i]%mod;
}
int len=1;while (len<=n) len<<=1;
Get_Inv(g,f,len);
for (int i=0;i<=n;++i) f[i]=1ll*f[i]*fastpow(rem,1ll*i*i%(mod-1))%mod;
for (int i=n+1;i<len;++i) f[i]=0;
memset(g,0,sizeof(g));Get_ln(f,g,len);
printf("%lld\n",1ll*g[n]*jc[n]%mod);
return 0;
}

posted @ 2018-12-06 16:06  租酥雨  阅读(883)  评论(1编辑  收藏