Loading

P5860 「SWTR-03」Counting Trees

P5860 「SWTR-03」Counting Trees

supercalifragilisticexpialidocious 好词,记住了。 瞬间忘掉

首先得是一颗树,那么 度数和=2*边数=2*点数-2

所以 \(\sum 2-v_i=2\)

一个点权为 \(v\) 的点的生成函数设为 \(1+x^{2-v_i}\) ,都卷起来求 \([x^2]\) 就好了???

事实并不那么美好。。。\(v_i>2\) 的时候系数加哪里去啊?

忽然想到一个叫做“分式域”的东西,之前在command_block的blog里翻到过的,这个负指数感觉有点关系,赶紧去翻了翻。

好像也没啥特殊的,照常运算即可,只不过把多项式偏移一下计算。

为了方便把上面的指数全部取负号,因为这样不用考虑太多负指数。

也就是我们要求

\[[x^{-2}]\prod 1+x^{v_i-2} \]

注意到指数为负数当且仅当 \(v_i=1\) ,那暴力分讨吧。

  • 对于所有 \(v_i>1\) ,取 \(\ln\)\(\exp\) 都第 3 次了我也算是学会了

(这里顺着做题思路写,所以是 \(v_i>1\) ,后面有补充)

考虑 \(\ln(1-x) =-\sum\limits_{i=1}\dfrac{x^i}{i}\) 这个式子,往里面套得到

\[\ln(1+x^v)=-\sum_{i=1}\dfrac{(-x^v)^i}{i} \]

统计一下每一种 \(v\) 的个数强行加就是 \(O(n\ln n)\)

设这个东西 \(\exp\) 回去是 \(F(x)\)

  • 忽然发现 \(v_i=2(v=0)\) 也得判,因为这玩意会无限加下去。设有 \(cnt_2\)\(v_i=2\) ,答案最后乘上 \(2^{cnt_2}\) 即可

  • 最不习惯的 \(-1\) 次幂。

统计 \(v_i=1\) 的个数设为 \(cnt_1\)

忽然发现 \(ans=\sum_\limits{i=2}^{cnt_1}\dbinom{cnt_1}{i}F[i-2]\) ,直接撒花!

因为 \(F(x)\) 只会卷出 \(\ge 0\) 的指数,所以 \(i\)\(2\) 开始加。由于必须退掉 \(i+2\) 次幂才能得到 \(-2\) 次幂,所以就是上面那个式子啦!

事实上,如果条件没有这么特殊,不能用组合方法,可以暴力卷,下标什么的推一推就能求出 \([x^{-n}]\) 了(口胡ing)

手残把除 \(i\) 写成除 \(i!\) 调了一年

