前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cf559C. Gerald and Giant Chess(容斥原理)

cf559C. Gerald and Giant Chess(容斥原理)

作者头像
attack
发布2018-09-17 15:31:11
3040
发布2018-09-17 15:31:11
举报

题意

$h \times w$的网格,有$n$个障碍点,

每次可以向右或向下移动

求从$(1, 1)$到$(h, w)$不经过障碍点的方案数

Sol

容斥原理

从$(1, 1)$到$(h, w)$不经过障碍点的方案数为$C(h + w, h)$

设$f[i]$表示到达第$i$个黑格子的合法路径的方案数

首先对所有点按$x$排序,这样就能保证每次从他的左上方转移而来

然后根据公式算一下就好了

代码语言:javascript
复制
// luogu-judger-enable-o2
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
//#define int long long
using namespace std;
const int MAXN = 3 * 1e6, mod = 1e9 + 7;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    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;
}
Pair P[MAXN];
int h, w, N;
int fac[MAXN], ifac[MAXN], f[MAXN];
int fastpow(int a, int p) {
    int base = 1;
    while(p) {
        if(p & 1) base = (base * a) % mod;
        a = (a * a) % mod; p >>= 1;
    }
    return base % mod;
}
int C(int N, int M) {
    return (fac[N] * ifac[M] % mod * ifac[N - M]) % mod;
}
main() {
    h = read(), w = read(); N = read() + 1; 
    fac[0] = 1; for(int i = 1; i <= h + w; i++) fac[i] = i * fac[i - 1] % mod;
    ifac[h + w] = fastpow(fac[h + w], mod - 2); 
    for(int i = h + w; i >= 1; i--) ifac[i - 1] = i * ifac[i] % mod;
    for(int i = 1; i <= N - 1; i++) {
        int x = read(), y = read();
        P[i] = MP(x, y);
    }
    P[N] = MP(h, w);
    sort(P + 1, P + N + 1);
    for(int i = 1; i <= N; i++) {
        f[i] = C(P[i].fi + P[i].se - 2, P[i].fi - 1);
        for(int j = i - 1; j >= 1; j--) {
            if(P[j].se <= P[i].se) {
                int x = P[i].fi - P[j].fi + 1, y = P[i].se - P[j].se + 1;
                (f[i] -= f[j] * C(x + y - 2, x - 1) % mod + mod) %= mod;
            }
        }
    }
    printf("%I64d", (f[N] + mod) % mod);
    return 0;
}
/*
2 3 2
2 1
2 2
*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档