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 ...
随机推荐
- 创建calico网络报错client response is invalid json
使用docker创建calico网络失败. # docker network create --driver calico --ipam-driver calico-ipam testcalico E ...
- ceph部署与问题
一.基本情况:物理设备:4台惠普dl360,4个千兆网卡 4个1T盘操作系统统一为:CentOS 7.2.1511ceph版本:10.2.3ceph-deploy版本:1.5.36网络情况:192.1 ...
- UVa-10954
10954 - Add All Time limit: 3.000 seconds Yup!! The problem name reflects your task; just add a set ...
- mosquitto.conf之log配置
# ================================================================= # Logging # 日志信息 # ============= ...
- 【Data Structure & Algorithm】求1+2+…+n
求1+2+-+n 题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A ? B : C). 分析:此题没多少实际意义,因为 ...
- 爬虫代码实现四:采用Hbase存储爬虫数据(2)
导入hbase的jar包,在maven仓库找:进入http://mavenrepository.com/,输入hbase client,选择apache hbase client, 点击进入,选择 点 ...
- 使用git命令将本地项目上传到GIthub
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文 ...
- 51nod1459【二级最短路】
标签说的是BFS... 太菜,不知道怎么BFS...是不是spfa写,就叫BFS...感觉不是.... 只是二级最短路的写法,直接搞就很容易了,简单题: #include <bits/stdc+ ...
- Telnet 命令格式
Telnet host 端口 如:Telnet 127.0.0.1 11211 执行命令进入后 ctr +] ,打开回显,并回车即可
- uoj#273. 【清华集训2016】你的生命已如风中残烛(组合数学)
传送门 一道打表题 我们把那些普通牌的位置看成\(-1\),那么就是要求有多少个排列满足前缀和大于等于\(1\) 考虑在最后放一个\(-1\),那么就是除了\(m+1\)的位置前缀和都要大于等于\(1 ...