LINK


题目大意

有一个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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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)不经过坏点有多少条路径. 先把这些坏点排 ...

  4. 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值就可以 ...

  5. CF 559C - Gerald and Giant Chess (组合计数)

    \(C_{x+y}^y\)的公式,DP容斥删多余贡献. #include <cstdio> #include <iostream> #include <cstring&g ...

  6. CodeForces 540E - Gerald and Giant Chess(数论)

    给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法. 首先要知道从一个点A到另一个点B在没有障碍下有多少种走法.保证A在B的左上方,如图 一共需要走(X+Y)步(图中△x,△y), ...

  7. 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的网格,让你 ...

  8. 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 ...

  9. CodeForces 559C Gerald and Gia (格路+容斥+DP)

    CodeForces 559C Gerald and Gia 大致题意:有一个 \(N\times M\) 的网格,其中有些格子是黑色的,现在需要求出从左上角到右下角不经过黑色格子的方案数(模 \(1 ...

随机推荐

  1. 01_MapReduce流程总结

    1. MapReduce整体流程 1. 每个map,reduce都作为1个独立进程process启动(多进程并发方式,spark是多线程并发) 2. 由于进程空间独享,因此方便控制每个map, red ...

  2. mysql字符编码的设置以及mysql中文乱码的解决方法

    查看字符编码 首先,将中文插入到数据库乱码是因为没有将数据库编码设置为支持中文的编码,mysql的默认编码是Latin1,不支持中文,应该设置为utf8查看自己的数据库编码是否已设置好,进入数据库,输 ...

  3. 微信开发中使用curl忽略https证书

    http://blog.csdn.net/ljh504429906/article/details/51103519 微信开发中需要使用http及https的post与get请求实现api的调用.   ...

  4. JavaScript encodeURIComponent()

    ■ 把字符串作为 URI 组件进行编码.JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unes ...

  5. bootstrap 知识点

    1.datetimepicker //带分钟选择 $('.form_datetime').datetimepicker({ format: 'yyyy-mm-dd HH:mm:ss', languag ...

  6. Js判断是否联网引入不同js

    需求:当百度地图在内网中也能使用. 分析:js判断是否联网,然后根据联网状态加载不同js. 失败案例: 1.直接document.write <script language="jav ...

  7. uva11626逆时针排序

    给一个凸包,要求逆时针排序,刚开始一直因为极角排序就是逆时针的,所以一直wa,后来发现极角排序距离相同是,排的是随机的,所以要对末尾角度相同的点重新排一次 #include<map> #i ...

  8. Highcharts 向下钻取饼图

    Highcharts 向下钻取饼图 配置 drilldown 配置 drilldown 用于向下钻取数据,通过点击某项深入到其中的具体数据. drilldown: { series: drilldow ...

  9. IHTMLDocument2 TO IWebBrowser2

    if(NULL != pIHTMLDocument2) { IHTMLWindow2* pIHTMLWindow2 = NULL; hr = pIHTMLDocument2->get_paren ...

  10. C++复习2.软件开发知识小节

    高质量的软件开发 1.满足正确性,健壮性,可靠性,性能,易用性,清晰性,安全性,兼容性,扩展性,可移植性等等来评价软件的质量. 2.没有错误的程序世间难求,任何一个程序,无论他多么的小,总是存在着错误 ...