E. Gerald and Giant Chess
time limit per test

2 seconds

memory limit per test

256 megabytes2015-09-09

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 anh × 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 modulo109 + 7.

Sample test(s)
input
3 4 2
2 2
2 3
output
2
input
100 100 3
15 16
16 15
99 88
output
545732279

我们对着2000个点离(1,1)点的距离进行排序;
dp[i] 表示 从11点没有经过黑点到达i的方案数
dp[i]=C(x+y-2,y-1)-(dp[j]*C(x-x1+y-y1,x-x1))
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
const int maxn=+;
typedef long long LL;
const LL mod=;
LL dp[maxn];
struct point{
int x,y;
bool operator < (const point &rhs) const
{
return (x+y)<(rhs.x+rhs.y);
}
}P[maxn];
LL fax[*];
void init()
{
int n=*;
fax[]=;
for(int i=; i<=n; i++)
fax[i]=(fax[i-]*i)%mod;
}
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if(b==){
d=a; x=;y=;
}else {
gcd(b,a%b,d,y,x); y-=x*(a/b);
}
}
LL inv(LL a,LL n)
{
LL d,x,y;
gcd(a,n,d,x,y);
return (x+n)%n;
}
LL lucas(int n,int m)
{
LL ans=;
while(n&&m)
{
int a=n%mod,b=m%mod;
if(a<b)return ;
ans= ( ( ( ans * fax[a] )%mod )* inv(fax[b]*fax[a-b],mod) ) %mod;
n/=mod ; m/=mod;
}
return ans;
}
int main()
{
int h,w,n;
init();
while(scanf("%d%d%d",&h,&w,&n)==)
{
for(int i=; i<n; i++)
{
scanf("%d%d",&P[i].x,&P[i].y);
}
P[n].x=h;P[n].y=w;
sort(P,P+n+); for(int i=; i<=n; i++)
{
dp[i]=lucas(P[i].x+P[i].y-,P[i].y-);
for(int j=; j<i; j++)
if(P[i].x>=P[j].x&&P[i].y>=P[j].y)
{
dp[i]= ( ( dp[i] - ( dp[j]*lucas(P[i].x-P[j].x+P[i].y-P[j].y ,P[i].x-P[j].x ) )%mod)+mod)%mod;
}
}
printf("%I64d\n",dp[n]);
} return ;
}

2015-09-09---opas


E. Gerald and Giant Chess的更多相关文章

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

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

  3. Gerald and Giant Chess

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

  4. CF559C Gerald and Giant Chess

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

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

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

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

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

  8. Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess

    这场CF又掉分了... 这题题意大概就给一个h*w的棋盘,中间有一些黑格子不能走,问只能向右或者向下走的情况下,从左上到右下有多少种方案. 开个sum数组,sum[i]表示走到第i个黑点但是不经过其他 ...

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

随机推荐

  1. input的placeholder在ie9下不兼容的结局办法。

      /*      IE9placeholder支持      */     if(!placeholderSupport()){   // 判断浏览器是否支持 placeholder         ...

  2. 【PyQt5-Qt Designer】QLineEdit 文本输入

    QLineEdit 文本输入 一.QlineEdit 基本方法 setAlignment() 按固定值方式对齐文本 Qt.AlignLeft:水平方向靠左对齐 Qt.AlignRight:水平方向靠右 ...

  3. python QT 编程之路

    pyQT4  的Wheel 下载 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4 python发送GET 或者 POST请求 https://www ...

  4. 利用Linux的硬连接删除MySQL大文件

    利用Linux的硬连接删除MySQL大文件 http://blog.csdn.net/wxliu1989/article/details/22895201 原理:硬链接基础当多个文件共同指向同一ino ...

  5. LigerUi自动检索输入

    var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASI ...

  6. 【转】MySQL的学习--触发器

    MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器 在MySQL中,创建触发器语法如下 ...

  7. Scala对class/object反射

    近期有需求,要根据解析字符串,根据字符串的内容去调用方法.想到的基本就是使用反射.但是基本上能找到的资料,全没有讲scala的反射.只有零星点点的讲解.大部分都是用scala的语法写java反射最后翻 ...

  8. throws和throw的区别

    throws是声明在方法上,告诉调用者这个方法可能会出现的问题.格式  :   方法()   throws  自定义异常类(异常类)    就是在这个方法里面会出问题时,new  throw时,    ...

  9. CentOS里alias命令

    alias命令 功能描述:我们在进行系统的管理工作一定会有一些我们经常固定使用,但又很长的命令.那我们可以给这些这一长串的命令起一个别名.之后还需要这一长串命令时就可以直接以别名来替代了.系统中已经有 ...

  10. jsonServer 造个假的服务器传递数据接口 再用axois来请求数据