[SCOI2010]生成字符串
题目描述
lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数。现在lxhgww想要知道满足要求的字符串共有多少个,聪明的程序员们,你们能帮助他吗?
输入输出格式
输入格式:
输入数据是一行,包括2个数字n和m
输出格式:
输出数据是一行,包括1个数字,表示满足要求的字符串数目,这个数可能会很大,只需输出这个数除以20100403的余数
输入输出样例
输入样例#1:
2 2
输出样例#1:
2
说明
limitation
每点2秒
对于30%的数据,保证1<=m<=n<=1000
对于100%的数据,保证1<=m<=n<=1000000
来源:SCOI 2010
题解:
如果n==m则是标准的卡特兰数,然而题目中的m和n是不相等的。
仍按照卡特兰数的证明思路若出现前k个中恰好出现-1,那么将前面这一段翻转,就会出现n+1个1和m-1个-1那么不合法的方案就为C(n+m,m-1),容斥一下即可。
代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const LL max_n = 1e6+1;
const LL mod = 20100403;
const LL inf = 1e9+7;
LL n,m;
inline LL fast_pow(LL a,LL b,LL p)
{
LL ans=1;
while(b)
{
if(b&1) ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
return ans;
}
inline LL comb(LL a,LL b)
{
if(b>a) return 0;
if(b==a) return 1;
if(b>a-b) b=a-b;
LL ca=1,cb=1,ans=1;
for(LL i=0; i<b; ++i)
{
ca=(ca*(a-i))%mod;
cb=(cb*(b-i))%mod;
}
ans=(ca*fast_pow(cb,mod-2,mod))%mod;
return ans;
}
inline LL lucas(LL n,LL m)
{
LL ans=1;
while(n && m && ans)
{
ans=(ans*comb(n%mod,m%mod))%mod;
n/=mod;
m/=mod;
}
return ans;
}
int main()
{
scanf("%lld%lld",&n,&m);
printf("%lld",(lucas(n+m,m)-lucas(n+m,m-1)+mod)%mod);
return 0;
}