题意

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

每次可以向右或向下移动

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

Sol

容斥原理

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

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

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

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

// 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 = * 1e6, mod = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > ''){if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', 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 = ;
while(p) {
if(p & ) base = (base * a) % mod;
a = (a * a) % mod; p >>= ;
}
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() + ;
fac[] = ; for(int i = ; i <= h + w; i++) fac[i] = i * fac[i - ] % mod;
ifac[h + w] = fastpow(fac[h + w], mod - );
for(int i = h + w; i >= ; i--) ifac[i - ] = i * ifac[i] % mod;
for(int i = ; i <= N - ; i++) {
int x = read(), y = read();
P[i] = MP(x, y);
}
P[N] = MP(h, w);
sort(P + , P + N + );
for(int i = ; i <= N; i++) {
f[i] = C(P[i].fi + P[i].se - , P[i].fi - );
for(int j = i - ; j >= ; j--) {
if(P[j].se <= P[i].se) {
int x = P[i].fi - P[j].fi + , y = P[i].se - P[j].se + ;
(f[i] -= f[j] * C(x + y - , x - ) % mod + mod) %= mod;
}
}
}
printf("%I64d", (f[N] + mod) % mod);
return ;
}
/*
2 3 2
2 1
2 2
*/

cf559C. Gerald and Giant Chess(容斥原理)的更多相关文章

  1. CF559C Gerald and Giant Chess

    题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. CF-559C Gerald and Giant Chess(计数DP)

    给定一个 \(H*W\)的棋盘,棋盘上只有\(N\) 个格子是黑色的,其他格子都是白色的. 在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动到黑色格子中.求这个卒从左上角移动到右下角 ...

  3. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

  4. dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess

    Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...

  5. CodeForces 559C Gerald and Giant Chess

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Gerald and Giant Chess

    Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. E. Gerald and Giant Chess

    E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...

  8. Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP

    C. Gerald and Giant Chess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  9. codeforces(559C)--C. Gerald and Giant Chess(组合数学)

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. ubuntu16.04 ROS安转及RVIZ启动

    1.软件中心配置 首先打开软件和更新对话框,打开后按照下图进行配置(确保你的"restricted", "universe," 和 "multiver ...

  2. 火狐浏览器安装VULTR笔记

    1.购买一台vultr服务器, 支持支付宝扫码支付,直接美刀转人民币实时结算:优先选日本的,然后美国的; 购买服务器步骤: Server Location: Tokyo Japan Server Ty ...

  3. POJ2184(01背包变形)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11092   Accepted: 4404 D ...

  4. css3 position:sticky

    最近在写一个小程序,项目中遇到一个需求:页面滚动到tab切换菜单时,菜单fixed到页面顶部: 实现方法: 使用小程序的onPageScroll事件,滚动到指定位置添加fixed样式: bug1:获取 ...

  5. 五、hibernate在myeclipse中生成实体和映射

  6. 【原】Oracle 11.2.0.1 64bit for RHEL6.0 Server x86_64 静默安装

    作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/p/4182469.html 本文适用Or ...

  7. char的定义在iOS和Android下是不同的

    char is different in iOS and Android!跨平台开发时很容易忽略的非常坑爹的一个区别. 我的需求是实现一个算法,这个算法在iOS和Android下需要保持一致的结果,很 ...

  8. 16-CoreData之多表关联(存储自定义数据模型)

    多表关联 1.1-简介 什么是多表关联 在处理数据库的关系中,无非只有三种关系 一对一:一个老师只能在一个教室上课,不可能同时在两个教室上课 一对多:一个教室可以有多个学生,但一个学生只能在一个教室 ...

  9. POJ3697【BFS】

    题意: n个点的完全图,删掉m条边以后,求与1联通的点的个数. 思路: 直接判断 遍历图,n(n+1)/2=5e7 复杂度n^2......,哦,这样也行... //#include<bits/ ...

  10. lightoj1087 【线段树】

    题意: 给你n个数,然后给你q个询问,有两种询问: a: 表示在右边插入一个数 c:表示从左边拿出一个数,然后输出: 思路: 一开始在想,自己手上的黑科技:线段树和树状数组 线段树上的操作: 求区间最 ...