Codeforces 559C Gerald and Giant Chess【组合数学】【DP】
题目大意
有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数
思路
考虑容斥
先把所有黑点按照x值进行排序方便计算
\(dp_{i}\)表示从起点走到第i个黑点不经过任何的黑点的方案数
然后\(dp_{i}=C(x_i+y_i-2,x_i-1)-\sum_{j|x_j\leq x_i,y_j\leq y_i}dp_{j}\times C(j->i)\)
这样容斥为什么是正确的,\(dp_{j}\)考虑了所有经过j的情况,其他的包含j的点都不会考虑j的贡献
所以容斥是正确的
//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 2e5 + 10;
const int Mod = 1e9 + 7;
int fac[N], inv[N];
int w, h, n, dp[N];
//dp表示不经过任何黑点走到第i个黑点的方案数
struct Point {
int x, y;
} p[N];
bool cmp(Point a, Point b) {
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
}
int sub(int a, int b) {
return (a -= b) < 0 ? a + Mod : a;
}
int mul(int a, int b) {
return 1ll * a * b % Mod;
}
int fast_pow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
int C(int a, int b) {
return mul(fac[a], mul(inv[b], inv[a - b]));
}
void init(int len) {
fac[0] = inv[0] = 1;
fu(i, 1, len) fac[i] = mul(fac[i - 1], i);
fu(i, 1, len) inv[i] = fast_pow(fac[i], Mod - 2);
}
int main() {
Read(h), Read(w), Read(n);
init(h + w);
fu(i, 1, n) Read(p[i].x), Read(p[i].y);
sort(p + 1, p + n + 1, cmp);
fu(i, 1, n) {
dp[i] = C(p[i].x + p[i].y - 2, p[i].x - 1);
fu(j, 1, i - 1)
if (p[j].y <= p[i].y)
dp[i] = sub(dp[i], mul(dp[j], C(p[i].x - p[j].x + p[i].y - p[j].y, p[i].x - p[j].x)));
}
int ans = C(h + w - 2, h - 1);
fu(i, 1, n) ans = sub(ans, mul(dp[i], C(h - p[i].x + w - p[i].y, h - p[i].x)));
Write(ans);
return 0;
}
Codeforces 559C Gerald and Giant Chess【组合数学】【DP】的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess (Lucas + dp)
题目链接:http://codeforces.com/contest/560/problem/E 给你一个n*m的网格,有k个坏点,问你从(1,1)到(n,m)不经过坏点有多少条路径. 先把这些坏点排 ...
- 2018.11.07 codeforces559C. Gerald and Giant Chess(dp+组合数学)
传送门 令f[i]f[i]f[i]表示对于第iii个棋子,从(1,1)(1,1)(1,1)出发到它不经过其它棋子的方案数. 于是我们假设(h,w)(h,w)(h,w)有一个棋子,求出它的fff值就可以 ...
- CF 559C - Gerald and Giant Chess (组合计数)
\(C_{x+y}^y\)的公式,DP容斥删多余贡献. #include <cstdio> #include <iostream> #include <cstring&g ...
- CodeForces 540E - Gerald and Giant Chess(数论)
给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法. 首先要知道从一个点A到另一个点B在没有障碍下有多少种走法.保证A在B的左上方,如图 一共需要走(X+Y)步(图中△x,△y), ...
- 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 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 Gerald and Gia (格路+容斥+DP)
CodeForces 559C Gerald and Gia 大致题意:有一个 \(N\times M\) 的网格,其中有些格子是黑色的,现在需要求出从左上角到右下角不经过黑色格子的方案数(模 \(1 ...
随机推荐
- Extjs 分页多选的实现
Extjs 版本 6.X 单页面的多选,没有任何问题. 直接使用 Grid的配置项进行绑定即可获取: xtype: 'grid', bind: { selection: '{checkedRecord ...
- 比较好的Json 格式数据
{ "81040753986": [{ "order_info": { "unique_package_reference": " ...
- NoSQL&&Redis介绍
再说Redis之前,想先说一下NoSQL.在最早的单机时代,随着数据的增加一台机器可能放不下了.同时索引占用的内存空间也会越来越大.对请求的读写操作影响很大.于是就在数据库之前增加了一层保护层 — 缓 ...
- 值类型的TryParse
值类型(Struct(如:DateTime).基本类型(如:double).枚举类型)的TryParse方法,通常可使用该方法将“字符串”转换为当前类型,并out出.比如:日期格式的字符串 转换为 ...
- cordova安卓sdk
Android SDK在线更新镜像服务器来下载安装: 1.北京化工大学镜像服务器地址: IPv4: ubuntu.buct.edu.cn/ 端口:80 IPv4: ubuntu.buct.cn/ 端口 ...
- 1-16-1 LVM管理和ssm存储管理器使用&磁盘配额
大纲: 1-1- LVM逻辑卷的管理 1-2- SSM管理工具的使用 1-3- 磁盘配额技巧 ====================================== 问题描述: 当我们需要在一个 ...
- 浅谈js数据类型识别方法
js有6种基本数据类型 Undefined , Null , Boolean , Number , String ,Symbol和一种引用类型Object,下面我们就来一一看穿,哦不,识别他们. t ...
- Quartz教程:快速入门
原文链接 | 译文链接 | 翻译:nkcoder | 校对:方腾飞 本系列教程由quartz-2.2.x官方文档翻译.整理而来,希望给同样对quartz感兴趣的朋友一些参考和帮助,有任何不当或错误之处 ...
- Oracle recovery manager failed to restore
解决办法: 1:清理过期失效的备份, 2:增加recovery_file_dest_size参数值即可: SQL> show parameter db_recover NAME ...
- redis安装配置记录
环境:CentOS7,最小化安装 安装gcc wget # yum upgrade # yum install gcc # yum install wget 下载并安装redis # wget htt ...