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 ...
随机推荐
- poj3417 Network——LCA+树上差分
题目:http://poj.org/problem?id=3417 根据一条边被几个环覆盖来判断能不能删.有几种情况等: 用树上差分,终点 s++,LCA s-=2,统计时计算子树s值的和即可: 用S ...
- socket服务器并发处理
我们知道,服务器通常是要同时服务多个客户端的,如果我们运行上一篇实现的server和client之后,再开一个终端运行client试试,新的client就不能能得到服务了.因为服务器之支持一个连接. ...
- 苹果app(iOS app)的URL schemes
最近折腾iOS快捷启动应用或应用内的某个动作的神器launch center pro (LCP),发现很多国产app并没有被LCP官方收录,所以不得不想办法找到app的url schemes. 下面是 ...
- Summit Online Judge
题意: 询问将取值在 $[L,R]$ 的若干个整数相加,可以得到 $[x,y]$ 区间内多少个数字. 解法: 只需要考虑求 $[L,R]$ 的数字能凑出 $[1,n]$ 的多少个数字,即可得出答案. ...
- httpclient:Ip 代理
参考:http://blog.csdn.net/sdfiiiiii/article/details/70432060 http://blog.csdn.net/qy20115549/article/ ...
- C++ 预处理指令 #pragma
http://www.cnblogs.com/qinfengxiaoyue/archive/2012/06/05/2535524.html
- 基于Jenkins自动构建系统开发
1 绪论 1.1 课题的研究背景 随着IT行业的不断发展,软件开发的复杂度也随着不断提高.与此同时,软件的开发团队也越来越庞大,而如何更好地协同整个团队进行高效准确的工作,从而确保软件开发的质量已经 ...
- ZOJ3158 【DFS】
题意: 从上往下切割,不能切边缘,然后问你两个差值,要小于给出的值. 思路: 直接爆搜,枚举每一行的劈开位置: #include<bits/stdc++.h> using namespac ...
- AtCoder Regular Contest 061 E - すぬけ君の地下鉄旅行【最短路】
具体题解又要搬大哥的了,嘿嘿~ 请点击:G点我 这道题目的难点就是同一家公司的路直接走不需要再花费,然后多了一个公司这个东西,这个不像是边的副权值(瞎说的)之类的东西,这是对于路来说的,路的属性... ...
- TRANSFORM_TEX
Shader "Custom/Exam1" { Properties { _MainTex ("Texture", 2D) = "white" ...