Codeforces Round #313(div.2) E. Gerald and Giant Chess

题目链接

题目大意:有一个 hw 的棋盘,一个兵要从 S(1,1) 走到 T(h,w) ,且只能向下或向右走,还有 n 个格子是不能走的,问从S走到 T 的路径数。

分析:如果每个格子都能走,那么从(1,1)走到 (p,q) 的答案为 Cp1p+q2 (要向下走 p1 步,向左走 q1 步,我们从 p+q2 个位置中选出 p1 个向下走,剩下的就是向右走的啦)。

简单题解:
我们设 A0,A1,A2,,An1 表示排序后的不能走的格子。
G(A,B) 表示从 A(x1,y1) B(x2,y2) 的方案数,由上面的分析很容易得到

G=Cx2x1x2x1+y2y1
Fi 表示走到 Ai 不经过 A0,A1,,Ai1 的答案,那么有转移方程
Fi=G(S,Ai)j=1i1G(Aj,Ai)Fj
(h,w) 也看成不能走的格子,记为 An ,那么 Fn 就是答案。

算组合数的话预处理阶乘及逆元即可。

时间复杂度: O(n2)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

inline int read()
{
    int x = 0, f = 1, t = getchar();
    while(t < 48 || t > 57) t == 45 ? f = -1 : 0, t = getchar();
    while(t > 47 && t < 58) x = (x << 1) + (x << 3) + t - 48, t = getchar();
    return x * f;
}

const int maxn = 2005;
const int maxl = 200000;
const int MOD = 1e9 + 7;

struct Black
{
    int x, y;
    bool operator < (const Black &r) const {
        return x < r.x || (x == r.x && y < r.y);
    }
};
int h, w, n;
Black data[maxn];
LL fac[maxl + 5], inv[maxl + 5];
LL f[maxn];

LL power_mod(LL x, int y)
{
    LL res = 1;
    for(; y; y >>= 1, x = x * x % MOD)
        if(y & 1) res = res * x % MOD;
    return res;
}
void init_fact()
{
    fac[0] = 1; for(int i = 1; i <= maxl; ++i) fac[i] = fac[i - 1] * i % MOD;
    inv[maxl] = power_mod(fac[maxl], MOD - 2); for(int i = maxl; i >= 1; --i) inv[i - 1] = inv[i] * i % MOD;
}
LL C(int x, int y)
{
    if(x < y) return 0;
    return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
LL calc(int x, int y)
{
    if(x < 1 || y < 1) return 0;
    return C(x + y - 2, x - 1);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    init_fact();

    h = read(), w = read(), n = read();
    for(int i = 0; i < n; ++i) data[i] = (Black){read(), read()};
    data[n] = (Black){h, w};
    sort(data, data + n + 1);

    for(int i = 0; i <= n; ++i)
    {
        LL res = 0;
        for(int j = 0; j < i; ++j)
            res = (res + f[j] * calc(data[i].x - data[j].x + 1, data[i].y - data[j].y + 1)) % MOD;
        f[i] = (calc(data[i].x, data[i].y) - res + MOD) % MOD;
    }
    int ans = f[n];
    printf("%d\n", ans);

#ifndef ONLINE_JUDGE
    fclose(stdin), fclose(stdout);
#endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值