第一问:求每个点度数不超过 4 的无根树数量
第二问:求每个点儿子数不超过 3 的有根树数量
首先第二个问相对简单,移步这里 传送门
其次写这道题之前应先膜拜 https://blog.csdn.net/zxyoi_dreamer/article/details/97561441
假设第一个问求出的东西的生成函数是 A ( x ) A(x) A(x),我们开始推导
我们发现首先可以分为两个大类分别统计答案
一个是有对称边的,一个是无对称边的
对称边是指删掉某一条边后两边的子树同构
我们将对称边个数即为
e
∈
[
0
,
1
]
e\in[0,1]
e∈[0,1],点的等价类个数记为
c
c
c,边的等价类个数记为
d
d
d
我们对
e
e
e 讨论
- 当 e = 0 e=0 e=0 的时候, 即无对称边,任选一个重心为根转化为有根树,显然这个根独自成为一个点等价类,对于剩下所有的点,它们的父亲边在同一个边等价类中当且仅当它们在同一个点等价类中, c − d = 1 c-d=1 c−d=1
- 当 e = 1 e=1 e=1 的时候,在中间的边上插入一个点作为根,剩下的每一个点等价类对应一个边的等价类, c − d = 0 c-d=0 c−d=0
其中点等价类指的是把一类点作为根形成的数都同构的那些点
其中边等价类删去一类边形成的两个子树都同构的那些边
于是我们发现任意一棵树
c
−
d
+
e
=
1
c-d+e=1
c−d+e=1
于是我们对
∑
c
,
∑
d
,
∑
e
\sum c,\sum d,\sum e
∑c,∑d,∑e 单独求
令 e e e 的生成函数为 E ( x ) E(x) E(x),那么 E ( x ) = A ( x 2 ) E(x)=A(x^2) E(x)=A(x2),两边同构
令
d
d
d 的生成函数为
D
(
x
)
D(x)
D(x),发现就是统计一条边两边本质不同的子树个数
两个端点的子树不能为空,考虑用
B
u
r
n
s
i
d
e
Burnside
Burnside 来计数,分别算交换和不交换的方案数,如下
D ( x ) = ( A ( x ) − 1 ) 2 + ( A ( x 2 ) − 1 ) 2 D(x)=\frac{(A(x)-1)^2+(A(x^2)-1)}{2} D(x)=2(A(x)−1)2+(A(x2)−1)
令
c
c
c 的生成函数为
C
(
x
)
C(x)
C(x),就是统计一个点作为根的本质不同的树,考虑它的四个儿子的旋转变化
继续用
B
u
r
n
s
i
d
e
Burnside
Burnside,对 24 中排列讨论可得:
C
(
x
)
=
x
∗
A
(
x
)
4
+
6
∗
A
(
x
2
)
A
(
x
)
2
+
3
∗
A
(
x
2
)
2
+
8
∗
A
(
x
3
)
A
(
x
)
+
6
∗
A
(
x
4
)
24
C(x)=x*\frac{A(x)^4+6*A(x^2)A(x)^2+3*A(x^2)^2+8*A(x^3)A(x)+6*A(x^4)}{24}
C(x)=x∗24A(x)4+6∗A(x2)A(x)2+3∗A(x2)2+8∗A(x3)A(x)+6∗A(x4)
感觉有点像 点数 - 边数 = 1 的那个套路,这样就可以对点边的贡献分开算了
因为常数过大 T L E TLE TLE 了
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#define cs const
using namespace std;
cs int Mod = 998244353;
int add(int a, int b){ return a + b >= Mod ? a + b - Mod : a + b; }
int mul(int a, int b){ return 1ll * a * b % Mod; }
int dec(int a, int b){ return a - b < 0 ? a - b + Mod : a - b; }
void Add(int &a, int b){ a = add(a, b); }
int ksm(int a, int b){ int ans = 1; for(;b;b>>=1,a=mul(a,a)) if(b&1) ans = mul(ans, a); return ans; }
typedef vector<int> poly;
cs int inv2 = ksm(2, Mod-2), inv6 = ksm(6, Mod-2), inv24 = ksm(24, Mod-2);
int T, n, up, bit; poly rev;
void init(int deg){
up = 1; bit = 0; while(up < deg) up <<= 1, ++bit; rev.resize(up);
for(int i = 0; i < up; i++) rev[i] = (rev[i>>1]>>1)|((i&1)<<(bit-1));
}
poly operator + (poly a, poly b){
int deg = max(a.size(), b.size()); a.resize(deg); b.resize(deg);
for(int i = 0; i < deg; i++) a[i] = add(a[i], b[i]); return a;
}
poly operator - (poly a, poly b){
int deg = max(a.size(), b.size()); a.resize(deg); b.resize(deg);
for(int i = 0; i < deg; i++) a[i] = dec(a[i], b[i]); return a;
}
poly operator * (poly a, int b){ for(int i = 0; i < (int)a.size(); i++) a[i] = mul(a[i], b); return a; }
void NTT(poly &a, int typ){
for(int i = 0; i < up; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
for(int i = 1; i < up; i <<= 1){
int wn = ksm(3, (Mod-1)/(i<<1));
for(int j = 0; j < up; j += (i<<1))
for(int k = 0, w = 1; k < i; k++, w = mul(w, wn)){
int x = a[k + j], y = mul(w, a[k + j + i]);
a[k + j] = add(x, y); a[k + j + i] = dec(x, y);
}
} if(typ == -1){
reverse(a.begin() + 1, a.end());
for(int i = 0, inv = ksm(up, Mod-2); i < up; i++) a[i] = mul(a[i], inv);
}
}
poly operator * (poly a, poly b){
int deg = a.size() + b.size() - 1; init(deg);
a.resize(up); b.resize(up); NTT(a, 1); NTT(b, 1);
for(int i = 0; i < up; i++) a[i] = mul(a[i], b[i]);
NTT(a, -1); a.resize(deg); return a;
}
poly inv(poly a){
int deg = a.size(); poly b(1, ksm(a[0], Mod - 2)), c;
for(int len = 4; len <= (deg << 2); len <<= 1){
c = a; c.resize(len >> 1);
init(len); c.resize(up); b.resize(up); NTT(b, 1); NTT(c, 1);
for(int i = 0; i < up; i++) b[i] = mul(b[i], dec(2, mul(b[i], c[i])));
NTT(b, -1); b.resize(len >> 1);
} b.resize(deg); return b;
}
poly Get(poly a, int coef, int len){
poly b; b.resize(len);
for(int i = 0; i < len; i++){
if(i * coef < len) b[i * coef] = a[i];
} return b;
}
poly Newton(int deg){
poly b; b.push_back(1);
for(int len = 2; len <= (deg << 1); len <<= 1){
poly c = b * b * b + Get(b, 2, len) * b * 3 + Get(b, 3, len) * 2;
c = c * inv6; c.insert(c.begin(), 0);
c.resize(len); ++c[0]; c = c - b;
poly d = b * b * 3 + Get(b, 2, len) * 3;
d = d * inv6; d.insert(d.begin(), 0);
d.resize(len); d[0] = dec(d[0], 1);
b = b - c * inv(d); b.resize(len);
} return b;
}
poly A, B;
void work(int n){
A = Newton(n); A.resize(n);
poly A2 = Get(A, 2, n);
poly A3 = Get(A, 3, n);
poly A4 = Get(A, 4, n);
B = A2; B.resize(n);
A[0] = dec(A[0], 1); A2[0] = dec(A2[0], 1);
poly D = (A * A + A2) * inv2 ; D.resize(n);
Add(A[0], 1); Add(A2[0], 1);
init(n << 2);
A.resize(up); A2.resize(up); A3.resize(up); A4.resize(up);
NTT(A, 1); NTT(A2, 1); NTT(A3, 1); NTT(A4, 1);
poly C; C.resize(up);
for(int i = 0; i < up; i++){
C[i] = mul(mul(A[i], A[i]), mul(A[i], A[i]));
Add(C[i], mul(6, mul(A2[i], mul(A[i], A[i]))));
Add(C[i], mul(3, mul(A2[i], A2[i])));
Add(C[i], mul(8, mul(A[i], A3[i])));
Add(C[i], mul(6, A4[i])); C[i] = mul(C[i], inv24);
}
NTT(C, -1); C.insert(C.begin(), 0); C.resize(n);
NTT(A, -1);
B = B + C - D;
}
int main(){
work(1e5 + 5);
scanf("%d", &T);
while(T--){
scanf("%d", &n);
cout << B[n] << " " << A[n] << '\n';
} return 0;
}