Codeforces E. Qwerty78 Trip 【组合数学】

题目链接:E. Qwerty78 Trip
n*m的格子,其中有一个格子是坏的,问你从(1, 1) - (n, m)有多少种走法。

填满n * m的矩阵,发现这就是一个杨辉三角的组合数问题。
在格子没有坏的情况下,(1, 1) - (n, m)的走法dp[1][1][n][m] = C(n+m-2, m-1)。
当(x, y)坏的情况下,少出的情况就是dp[1][1][x][y] * dp[x][y][n][m]。
我们把(x, y)看做(1, 1)然后求一下就好了。套个逆元就OK了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <stack>
#define CLR(a, b) memset(a, b, sizeof(a))
#define PI acos(-1.0)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2 * 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL pow_mod(LL a, int n) {
    LL ans = 1;
    while(n) {
        if(n & 1) {
            ans = ans * a % MOD;
        }
        a = a * a % MOD;
        n >>= 1;
    }
    return ans;
}
LL f[MAXN];
LL C(int n, int m) {
    return f[n] * pow_mod(f[n-m], MOD-2) % MOD * pow_mod(f[m], MOD-2) % MOD;
}
int n, m;
int main()
{
    f[0] = 1LL;
    for(int i = 1; i <= 200000; i++) {
        f[i] = f[i-1] * i % MOD;
    }
    int t; scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        int x, y; scanf("%d%d", &x, &y);
        if(x == 1 && y == 1) {
            printf("0\n");
            continue;
        }
        printf("%lld\n", (C(n+m-2, m-1) - C(n+m-(x-1)-(y-1)-2, m-(y-1)-1) * C(x+y-2, y-1) % MOD + MOD) % MOD);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值