卡特兰数
卡特兰数有很多表示方法,其中一个是:
如图,一个 的网格,起点为 ,终点为 ,不能越过 这条直线,每次只能向上或向右一个点,求从起点走到终点的方案数。
显然,如果没有 的限制,方案数为 。
但是有这个限制,所以减去不合法的方案数。因为越过了 。
设路径第一次交 于 ,将 的路径沿 翻折,到 ,其中 ,翻折上去时向上变成向右,向右变成向上,所以就变成了起点为 ,终点为 的方案数,也就是 。
综上, 是合法方案数,。
将 变形:
所以 。
还有一种变形方式,。
因为我做不了图,所以就借一下 S_Gloria 的图。
这些都是卡特兰数的例子,接下来会一一证明。
1
将向上视为出栈,向右视为入栈,就变成了 2,所以是卡特兰数。
同时,设在压入第 个数前,前 个数全都出栈了,则 ,所以 。
3
设第 个数为根结点,如果 ,那么是空树,则 ,所以 ,所以是卡特兰数。
4
注:这里有 个节点。
设左子树有 个节点,右子树有 个节点,所以 。
5
设 ,所以左边要有 个点,右边要有 ,所以 。
6
用处理括号序列的方法,( 是入栈,) 是出栈,变成 1。
所以,卡特兰数的公式有:
斯特林数
斯特林数有两类,第一类斯特林数和第二类斯特林数。
第一类斯特林数
记 将 个不同的数组成 个圆排列的方案数。
有两种情况:第 个单组一个圆排列,第 个放到其它圆排列上。
单组一个圆排列
此时前 个数组成 个圆排列,。
放到前面的圆排列里
此时前 个数组成 个圆排列,第 个数可以放到前 个数的其中一个前面,。
综上,
第二类斯特林数
记 为 个不同的数放到 个集合中的方案数。
同理,有两种情况:第 个数单开一个集合,或者第 个数放到 个集合的其中一个,这个递推式很显然。
综上,。
卡特兰数练习
#P1192. 树屋
显然高度为 尺的阶梯有 个顶点,有两个是在最上面/最下面的钢材的顶点,所以剩下的 个顶点肯定要一个钢材一个顶点,要不然就不是矩形了。
给顶点编号(摘抄自 syksykCCC 的题解):
显然,覆盖 的矩形会分成 的两个子问题,所以 。
code
#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. 完美序列
设 是左括号, 是右括号,所以就是卡特兰数的第六种形式。
code
#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. 划分
显然让 和 组合最优。
prof.
设 。
中,如果让 , 配对,则贡献 ,如果让 , 配对,则贡献 ,证毕。
此时可以把这 对数调换顺序,对于一种 就有了 种方案。
还可以把 调换顺序,此时对于一种 的无序对有了 种方案。
此时,设 , 是左括号, 是右括号,就是 , 是卡特兰数。
综上,方案数为 。
code
#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. 挖矿
首先结尾要符合条件,所以 , 的直接输出 。
设 。
因为 ,所以不能越过 ,设 ,也就是说从 到 ,不可以越过 。
因此不合法的方案会经过 的点。
因此,设路径第一次交 于 ,将 沿 翻折成 ,则 。
所以不合法的方案数是 ,合法的是 。
code
#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. 魔法石
设施了 次变红魔法, 次变蓝魔法。
显然有三种情况:
- :显然至少一个石头是蓝的,无解;
- :显然最优方案可以全部有解,所以就是随便排, 种方案;
所以只剩一种情况:。
贪心可以求出最多有 个石头被施的变红魔法大于变蓝魔法(也就是只多施一次),其他红蓝相等的要最后施蓝的。
因为一个红蓝相等就是类似 ******RB 的类型,****** 占位,所以丢掉,变成 RB。
此时就是要求出有多少种组合可以找出 个 RB,故:
所有前缀
B比R最多多 。
把 B 视为向上,R 视为向右,从 走到 不能越过 。
这个是上一题用过的做法,答案是 ,当然 也可以用,因为此时 。
做完了吗?并没有。因为当 的时候,最后一个不一定要 RB,只需要 R,所以 ,这时候才做完了。
code
#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 水の三角(修改版)
显然,如果没有斜边,就是 ,其中 为行号减一,列号减一。
有了斜边也差不多,设走了 条斜边,那么下和右就只剩 条了,方案数 ,再乘上斜边的放置方案 ,答案就是:
code
#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();
}






