cf559C. Gerald and Giant Chess(容斥原理)
题意
$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(容斥原理)的更多相关文章
- CF559C Gerald and Giant Chess
题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CF-559C Gerald and Giant Chess(计数DP)
给定一个 \(H*W\)的棋盘,棋盘上只有\(N\) 个格子是黑色的,其他格子都是白色的. 在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动到黑色格子中.求这个卒从左上角移动到右下角 ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- 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的网格,让你 ...
- 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 ...
- Gerald and Giant Chess
Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- 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 ...
- 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 ...
随机推荐
- 基于候选区域的R-CNN系列网络简介
使用候选区域方法(region proposal method)创建目标检测的感兴趣区域(ROI).在选择性搜索(selective search,SS)中,首先将每个像素作为一组.然后,计算每一组的 ...
- bzoj 4559 [JLoi2016]成绩比较 —— DP+拉格朗日插值
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4559 看了看拉格朗日插值:http://www.cnblogs.com/ECJTUACM-8 ...
- calico在docker上的部署及验证
1. 背景 以下的部署以五台服务器环境为例: 服务器1: hostname为etcdnode1, IP为192.168.56.100 服务器2: hostname为etcdnode2, IP为192. ...
- docker容器磁盘
docker容器磁盘扩容 一.配置文件里更改容器创建时的默认磁盘大小 [root@ip---- ~]# cat /etc/sysconfig/docker-storage DOCKER_STORAGE ...
- windows install JDK&&JRE
重装系统后,安装的java环境没了,只能重装一下~~~~ 1.下载JDK 2.这里会安装两次,其中第一次为安装 JDK,第二次安装JRE,建议不要将这两个放在同一个文件夹. 3.配置环境变量 用鼠标右 ...
- libvirt监控
- PHP中的常用正则表达式集锦
PHP中的常用正则表达式集锦: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xf ...
- bzoj 3504: [Cqoi2014]危桥【最大流】
妙啊,很容易想到连(s,a1,an)(s,b1,bn)(a2,t,an)(b2,t,bn),这样,但是可能会发生a1流到b2或者b1流到a2这种不合法情况 考虑跑两次,第二次交换b1b2,如果两次都合 ...
- [HNOI2010] 弹飞绵羊 bounce
标签:分块.题解: 200000,而且标号从0开始,很符合分块的条件啊.看看怎么实现. 首先分成√n个区间,然后如果我们对于每一个位置i,求出一个Next[i]和step[i],分别表示跳到的后一个位 ...
- 17..userinfo.txt 文件中存放以下结构:
alex:alex3714 wusir:123456 meet:meet123 1.让用户选择: 1.注册 2.登录 2.用户选择注册就将账号和密码添加到userinfo.txt中,如果用户名存在就提 ...