【BZOJ 1856】【SCOI 2010】字符串

本文介绍了一种利用组合数学原理解决特定路径计数问题的方法,通过将问题转化为从起点到终点的路径计数,利用组合数计算总路径数,并通过计算经过特定线的路径数来排除不符合条件的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

考虑这样转化问题:从(0,0)出发走向(n+m,n-m),字符串中1表示向右上角斜走一格,0表示向右下角斜走一格,1的数量少于0的数量等价于碰到直线y=-1。
如果不考虑约束条件,答案就是C(n+m,n)。
对于经过y=-1这个条件,可以将(0,0)对称到(0,-2),从(0,-2)走到(n+m,n-m)的情况就是所有从(0,0)出发走到(n+m,n-m)的情况,一共有C(n+m,m-1)中。
所以最后答案就是C(n+m,n) - C(n+m,m-1)。也不用什么高级的方法,直接用扩欧做一下就好了。

#include<cmath>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
#define mod 20100403
#define N 100000
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
ll res;
int n,m;
ll multi(ll x)
{
    ll t = 1; int i;
    fo(i,1,x) t = (t * i) % mod;
    return t;
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
    if (b == 0) {x = 1; y = 0; return;}
    exgcd(b,a%b,x,y);
    ll  t = x; x= y; y = t - a/ b * y;
}
ll inv(ll a)
{
    ll x,y; exgcd(a,mod,x,y); 
    x = (x + mod) % mod; return x;
}
ll cc(ll x,ll y)
{
    ll a,b,c;
    a = multi(x); b = multi(y); c = multi(x-y);
    return (a * inv(b) % mod * inv(c) % mod) % mod;
}

int main()
{
    scanf("%d%d",&n,&m);
    res = cc(n+m,n) - cc(n+m,n+1);
    res = res + mod * 2; res %= mod;
    printf("%lld",res);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值