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 ...
随机推荐
- 洛谷 1082 同余方程——exgcd(水题)
题目:https://www.luogu.org/problemnew/show/P1082 大水题. #include<iostream> #include<cstdio> ...
- 基于Jenkins自动构建系统开发
1 绪论 1.1 课题的研究背景 随着IT行业的不断发展,软件开发的复杂度也随着不断提高.与此同时,软件的开发团队也越来越庞大,而如何更好地协同整个团队进行高效准确的工作,从而确保软件开发的质量已经 ...
- 【Linux学习】Linux系统管理1—进程管理
Linux系统管理1-进程管理 一.Linux的三种进程 Linux包括3中不同类型的进程: 交互进程:由一个shell启动的进程.交互进程可以在前后台运行 批处理进程:该进程和终端无联系,是一个进程 ...
- UVa 11825 Hackers' Crackdown (状压DP)
题意:给定 n 个计算机的一个关系图,你可以停止每台计算机的一项服务,并且和该计算机相邻的计算机也会终止,问你最多能终止多少服务. 析:这个题意思就是说把 n 台计算机尽可能多的分成一些组,使得每组的 ...
- qq开放平台可以应用到网页游戏的api整理
创建角色界面api整理 一.需求描述 1. 创建角色名称可以用qq空间昵称代替 2. 如果玩家是在新区玩的话,赠送老玩家支持礼包 3. 可以看到,好友xxx也在玩,而且到了多少等级,如果加为好友 ...
- 服务迁移之路 | Spring Cloud向Service Mesh转变
一.导读 Spring Cloud基于Spring Boot开发,提供一套完整的微服务解决方案,具体包括服务注册与发现,配置中心,全链路监控,API网关,熔断器,远程调用框架,工具客户端等选项中立的开 ...
- 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形...)
前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较'高级'的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...
- hoj2662 Pieces Assignment
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- P5137 polynomial(分治)
传送门 神仙--这题有毒-- 一直在那里考虑没有逆元怎么办然后考虑解exgcd巴拉巴拉 最后只好看题解了 而且这题龟速乘都不行--得用代码里那种叫人半懂不懂的方式取模-- //minamoto #in ...
- Sql Server2008R2与IDEA的连接
数据库的连接笔者搞了一天,参阅了众多连接方案,大部分都是Eclipse和My sql,笔者一遍一遍的调试,终于皇天不负有心人,成绩先摆出来 为了让更多的新手能少走弯路,话不多说,上干货 首先,我们需要 ...