AtCoder Regular Contest 058 D.Iroha and a Grid(组合数学)

Link

https://atcoder.jp/contests/arc058/tasks/arc058_b

Description

一个H×W的矩形区域,它的左下角有一块A×B的矩形区域,要求中途不经过左下角区域,从大矩形的左上角走到右下角,求方案数;

Solution

如下图
在这里插入图片描述
该图是10×5的矩阵,如果不考虑左下角直接从s到t方案数为 C 10 − 1 + 5 − 1 5 − 1 C_{10-1+5-1}^{5-1} C101+5151,而如果考虑左下角区域,则不妨将路径分成两部分,即s到t,可先从s到a,然后a到b,b在到t,那么这样的情况下的方案数,就是s到a的方案数乘以b到t的方案数,即 C 3 − 1 + 2 − 1 2 − 1 ∗ C 10 − 2 + 5 − 3 − 1 10 − 2 C_{3-1+2-1}^{2-1}*C_{10-2+5-3-1}^{10-2} C31+2121C102+531102种方案,然后把所有的情况以该种方式计算出来,即将所有的a和b的结果求和,剩下的就是求逆元和阶乘了。

Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define inf 0x7f7f7f7f
#define maxn 100006
#define mod 1000000007
#define N 1
#define P 2


typedef long long ll;
typedef struct {
    int u, v, next, w;
} Edge;
Edge e[1];
int cnt, head[1];

inline void add(int u, int v,int w) {
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    // e[cnt].f=f;
    e[cnt].next = head[u];
    head[u] = cnt++;
    e[cnt].u = v;
    e[cnt].v = u;
    e[cnt].w = w;
    //    e[cnt].f=-f;
    e[cnt].next = head[v];
    head[v] = cnt++;
}

inline void write(int x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
inline ll read() {
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
ll h,w,a,b,f[2*100005],inv[2*100005];

ll qpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1)res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
void solve(){
    f[0]=1;
    for(ll i=1;i<=h+w-2;i++){
        f[i]=(f[i-1]*i)%mod;
    }
    inv[0]=1;
    inv[h+w-2]=qpow(f[h+w-2],mod-2);
    for(int i=h+w-3;i>0;i--)
        inv[i]=inv[i+1]*(i+1)%mod;

}
ll C(ll a,ll b){
    return f[a]*inv[b]%mod*inv[a-b]%mod;
}

int main(){
    h=read(),w=read(),a=read(),b=read();
    solve();
    ll res=0;
    for(int i=1;i<=h-a;i++){
        ll t=C(i-1+b-1,i-1)*C(h-i+w-b-1,h-i)%mod;
        res=(res+t)%mod;
    }
    cout<<res<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值