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
3 4 2
2 2
2 3
Output
2
Input
100 100 3
15 16
16 15
99 88
Output
545732279

总路径数减去经过黑点的路径数就是答案。若之前无障碍,走到点(x,y)的路径数=C(x,x+y)。实际计算时为避免重复,要减去从之前的黑点到当前黑点的路径数。

计算的值非常大,由于涉及取模除法,需要用到乘法逆元算法。

乘法逆元什么鬼!虽然看懂了但是不会实现,我选择死亡

高端解析和高端代码链接:http://blog.csdn.net/sdfzyhx/article/details/51822192

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int mxn=;
const long long p=;
long long jc[mxn],rc[mxn];//阶乘 逆元
long long dp[];
int h,w,n;
struct node{
int x,y;
}a[];
int cmp(node a,node b){
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
void eu(long long a,long long b,long long &x,long long &y){//扩展欧几里得
if(!b){
x=;y=;
}
else{
eu(b,a%b,y,x);
y-=x*(a/b);
}
return;
}
void init(){
int i;
jc[]=rc[]=;
long long x;
for(i=;i<;i++){
jc[i]=(jc[i-]*i)%p;//阶乘
eu(jc[i],p,rc[i],x);
rc[i]=(rc[i]%p+p)%p;
}
return;
}
long long c(int x,int y){
return jc[y]*rc[x]%p*rc[y-x]%p;
}
int main(){
init();
scanf("%d%d%d",&h,&w,&n);
int i,j;
for(i=;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
}
n++;a[n].x=h;a[n].y=w;
sort(a+,a+n+,cmp);
for(i=;i<=n;i++){
dp[i]=c(a[i].x-,a[i].x+a[i].y-);
// printf("---%d---\n",dp[i]);
for(j=;j<i;j++){
if(a[j].y<=a[i].y)
dp[i]=((dp[i]-dp[j]*c(a[i].x-a[j].x,a[i].x+a[i].y-a[j].x-a[j].y)%p)+p)%p;//减去重复值
}
}
printf("%lld\n",dp[n]);
return ;
}

CodeForces 559C Gerald and Giant Chess的更多相关文章

  1. Codeforces 559C Gerald and Giant Chess【组合数学】【DP】

    LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...

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

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

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

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

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

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

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

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

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

  8. Gerald and Giant Chess

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

  9. CF559C Gerald and Giant Chess

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

随机推荐

  1. Jenkins学习八:Jenkins语言本地化

    在Jenkins中,英语一大片,不懂英语的看着头疼.非常高兴的是,Jenkins作为一个主流流行的持续构建工具,提供了一个本地化语言的配置界面. 你可以找到它,在Jenkins每页的左下角.如下图: ...

  2. Tomcat 和 Resin 比较,哪个更适合你?

    先简单介绍下Resin.Resin是CAUCHO公司的产品,是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JAVA语 ...

  3. Android 手势识别类 ( 二 ) GestureDetector 源码浅析

    前言:Android 关于手势的操作提供两种形式:一种是针对用户手指在屏幕上划出的动作而进行移动的检测,这些手势的检测通过android提供的监听器来实现:另一种是用 户手指在屏幕上滑动而形成一定的不 ...

  4. 关于Yii2中count方法的使用

    统计文章与分类中间表中c_id的数目,也就是category表中total字段的值 原生SQL语句:select count(c_id) from article_category where c_i ...

  5. 正则基础之——环视(Lookaround)

    环视基础 环视只进行子表达式的匹配,不占有字符,匹配到的内容不保存到最终的匹配结果,是零宽度的.环视匹配的最终结果就是一个位置. 环视的作用相当于对所在位置加了一个附加条件,只有满足这个条件,环视子表 ...

  6. iOS程序间调用

    1.在被调用应用内的info.plist里面设置如下: 鼠标右击information property list ,然后从列表中选择URL types 右击 add row 添加一个对象(item) ...

  7. acl拒绝访问流量

        interface Ethernet0/0 ip address 12.1.1.2 255.255.255.0 ip access-group 10 in half-duplex   R1# ...

  8. Linux下Qt的安装与配置

    参考资料:http://www.cnblogs.com/emouse/archive/2013/01/28/2880142.html Linux 下编译.安装.配置 QT 下载qt 这里用的是4.7. ...

  9. php基础06:运算符

    <?php //1.PHP 字符串运算符: 串接 $str1 = "gao"; $str1 = $str1."xiong"; echo $str1; ec ...

  10. MyBatis出错Result Maps collection does not contain value for java.lang.Integer

    Servlet.service() for servlet [SpringMVC] in context with path [/eyou] threw exception [Request proc ...