跳到正文

个人记录

DIV3 7.14 斯特林数、卡特兰数 预习&做题记录

NoneOfTheAbove约 24 分钟
原文
文章信息与版本

文章信息

收录
2026年7月10日
更新
2026年7月15日
篇幅
约 24 分钟

版本

  1. v3当前
  2. v2
  3. v1

卡特兰数

卡特兰数有很多表示方法,其中一个是:

如图,一个 n∗nn*n 的网格,起点为 (0,0)(0,0),终点为 (n,n)(n,n),不能越过 y=xy=x 这条直线,每次只能向上或向右一个点,求从起点走到终点的方案数。

显然,如果没有 y=xy=x 的限制,方案数为 C2×nnC_{2 \times n}^{n}。

但是有这个限制,所以减去不合法的方案数。因为越过了 y=xy=x。

设路径第一次交 y=x+k+1y=x+k+1 于 PP,将 O→PO \to P 的路径沿 y=x+1y=x+1 翻折,到 O′→PO' \to P,其中 O′(−1,1)O'(-1,1),翻折上去时向上变成向右,向右变成向上,所以就变成了起点为 (−1,1)(-1,1),终点为 (n,n)(n,n) 的方案数,也就是 C2×nn−1C_{2 \times n}^{n-1}。

综上,C2×nn−C2×nn−1C_{2 \times n}^{n} - C_{2 \times n}^{n-1} 是合法方案数,Cn=C2×nn−C2×nn−1C_n=C_{2 \times n}^{n} - C_{2 \times n}^{n-1}。

将 CnC_n 变形:

Cn=C2×nn−C2×nn−1=(2×n)!n!×n!−(2×n)!(n−1)!×(n+1)!=(2×n)!n!×n!−(2×n)!n!×n!×n+1n=(2×n)!n!×n!−(2×n)!n!×n!×nn+1=(2×n)!n!×n!×(1−nn+1)=1n+1×(2×n)!n!×n!\begin{aligned} C_n &= C_{2 \times n}^{n} - C_{2 \times n}^{n-1}\\ &= \frac{(2 \times n)!}{n! \times n!} - \frac{(2 \times n)!}{(n-1)! \times (n+1)!}\\ &= \frac{(2 \times n)!}{n! \times n!} - \frac{(2 \times n)!}{n! \times n! \times \frac{n+1}{n}}\\ &= \frac{(2 \times n)!}{n! \times n!} - \frac{(2 \times n)!}{n! \times n!} \times \frac{n}{n+1}\\ &= \frac{(2 \times n)!}{n! \times n!} \times (1 - \frac{n}{n+1})\\ &= \frac{1}{n+1} \times \frac{(2 \times n)!}{n! \times n!} \end{aligned}

所以 Cn=1n+1×(2×n)!n!×n!=C2×nnn+1C_n=\frac{1}{n+1} \times \frac{(2 \times n)!}{n! \times n!}=\frac{C_{2 \times n}^{n}}{n+1}。

还有一种变形方式,Cn=1n+1×(2×n)!n!×n!=(2×n)!n!×n!×(n+1)=(2×n)!n!×(n+1)!C_n=\frac{1}{n+1} \times \frac{(2 \times n)!}{n! \times n!} = \frac{(2 \times n)!}{n! \times n! \times (n+1)} = \frac{(2 \times n)!}{n! \times (n+1)!}。

因为我做不了图,所以就借一下 S_Gloria 的图。

这些都是卡特兰数的例子,接下来会一一证明。

1

将向上视为出栈,向右视为入栈,就变成了 2,所以是卡特兰数。

同时,设在压入第 i+1i+1 个数前,前 ii 个数全都出栈了,则 Cn←Cn+Ci×Cn−i−1C_n \gets C_n + C_i \times C_{n-i-1},所以 Cn+1=∑i=0nCi×Cn−iC_{n+1} = \sum\limits_{i=0}^{n} C_i \times C_{n-i}。

3

