我是抄的 orzz
QAQ_bipartite_one
∑k=0nCkn∗2(n−k)∗k
注意这里是 2(n−k)∗k 不是 2(n−k)+k 怎么卷?
2(n−k)∗k=(2√)n2(2√)k2∗(2√)(n−k)2
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
const int P=998244353;
const int G=3;
const int SQRT=116195171;
const int INV=(P+1)>>1;
const int N=300005;
inline ll Pow(ll a,int b){
ll ret=1; for (;b;b>>=1,a=a*a%P) if (b&1) ret=ret*a%P; return ret;
}
inline ll Inv(ll a){
return Pow(a,P-2);
}
ll fac[N];
inline void Init(int n){
fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%P;
}
int num;
int w[2][N];
inline void Pre(int m){
num=m;
int g=Pow(G,(P-1)/m);
w[0][0]=w[1][0]=1;
for (int i=1;i<num;i++) w[1][i]=(ll)w[1][i-1]*g%P;
for (int i=1;i<num;i++) w[0][i]=w[1][num-i];
}
int R[N];
inline void FFT(int n,int *a,int r){
for (int i=0;i<n;i++) if (i<R[i]) swap(a[i],a[R[i]]);
for (int i=1;i<n;i<<=1)
for (int j=0;j<n;j+=(i<<1))
for (int k=0;k<i;k++){
int x=a[j+k],y=(ll)a[j+k+i]*w[r][num/(i<<1)*k]%P;
a[j+k]=(x+y)%P; a[j+i+k]=(x+P-y)%P;
}
if (!r) for (int i=0,inv=Pow(n,P-2);i<n;i++) a[i]=(ll)a[i]*inv%P;
}
int n,m;
int a[N];
int main(){
freopen("QAQ_bipartite_one.in","r",stdin);
freopen("QAQ_bipartite_one.out","w",stdout);
scanf("%d",&n); Init(n);
int L=0; m=1;
while (m<=(n<<1)) m<<=1,L++;
for (int i=1;i<m;i++) R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
Pre(m);
for (int i=0;i<=n;i++)
a[i]=Inv(fac[i]*Pow(SQRT,(ll)i*i%(P-1))%P);
FFT(m,a,1);
for (int i=0;i<m;i++) a[i]=(ll)a[i]*a[i]%P;
FFT(m,a,0);
printf("%d\n",(ll)a[n]*fac[n]%P*Pow(SQRT,(ll)n*n%(P-1))%P);
return 0;
}
QAQ_bipartite_two
设上一题的也就是有色的指数生成函数是
F(x)
,这题的无色的指数生成函数为
G(x)
,无色的强制必须连通的指数生成函数是
H(x)
,那么
G=eH
F=∑i=0∞2iHii!=e2H
因为 k 个联通块染色会有
所以 F=G2 直接多项式开根
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int P=998244353;
const int G=3;
const int SQRT=116195171;
const int INV=(P+1)>>1;
const int N=600005;
inline ll Pow(ll a,int b){
ll ret=1; for (;b;b>>=1,a=a*a%P) if (b&1) ret=ret*a%P; return ret;
}
inline int Root(int a){
if (a==1) return 1;
}
inline ll Inv(ll a){
return Pow(a,P-2);
}
ll fac[N];
inline void Init(int n){
fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%P;
}
int num;
int w[2][N];
inline void Pre(int m){
num=m;
int g=Pow(G,(P-1)/m);
w[0][0]=w[1][0]=1;
for (int i=1;i<num;i++) w[1][i]=(ll)w[1][i-1]*g%P;
for (int i=1;i<num;i++) w[0][i]=w[1][num-i];
}
int R[N];
inline void FFT(int *a,int n,int r){
for (int i=0;i<n;i++) if (i<R[i]) swap(a[i],a[R[i]]);
for (int i=1;i<n;i<<=1)
for (int j=0;j<n;j+=(i<<1))
for (int k=0;k<i;k++){
int x=a[j+k],y=(ll)a[j+k+i]*w[r][num/(i<<1)*k]%P;
a[j+k]=(x+y)%P; a[j+i+k]=(x+P-y)%P;
}
if (!r) for (int i=0,inv=Pow(n,P-2);i<n;i++) a[i]=(ll)a[i]*inv%P;
}
inline void GetInv(int *a,int *b,int n){
static int tmp[N];
if (n==1) return void(b[0]=Inv(a[0]));
GetInv(a,b,n>>1);
for (int i=0;i<n;i++) tmp[i]=a[i],tmp[n+i]=0;
int L=0; while (!(n>>L&1)) L++;
for (int i=1;i<(n<<1);i++) R[i]=(R[i>>1]>>1)|((i&1)<<L);
FFT(tmp,n<<1,1); FFT(b,n<<1,1);
for (int i=0;i<(n<<1);i++)
tmp[i]=(ll)b[i]*(2+P-(ll)tmp[i]*b[i]%P)%P;
FFT(tmp,n<<1,0);
for (int i=0;i<n;i++) b[i]=tmp[i],b[n+i]=0;
}
const int INV2=(P+1)/2;
inline void GetRoot(int *a,int *b,int n){
static int tmp[N],invb[N];
if (n==1) return void(b[0]=Root(a[0]));
GetRoot(a,b,n>>1);
memset(invb,0,sizeof(int)*n); GetInv(b,invb,n);
int L=0; while (!(n>>L&1)) L++;
for (int i=1;i<(n<<1);i++) R[i]=(R[i>>1]>>1)|((i&1)<<L);
for (int i=0;i<n;i++) tmp[i]=a[i],tmp[n+i]=0;
FFT(tmp,n<<1,1); FFT(b,n<<1,1); FFT(invb,n<<1,1);
for (int i=0;i<(n<<1);i++)
tmp[i]=(ll)((ll)b[i]*b[i]+tmp[i])%P*invb[i]%P*INV2%P;
FFT(tmp,n<<1,0);
for (int i=0;i<n;i++) b[i]=tmp[i],b[n+i]=0;
}
int n,m;
int a[N],b[N];
ll pw[N];
int main(){
freopen("QAQ_bipartite_two.in","r",stdin);
freopen("QAQ_bipartite_two.out","w",stdout);
scanf("%d",&n); Init(n);
int L=0; m=1;
while (m<=(n<<1)) m<<=1,L++;
for (int i=1;i<m;i++) R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
Pre(m<<1);
for (int i=0;i<=n;i++)
a[i]=Inv(fac[i]*(pw[i]=Pow(SQRT,(ll)i*i%(P-1)))%P);
FFT(a,m,1);
for (int i=0;i<m;i++) a[i]=(ll)a[i]*a[i]%P;
FFT(a,m,0);
for (int i=0;i<=n;i++) a[i]=a[i]*pw[i]%P;
for (int i=n+1;i<m;i++) a[i]=0;
m=1; while (m<=n) m<<=1;
GetRoot(a,b,m);
printf("%d",(ll)b[n]*fac[n]%P);
return 0;
}
QAQ_bipartite_thr
F=e2H 多项式ln
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int P=998244353;
const int G=3;
const int SQRT=116195171;
const int INV2=(P+1)>>1;
const int N=600005;
inline ll Pow(ll a,int b){
ll ret=1; for (;b;b>>=1,a=a*a%P) if (b&1) ret=ret*a%P; return ret;
}
inline int Root(int a){
if (a==1) return 1;
}
inline ll Inv(ll a){
return Pow(a,P-2);
}
ll fac[N],inv[N];
inline void Init(int n){
fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%P;
inv[1]=1; for (int i=2;i<=n;i++) inv[i]=(ll)(P-P/i)*inv[P%i]%P;
inv[0]=1; for (int i=1;i<=n;i++) inv[i]=inv[i-1]*inv[i]%P;
}
int num;
int w[2][N];
inline void Pre(int m){
num=m;
int g=Pow(G,(P-1)/m);
w[0][0]=w[1][0]=1;
for (int i=1;i<num;i++) w[1][i]=(ll)w[1][i-1]*g%P;
for (int i=1;i<num;i++) w[0][i]=w[1][num-i];
}
int R[N];
inline void FFT(int *a,int n,int r){
for (int i=0;i<n;i++) if (i<R[i]) swap(a[i],a[R[i]]);
for (int i=1;i<n;i<<=1)
for (int j=0;j<n;j+=(i<<1))
for (int k=0;k<i;k++){
int x=a[j+k],y=(ll)a[j+k+i]*w[r][num/(i<<1)*k]%P;
a[j+k]=(x+y)%P; a[j+i+k]=(x+P-y)%P;
}
if (!r) for (int i=0,inv=Pow(n,P-2);i<n;i++) a[i]=(ll)a[i]*inv%P;
}
inline void GetInv(int *a,int *b,int n){
static int tmp[N];
if (n==1) return void(b[0]=Inv(a[0]));
GetInv(a,b,n>>1);
for (int i=0;i<n;i++) tmp[i]=a[i],tmp[n+i]=0;
int L=0; while (!(n>>L&1)) L++;
for (int i=1;i<(n<<1);i++) R[i]=(R[i>>1]>>1)|((i&1)<<L);
FFT(tmp,n<<1,1); FFT(b,n<<1,1);
for (int i=0;i<(n<<1);i++)
tmp[i]=(ll)b[i]*(2+P-(ll)tmp[i]*b[i]%P)%P;
FFT(tmp,n<<1,0);
for (int i=0;i<n;i++) b[i]=tmp[i],b[n+i]=0;
}
int n,m;
int a[N],b[N],c[N],f[N];
ll pw[N];
int main(){
freopen("QAQ_bipartite_thr.in","r",stdin);
freopen("QAQ_bipartite_thr.out","w",stdout);
scanf("%d",&n); Init(n);
int L=0; m=1;
while (m<=(n<<1)) m<<=1,L++;
for (int i=1;i<m;i++) R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
Pre(m<<1);
for (int i=0;i<=n;i++)
a[i]=Inv(fac[i]*(pw[i]=Pow(SQRT,(ll)i*i%(P-1)))%P);
FFT(a,m,1);
for (int i=0;i<m;i++) a[i]=(ll)a[i]*a[i]%P;
FFT(a,m,0);
for (int i=0;i<=n+1;i++) f[i]=(ll)a[i]*pw[i]%P*fac[i]%P,a[i]=(ll)a[i]*pw[i]%P;
for (int i=0;i<=n;i++) c[i]=f[i+1]*inv[i]%P;
for (int i=n+1;i<=m;i++) a[i]=0;
GetInv(a,b,m>>1);
FFT(b,m,1); FFT(c,m,1);
for (int i=0;i<m;i++) b[i]=(ll)c[i]*b[i]%P;
FFT(b,m,0);
printf("%d\n",(ll)b[n-1]*fac[n-1]%P*INV2%P);
return 0;
}