题意

C. Gerald and Giant Chess
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.

Input

The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).

Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.

Output

Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109 + 7.

Examples
Input
Copy
3 4 2
2 2
2 3
Output
Copy
2
Input
Copy
100 100 3
15 16
16 15
99 88
Output
Copy
545732279

给出一个h*w的棋盘(h,w<=1e5),其中有n个位置不能走(n<=2000),现在要从左上角走到右下角,每步只能向下或者向右走一步。问有多少种走法?右下角保证可以走到。

分析

参照PoemK的题解。

对于右走x步,下走y步的无限制方案数是C(x+y,y),可以记为C(x,y)。

首先将最右下角也作为一个不能走的位置,最后要求出到达这个位置的合法路径数dp[n]。

对所有不能走的位置排序,先比较行,再比较列。

对于位置i,首先有dp[i]=C(x[i],y[i]);

然后到达i途中不能经过任何一个障碍位置,所以枚举第一个障碍位置。dp[i]=dp[i]−∑dp[k]∗C(k−>i)(ifC(k−>i)>0)

时间复杂度\(O(n^2)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std; co int N=2e5,mod=1e9+7;
int add(int x,int y) {return (x+=y)>=mod?x-mod:x;}
int mul(int x,int y) {return (ll)x*y%mod;}
int fpow(int x,int k){
int re=1;
for(;k;k>>=1,x=mul(x,x))
if(k&1) re=mul(re,x);
return re;
}
int fac[N],ifac[N];
int binom(int n,int m) {return mul(fac[n],mul(ifac[m],ifac[n-m]));}
#define x first
#define y second
pair<int,int> a[N];
int h,w,n,f[N];
int main(){
fac[0]=ifac[0]=1;
for(int i=1;i<N;++i){
fac[i]=mul(fac[i-1],i);
ifac[i]=fpow(fac[i],mod-2);
}
read(h),read(w),read(n);
for(int i=1;i<=n;++i) read(a[i].x),read(a[i].y);
sort(a+1,a+n+1);
a[n+1].x=h,a[n+1].y=w;
for(int i=1;i<=n+1;++i){
f[i]=binom(a[i].x+a[i].y-2,a[i].x-1);
for(int j=1;j<i;++j)if(a[j].x<=a[i].x&&a[j].y<=a[i].y)
f[i]=add(f[i],mod-mul(f[j],binom(a[i].x+a[i].y-a[j].x-a[j].y,a[i].x-a[j].x)));
}
printf("%d\n",f[n+1]);
return 0;
}

CF559C Gerald and Giant Chess的更多相关文章

  1. cf559C. Gerald and Giant Chess(容斥原理)

    题意 $h \times w$的网格,有$n$个障碍点, 每次可以向右或向下移动 求从$(1, 1)$到$(h, w)$不经过障碍点的方案数 Sol 容斥原理 从$(1, 1)$到$(h, w)$不经 ...

  2. CF-559C Gerald and Giant Chess(计数DP)

    给定一个 \(H*W\)的棋盘,棋盘上只有\(N\) 个格子是黑色的,其他格子都是白色的. 在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动到黑色格子中.求这个卒从左上角移动到右下角 ...

  3. 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)

    [题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...

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

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

  6. Gerald and Giant Chess

    Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. E. Gerald and Giant Chess

    E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...

  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)--C. Gerald and Giant Chess(组合数学)

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. centos7 下安装mysql教程

    最近要在centos服务器上配置环境,在部署mysql的时候,碰到各种各样的问题,网上博客文章也是有各种坑,目前发现一个比较好的博客: https://blog.csdn.net/xiaomojun/ ...

  2. Redis的基本知识

    Redis: 1. 概念:开源的高性能键值对数据库, 提供多种键值对数据类型:字符串.(离散)哈希.列表.集合.有序集合类型 内部储存与持久化:内存的读写速度远高于磁盘:自身提供持久化机制(RDB.A ...

  3. Python函数的一点用法

    #python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 BIF是python内置的函数,任何一门语言都能用来创造函数,python也不例外 1.创建一个函数 def func() ...

  4. Java 平时作业七

    以下是几本计算机书籍的基本信息 编号  书名         价格      出版社 1  JAVA 基础   32   清华大学出版社 2  JAVA WEB 开发  40   电子工业出版社 3  ...

  5. (2018 Multi-University Training Contest 3)Problem L. Visual Cube

      //题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330//题目大意:按照一定格式画出一个 a×b×c 的长方体.  #include <b ...

  6. ArrayList、LinkedList和vector的区别

    1.ArrayList和Vector都是数组存储,插入数据涉及到数组元素移动等操作,所以比较慢,因为有下标,所以查找起来非常的快. LinkedList是双向链表存储,插入时只需要记录本项的前后项,查 ...

  7. Csrf_token ||| CSRF跨站请求伪造

    # 注: 部分内容参考网上,侵删   CSRF(Cross-site request forgery) 跨站请求伪造,是一种对网站的恶意利用  它会通过伪装成受信任用户的请求来利用受信任的网站来获取一 ...

  8. python实现三级菜单间的前后跳转

    #录入菜单def createMenu():    Menu = {}    provinces = ["陕西","四川","江苏"] sh ...

  9. redis命令Sortedset 类型(八)

    Sortedset又叫zset Sortedset是有序集合,可排序的,但是唯一. Sortedset和set的不同之处, 是会给set中的元素添加一个分数,然后通过这个分数进行排序. 命令 1> ...

  10. Visual C++ 6.0中if..else..的简单用法和基本格式

    # include <stdio.h> int main (void) { float score; printf("请输入您的考试成绩:"); scanf(" ...