跳到正文

算法·理论

数学总结——卡特兰数

program_xwl约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年6月13日
收录
2026年6月14日
更新
2026年11月20日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

卡特兰数最重要的是它的折线对称映射思路

定义与运算

卡特兰数 CnC_n 表示在一个坐标系中,起点在 (0,0)(0,0),终点 (n,n)(n,n),可以向左走或向上走,不能走到 y=xy=x 对角线上方的路径数。

公式1:Cn=∑i=0n−1CiCn−i−1C_n = \sum_{i=0}^{n-1} C_iC_{n-i-1}

枚举最后一次碰到对角线的点 (i,i)(i,i),从 (0,0)(0,0) 到 (i,i)(i,i) 的方案数为 CiC_i;从(i,i)(i,i) 到 (n,n)(n,n) 不碰到对角线的方案数,显然等价于把起点向右移动一格、终点向下移动一格的路径数,即 Cn−i−1C_{n-i-1}

公式2:Cn=(2nn)−(2nn+1)C_n = \binom{2n}{n}-\binom{2n}{n+1}

不超过一条线不好处理,考虑把对角线向上移动一格变成 y=x+1y=x+1,不超过 y=xy=x 变成不碰到 y=x+1y=x+1。这个合法情况也是不好处理的,考虑正难则反,总路径数为 (2nn)\binom{2n}{n},统计非法路径。

考虑对所有非法路径进行以下映射:把在 (0,0)(0,0) 到第一次碰到 y=x+1y=x+1 线的点 (k,k+1)(k,k+1) 全部沿着对角线翻折。翻折完成后,还是一条路径,但是起点变成了 (−1,1)(-1,1),如图:

这样映射有如下性质:

  • **所有的非法情况都可以被映射为 (−1,1)(-1,1) 到 (n,n)(n,n) 的路径:**显然,只要不合法的路径都会碰到 y=x+1y=x+1 这条线。

  • **所有的(−1,1)(-1,1) 到 (n,n)(n,n) 的路径被映射为非法情况:**显然,按照第一次碰到 y=x+1y=x+1 这条线的点翻折回去就是一条 (0,0)(0,0) 到 (n,n)(n,n) 的非法路径。

  • **上述两种映射不会出现不同路径映射结果相同的情况:**显然,映射过程中没有出现选择的情况。

所以,(−1,1)(-1,1) 到 (n,n)(n,n) 的路径和 (0,0)(0,0) 到 (n,n)(n,n) 的非法路径可以一一对应,所以非法路径的数量是 (2nn+1)\binom{2n}{n+1},合法路径数量即为 (2nn)−(2nn+1)\binom{2n}{n}-\binom{2n}{n+1}

公式3:Cn=4n−2n+1Cn−1C_n=\frac{4n-2}{n+1}C_{n-1}

与公式2等价,把组合数展开融在一起就是该公式。

基本运用

  • 长度为 2n2n 的合法括号序列数量为 CnC_n。正确性:合法括号序列的充要条件即:任意前缀左括号的数量不少于右括号的数量,整体左右括号数量相同。增量构造:加入一个左括号视为向右走一格,加入一个右括号视为向上走一格,最终走到 (n,n)(n,n)。
  • 节点数为 nn 的无标号二叉树的个数为 CnC_n。正确性:钦定一个根,枚举子树的划分情况,即 nn 个点的方案数 Tn=∑i=0n−1TiTn−i−1T_n=\sum_{i=0}^{n-1} T_iT_{n-i-1},与卡特兰数计算公式1相同,得证。

T1 [AHOI2012] 树屋阶梯

计数题不给模数是有什么心事吗?

思路

令 TiT_i 表示 n=in=i 的答案。

nn 个矩形,nn 个拐角,所以说如果答案合法,必然每个拐角都被互不相同的矩形覆盖。我们可以枚举 (0,0)(0,0) 被哪个拐点 (i,i)(i,i) 对应的矩形覆盖了,此时该矩形就会把阶梯分成两个阶梯,它们的阶梯数分别为 ii 和 n−i−1n-i-1,即

Ti=∑j=0n−1TjTi−j−1T_i=\sum_{j=0}^{n-1}T_jT_{i-j-1}

与卡特兰数计算公式1相同,就是卡特兰数。

代码

论神秘压位高精度

C++
#include <bits/stdc++.h>
using namespace std;

/*
高精度略
*/

const int N = 505;
int n;
BigInteger catalan[N];

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	catalan[0] = 1;
	for(int i = 1;i <= n;i++) for(int j = 0;j <= i-1;j++) catalan[i] += catalan[j]*catalan[i-j-1];
	cout << catalan[n];
	return 0;
}

T2 [SCOI2010] 生成字符串

思路

增量构造,初始在 (0,0)(0,0) 加入一格 11 看作向右走一格,加入一格 00 看作向上走一格,不能超过 y=xy=x 这条对角线,终点是 (n,m)(n,m)。可以用卡特兰数同理的折线翻折,得出结果为 (n+mn)−(m+mn+1)\binom{n+m}{n}-\binom{m+m}{n+1}。

代码

C++
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int mod = 20100403,N = 2e6+5;
int n,m,fact[N];

int power(int a,int b)
{
	int the_ans = 1;
	while(b)
	{
		if(b&1) the_ans = the_ans*a%mod;
		a = a*a%mod;
		b >>= 1;
	}
	return the_ans;
}

int c(int n,int m)
{
	if(n < m || m < 0) return 0;
	return fact[n]*power(fact[n-m]*fact[m]%mod,mod-2)%mod;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	fact[0] = 1;
	for(int i = 1;i <= n+m;i++) fact[i] = fact[i-1]*i%mod;
	cout << (c(n+m,n)-c(n+m,n+1)+mod)%mod;
	return 0;
}

T3 Natasha, Sasha and the Prefix Sums

思路

枚举最大前缀和 maxpre\text{maxpre},统计每次碰到 y=x+maxprey=x+\text{maxpre} 线的个数。我们可以计算出最大值大于等于某个值的方案数 ansi\text{ans}_i,可以容斥解决,但也可以直接计算

∑ansi\sum ans_i

为什么是对的?最大值是多少就会被算多少次。

代码

C++
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int mod = 998244853,N = 8005;
int n,m,fact[N],ans = 0;

int power(int a,int b)
{
	int the_ans = 1;
	while(b)
	{
		if(b&1) the_ans = the_ans*a%mod;
		a = a*a%mod;
		b >>= 1;
	}
	return the_ans;
}

int c(int n,int m)
{
	if(n < m || m < 0) return 0;
	return fact[n]*power(fact[n-m]*fact[m]%mod,mod-2)%mod;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	fact[0] = 1;for(int i = 1;i <= n+m+2;i++) fact[i] = fact[i-1]*i%mod;
	for(int maxpre = 1;maxpre <= n;maxpre++)
	{
		if(n-m >= maxpre) ans = (ans+c(n+m,n))%mod;
		else if(n-m < maxpre) ans = (ans+c(n+m,n-maxpre))%mod;
	}
	cout << ans;
	return 0;
}
原文