/*

    |-------                                                    |-----|                                   
    |                                                           |     | 
    |                                                           |     |
    |                                                           |     |                             *    |
    |------ |----|   ---  -----  \       / -----   ---          |-----| |    |   ---  ----- |    |     --|--
    |       |    | |/   \ |   |   \     /  |   | |/   \         |       |    | |/   \ |     |    |  |    |  
    |       |    | |      -----    \   /   ----- |              |       |    | |      |---- |    |  |    |
    |       |    | |      |         \ /    |     |              |       |    | |          | |    |  |    |
    |       |----| |      ----/      -     ---/  |      _____   |       |----| |      ----| |----|  |    --/


    \       /  \       / |----\     ------
     \     /    \     /  |     \    |
      \   /      \   /   |      \   |
       \-/        \-/    |      |   ------
        |          |     |      |        |
        |          |     |      /        |
        |          |     |     /         |
        |          |     |----/     ------


*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mkp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define sz(v) (int)v.size()
typedef long long LL;
typedef double db;
template<class T>bool ckmax(T&x,T y){return x<y?x=y,1:0;}
template<class T>bool ckmin(T&x,T y){return x>y?x=y,1:0;}
#define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i)
#define per(i,x,y) for(int i=x,i##end=y;i>=i##end;--i)
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}

const int N=500005;
const int M=N<<2;
#define mod 998244353
int n,f[M],g[M],ans,cnt[N];

namespace math{
int fac[N],ifc[N],inv[N];
inline int qpow(int n,int k){int res=1;for(;k;k>>=1,n=1ll*n*n%mod)if(k&1)res=1ll*n*res%mod;return res;}
inline void fmod(int&x){x-=mod,x+=x>>31&mod;}
inline int comb(int n,int m){return n<m?0:1ll*fac[n]*ifc[m]%mod*ifc[n-m]%mod;}
void initmath(const int&n=N-5){
	fac[0]=1;for(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%mod;
	ifc[n]=qpow(fac[n],mod-2);for(int i=n-1;i>=0;--i)ifc[i]=1ll*(i+1)*ifc[i+1]%mod;
	inv[1]=1;for(int i=2;i<=n;++i)inv[i]=1ll*inv[mod%i]*(mod-mod/i)%mod;
}
}
using math::qpow;
using math::fmod;

namespace poly{
int rev[M],lg,lim;
void init_poly(const int&n){
	for(lg=0,lim=1;lim<n;lim<<=1,++lg);
	for(int i=0;i<lim;++i)rev[i]=(rev[i>>1]>>1)|((i&1)<<(lg-1));
}
void NTT(int*a,int op){
	for(int i=0;i<lim;++i)
		if(i>rev[i])swap(a[i],a[rev[i]]);
	const int g=op?3:math::inv[3];
	for(int i=1;i<lim;i<<=1){
		const int wn=qpow(g,(mod-1)/(i<<1));
		for(int j=0;j<lim;j+=i<<1){
			int w0=1;
			for(int k=0;k<i;++k,w0=1ll*w0*wn%mod){
				const int X=a[j+k],Y=1ll*w0*a[i+j+k]%mod;
				fmod(a[j+k]=X+Y),fmod(a[i+j+k]=X-Y+mod);
			}
		}
	}
	if(op)return;const int ilim=qpow(lim,mod-2);
	for(int i=0;i<lim;++i)a[i]=1ll*ilim*a[i]%mod;
}
#define clr(a,n) memset(a,0,sizeof(int)*(n))
#define cpy(a,b,n) memcpy(a,b,sizeof(int)*(n))
void poly_mul(int*f,int*g,int*ans,int n,int m){
	static int A[M],B[M];init_poly(n+m);
	cpy(A,f,n),clr(A+n,lim-n),NTT(A,1);
	cpy(B,g,m),clr(B+m,lim-n),NTT(B,1);
	for(int i=0;i<lim;++i)ans[i]=1ll*A[i]*B[i]%mod;
	NTT(ans,0);
}
void poly_inv(int*g,int*f,int n){
	static int A[M];
	if(n==1)return g[0]=qpow(f[0],mod-2),void();
	poly_inv(g,f,(n+1)>>1),init_poly(n<<1);
	cpy(A,f,n),clr(A+n,lim-n),clr(g+n,lim-n);
	NTT(A,1),NTT(g,1);
	for(int i=0;i<lim;++i)g[i]=1ll*g[i]*(2-1ll*A[i]*g[i]%mod+mod)%mod;
	NTT(g,0),clr(g+n,lim-n);
}
void dao(int*g,int*f,int n){
	for(int i=0;i<n-1;++i)g[i]=1ll*(i+1)*f[i+1]%mod;g[n-1]=0;
}
void jif(int*g,int*f,int n){
	for(int i=1;i<=n;++i)g[i]=1ll*f[i-1]*math::inv[i]%mod;g[0]=0;
}
void poly_ln(int*g,int*f,int n){
	static int A[M],B[M];
	clr(A,n),poly_inv(A,f,n),dao(B,f,n),poly_mul(A,B,A,n,n),jif(g,A,n);
}
void poly_exp(int*g,int*f,int n){
	static int A[M];
	if(n==1)return g[0]=1,void();
	poly_exp(g,f,(n+1)>>1);
	clr(A,n),poly_ln(A,g,n);
	for(int i=1;i<n;++i)fmod(A[i]=f[i]+mod-A[i]);A[0]=1;
	poly_mul(A,g,g,n,n),clr(g+n,lim-n);
}

}

signed main(){
	math::initmath(),n=read();
	rep(i,1,n)++cnt[read()];
	for(int i=1;i<cnt[1];++i)
		for(int j=1;i*j<cnt[1];++j)fmod(f[i*j]+=1ll*(j&1?cnt[i+2]:mod-cnt[i+2])*math::inv[j]%mod);
	poly::poly_exp(g,f,cnt[1]);
	for(int i=2;i<=cnt[1];++i)fmod(ans+=1ll*math::comb(cnt[1],i)*g[i-2]%mod);
	ans=1ll*ans*qpow(2,cnt[2])%mod,printf("%d\n",ans);
	return 0;
}

多亏了码头保佑,我暂时最优解(

posted @ 2021-01-07 20:50  zzctommy  阅读(118)  评论(3编辑  收藏  举报