设第 jj 个数为根结点,如果 j=0j=0,那么是空树,则 cntn=∑i=1ncnti−1×cntn−icnt_n=\sum\limits_{i=1}^{n} cnt_{i-1} \times cnt_{n-i},所以 cntn+1=∑i=0ncnti×cntn−i=Cn+1cnt_{n+1} = \sum\limits_{i=0}^{n} cnt_i \times cnt_{n-i}=C_{n+1},所以是卡特兰数。

4

注:这里有 2×n+12 \times n + 1 个节点。

设左子树有 2×i+12 \times i + 1 个节点,右子树有 2×(n−i−1)+12 \times (n-i-1) + 1 个节点,所以 cntn=∑i=0n−1cnti×cntn−i−1=Cncnt_n=\sum\limits_{i=0}^{n-1} cnt_i \times cnt_{n-i-1}=C_n。

5

设 1→2×k1 \to 2 \times k,所以左边要有 2×(k−1)2 \times (k - 1) 个点,右边要有 2×(n−k)2 \times (n - k),所以 cntn=∑i=1ncnti−1×cntn−i=Cncnt_n = \sum\limits_{i=1}^{n} cnt_{i-1} \times cnt_{n-i} = C_n。

6

用处理括号序列的方法,( 是入栈,) 是出栈,变成 1。

所以,卡特兰数的公式有:

Cn={C2×nn−C2×nn−1C2×nnn+1(2×n)!n!×(n+1)!∑i=0n−1Ci×Cn−iC_n=\begin{cases} C_{2 \times n}^{n} - C_{2 \times n}^{n-1}\\ \frac{C_{2 \times n}^{n}}{n+1}\\ \frac{(2 \times n)!}{n! \times (n+1)!}\\ \sum\limits_{i=0}^{n-1} C_i \times C_{n-i} \end{cases}

斯特林数

斯特林数有两类,第一类斯特林数和第二类斯特林数。

第一类斯特林数

记 [nm]\begin{bmatrix} n \\ m\end{bmatrix} 将 nn 个不同的数组成 mm 个圆排列的方案数。

有两种情况:第 nn 个单组一个圆排列,第 nn 个放到其它圆排列上。

nn 单组一个圆排列

此时前 n−1n-1 个数组成 m−1m-1 个圆排列,[nm]←[nm]+[n−1m−1]\begin{bmatrix} n \\ m\end{bmatrix} \gets \begin{bmatrix} n \\ m\end{bmatrix} + \begin{bmatrix} n-1 \\ m-1\end{bmatrix}。

nn 放到前面的圆排列里

此时前 n−1n-1 个数组成 mm 个圆排列,第 nn 个数可以放到前 n−1n-1 个数的其中一个前面,[nm]←[nm]+[n−1m]×(n−1)\begin{bmatrix} n \\ m\end{bmatrix} \gets \begin{bmatrix} n \\ m\end{bmatrix} + \begin{bmatrix} n-1 \\ m\end{bmatrix} \times (n-1)。

综上,[nm]=[n−1m−1]+[n−1m]×(n−1)\begin{bmatrix} n \\ m\end{bmatrix} = \begin{bmatrix} n-1 \\ m-1\end{bmatrix} + \begin{bmatrix} n-1 \\ m\end{bmatrix} \times (n-1)

第二类斯特林数

记 {nm}\begin{Bmatrix} n \\ m\end{Bmatrix} 为 nn 个不同的数放到 mm 个集合中的方案数。

同理,有两种情况:第 nn 个数单开一个集合,或者第 nn 个数放到 mm 个集合的其中一个,这个递推式很显然。

综上,{nm}={n−1m−1}+{n−1m}×m\begin{Bmatrix} n \\ m\end{Bmatrix} = \begin{Bmatrix} n-1 \\ m-1\end{Bmatrix} + \begin{Bmatrix} n-1 \\ m\end{Bmatrix} \times m。

卡特兰数练习

#P1192. 树屋

显然高度为 nn 尺的阶梯有 n+2n+2 个顶点,有两个是在最上面/最下面的钢材的顶点,所以剩下的 nn 个顶点肯定要一个钢材一个顶点,要不然就不是矩形了。

给顶点编号(摘抄自 syksykCCC 的题解):

显然,覆盖 O,iO,i 的矩形会分成 N=i−1,N=n−iN=i-1,N=n-i 的两个子问题,所以 cntn=∑i=1ncnti−1×cntn−i=Cn=(2×n)!n!×(n+1)!cnt_n=\sum\limits_{i=1}^{n} cnt_{i-1} \times cnt_{n-i}=C_n=\frac{(2 \times n)!}{n! \times (n+1)!}。

code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,mod=1e9+7;
const i128 Inf=1e35;
ll qpow(ll a,ll b)
{
    if(b==0) return 1;
    if(b==1) return a;
    ll temp=qpow(a,b/2);
    temp=temp*temp%mod;
    if(b%2) temp=temp*a%mod;
    return temp;
}
ll fact[1001],invfact[1001];
void init()
{
    fact[0]=invfact[0]=1;
    for(int i=1;i<=1000;i++) fact[i]=fact[i-1]*i%mod;
    invfact[1000]=qpow(fact[1000],mod-2);
    for(int i=999;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;
}
ll n;
int main()
{
	cin>>n;
    init();
    cout<<(fact[2*n]*invfact[n]%mod)*invfact[n+1]%mod;
}

#P1193. 完美序列

设 a1,3,…,2×n−1a_{1,3,\dots,2 \times n - 1} 是左括号,a2,4,…,2×na_{2,4,\dots,2 \times n} 是右括号,所以就是卡特兰数的第六种形式。

code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=2e6+1,mod=1e9+7;
const i128 Inf=1e35;
ll n,fact[maxn],invfact[maxn];
ll qpow(ll a,ll b)
{
    if(b==0) return 1;
    if(b==1) return a;
    ll temp=qpow(a,b/2);
    temp=temp*temp%mod;
    if(b%2) temp=temp*a%mod;
    return temp;
}
void init()
{
    fact[0]=invfact[0]=1;
    for(int i=1;i<maxn;i++) fact[i]=fact[i-1]*i%mod;
    invfact[maxn-1]=qpow(fact[maxn-1],mod-2);
    for(int i=maxn-2;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;
}
int main()
{
	cin>>n;
    init();
    cout<<(fact[2*n]*invfact[n]%mod)*invfact[n+1]%mod;
}

#P1194. 划分

lg at

显然让 2×i−12 \times i - 1 和 2×i2 \times i 组合最优。

prof.

设 2∣(x−1)2 \mid (x-1)。

x,x+1,x+2,x+3x,x+1,x+2,x+3 中,如果让 x,x+3x,x+3,x+1,x+2x+1,x+2 配对,则贡献 x2+3×x+x2+3×x+2=2×x2+6×x+2x^2 + 3 \times x + x^2 + 3 \times x + 2 = 2 \times x^2 + 6 \times x + 2,如果让 x,x+1x,x+1,x+2,x+3x+2,x+3 配对,则贡献 x2+x+x2+5×x+6=2×x2+6×x+6x^2 + x + x^2 + 5 \times x + 6 = 2 \times x^2 + 6 \times x + 6,证毕。

此时可以把这 nn 对数调换顺序,对于一种 A,BA,B 就有了 n!n! 种方案。

还可以把 Ai,BiA_i,B_i 调换顺序,此时对于一种 (Ai,Bi)(A_i,B_i) 的无序对有了 2n2^n 种方案。

此时,设 (Ci,Di)=(min⁡(Ai,Bi),max⁡(Ai,Bi))(C_i,D_i)=(\min(A_i,B_i),\max(A_i,B_i)),CC 是左括号,DD 是右括号,就是 CnC_n,CC 是卡特兰数。

综上,方案数为 2n×n!×Cn=2n×n!×(2×n)!n!×(n+1)!=2n×(2×n)!(n+1)!2^n \times n! \times C_n=2^n \times n! \times \frac{(2 \times n)!}{n! \times (n+1)!}=2^n \times \frac{(2 \times n)!}{(n+1)!}。

code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=4e5+1,mod=998244353;
const i128 Inf=1e35;
ll n,frac[maxn],invfrac[maxn];
ll qpow(ll a,ll b)
{
    if(b==0) return 1;
    if(b==1) return a;
    ll temp=qpow(a,b/2);
    temp=temp*temp%mod;
    if(b%2) temp=temp*a%mod;
    return temp;
}
void init()
{
    frac[0]=invfrac[0]=1;
    for(int i=1;i<maxn;i++) frac[i]=frac[i-1]*i%mod;
    invfrac[maxn-1]=qpow(frac[maxn-1],mod-2);
    for(int i=maxn-2;i;i--) invfrac[i]=invfrac[i+1]*(i+1)%mod;
}
int main()
{
	cin>>n;
    init();
    cout<<qpow(2,n)*frac[2*n]%mod*invfrac[n+1]%mod;
}

#P1195. 挖矿

lg at

首先结尾要符合条件,所以 n≤m+kn \le m+k,n>m+kn > m+k 的直接输出 00。

设 n=5,m=7,k=3n=5,m=7,k=3。

因为 ai≤bi+ka_i \le b_i + k,所以不能越过 a=b+ka=b+k,设 y=a,x=by=a,x=b,也就是说从 OO 到 EE,不可以越过 y=x+ky=x+k。

因此不合法的方案会经过 y=x+k+1y=x+k+1 的点。

因此,设路径第一次交 y=x+k+1y=x+k+1 于 PP,将 O→PO \to P 沿 y=x+k+1y=x+k+1 翻折成 O′→PO' \to P,则 O′(−k−1,k+1)O'(-k-1,k+1)。

所以不合法的方案数是 C2×nn−k−1C_{2 \times n}^{n-k-1},合法的是 C2×nn−C2×nn−k−1C_{2 \times n}^{n} - C_{2 \times n}^{n-k-1}。

code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=2e6+1,mod=1e9+7;
const i128 Inf=1e35;
ll n,m,k,fact[maxn],invfact[maxn];
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b%2) ans=ans*a%mod;
        b/=2,a=a*a%mod;
    }
    return ans;
}
ll inv(ll x)
{
    return qpow(x,mod-2);
}
void init()
{
    fact[0]=invfact[0]=1;
    for(int i=1;i<maxn;i++) fact[i]=fact[i-1]*i%mod;
    invfact[maxn-1]=inv(fact[maxn-1]);
    for(int i=maxn-2;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;
}
ll C(ll n,ll m)
{
    if(n<m||m<0) return 0;
    return fact[n]*invfact[m]%mod*invfact[n-m]%mod;
}
int main()
{
    cin>>n>>m>>k;
    init();
    if(n>m+k) return cout<<0,0;
    cout<<(C(n+m,m)-C(n+m,m+k+1)+mod)%mod;
}

#P1964. 魔法石

lg at

设施了 RR 次变红魔法,BB 次变蓝魔法。

显然有三种情况:

  • R<BR<B:显然至少一个石头是蓝的,无解;
  • R≥B+nR \ge B + n:显然最优方案可以全部有解,所以就是随便排,CkRC_{k}^{R} 种方案;

所以只剩一种情况:B≤R≤B+nB \le R \le B+n。

贪心可以求出最多有 R−BR-B 个石头被施的变红魔法大于变蓝魔法(也就是只多施一次),其他红蓝相等的要最后施蓝的。

因为一个红蓝相等就是类似 ******RB 的类型,****** 占位,所以丢掉,变成 RB。

此时就是要求出有多少种组合可以找出 n−(R−B)n-(R-B) 个 RB,故:

所有前缀 B 比 R 最多多 B−[n−(R−B)]=R−nB-[n-(R-B)]=R-n。

把 B 视为向上,R 视为向右,从 (0,0)(0,0) 走到 (R,B)(R,B) 不能越过 y=x+(R−n)y=x+(R-n)。

这个是上一题用过的做法,答案是 CkR−Ck2×R−n+1C_{k}^{R}-C_{k}^{2 \times R - n + 1},当然 R≥B+nR \ge B + n 也可以用,因为此时 Ck2×R−n+1=0C_{k}^{2 \times R - n + 1}=0。

做完了吗?并没有。因为当 R=BR=B 的时候,最后一个不一定要 RB,只需要 R,所以 k←k−1k \gets k-1,这时候才做完了。

code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=5e5+1,mod=998244353;
const i128 Inf=1e35;
ll n,k,fact[maxn],invfact[maxn],ans;
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b%2) ans=ans*a%mod;
        a=a*a%mod,b/=2;
    }
    return ans;
}
ll inv(ll x) {return qpow(x,mod-2);}
void init()
{
    fact[0]=invfact[0]=1;
    for(int i=1;i<maxn;i++) fact[i]=fact[i-1]*i%mod;
    invfact[maxn-1]=inv(fact[maxn-1]);
    for(int i=maxn-2;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;
}
ll C(ll n,ll m)
{
    if(n<m||m<0) return 0;
    return fact[n]*invfact[m]%mod*invfact[n-m]%mod;
}
int main()
{
    cin>>n>>k;
    if(k<n) return cout<<0,0;
    init();
    for(int i=0;i<=k;i++)
    {
        if(2*i<k) continue;
        ans=(ans+C(k-(2*i==k),i)-C(k-(2*i==k),2*i-n+1)+mod)%mod;
    }
    cout<<ans;
}

P5014 水の三角(修改版)

显然,如果没有斜边,就是 Cn+mm−Cn+mm−1C_{n+m}^{m}-C_{n+m}^{m-1},其中 n,mn,m 为行号减一,列号减一。

有了斜边也差不多,设走了 xx 条斜边,那么下和右就只剩 N=n−x,M=m−xN=n-x,M=m-x 条了,方案数 CN+MM−CN+MM−1C_{N+M}^{M}-C_{N+M}^{M-1},再乘上斜边的放置方案 CN+M+iiC_{N+M+i}^{i},答案就是:

∑i=0m(CN+MM−CN+MM−1)×CN+M+iiN=n−i,M=m−i\sum\limits_{i=0}^{m} (C_{N+M}^{M}-C_{N+M}^{M-1}) \times C_{N+M+i}^{i}\\ N=n-i,M=m-i
code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=2e6+1,mod=998244353;
const i128 Inf=1e35;
ll t,a,fact[maxn],invfact[maxn];
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b%2) ans=ans*a%mod;
        a=a*a%mod,b/=2;
    }
    return ans;
}
ll inv(ll x)
{
    return qpow(x,mod-2);
}
void init()
{
    fact[0]=invfact[0]=1;
    for(int i=1;i<maxn;i++) fact[i]=fact[i-1]*i%mod;
    invfact[maxn-1]=inv(fact[maxn-1]);
    for(int i=maxn-2;i;i--) invfact[i]=invfact[i+1]*(i+1)%mod;
}
ll C(ll n,ll m)
{
    return fact[n]*invfact[m]%mod*invfact[n-m]%mod;
}
void solve()
{
    cin>>a;
    ll n=sqrt(a);
    while(n*(n+1)/2<a) n++;
    ll m=a-n*(n-1)/2,ans=0;
    n--,m--;
    for(int i=0;i<=m;i++)
    {
        ll N=n-i,M=m-i,A=C(N+M+i,i),B=(C(N+M,M)-C(N+M,M-1)+mod)%mod;
        ans=(ans+A*B%mod)%mod;
    }
    cout<<ans<<"\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
  	cin>>t;
    init();
    while(t--) solve();
}
原文