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

题目大意:给出一个棋盘为h*w,如今要从(1,1)到(h,w)。当中有n个黑点不能走,问有多少种可能从左上到右下(1,1和h,w永远是能够走的)

计算左上到右下的方法假设不考虑黑点的话,sum=C(h+w)(h)

由于存在黑点i(x,y),所以用所以计算从左上到黑点的方法有sum[i] = C(x+y)(x)。当中假设在黑点的左上还有黑点j(u,v),那么应该减去sum[j]*C(x-u+y-v)(y-u)。去掉全部在左上的黑点的影响就能够得到由左上到第i点的真正的方法数

从左上的第一个黑点。一直计算到右下(h,w)

注意:

1、C(h+w)(h)的数据非常大。C(h+w)(h) = (h+w)!/( h!*w! )。用数组fac记录下每一个数的阶乘

2、计算组合数的时候有除法,能够用逆元来做a/b%mod = a*(b^(mod-2))%mod。计算i的阶乘的逆元inv[i],第在对阶乘求逆元的方法还有inv[ fac[i] ] = inv[ fac[i+1] ]*(i+1)%mod ,这种方法仅对连续的阶乘有效。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
const LL MOD = 1e9+7 ;
struct node{
LL x , y ;
}p[3100];
LL h , w , n ;
LL fac[310000] , inv[310000] ;
LL sum[3100] ;
int cmp(node a,node b) {
return a.x < b.x || (a.x == b.x && a.y < b.y) ;
}
LL pow(LL x,LL k) {
LL ans = 1 ;
while( k ) {
if( k&1 ) ans = ans*x%MOD ;
k = k>>1 ;
x = (x*x)%MOD ;
}
return ans ;
}
void init() {
LL i , j , c ;
fac[0] = inv[0] = 1 ;
for(i = 1 ; i <= h+w ; i++)
fac[i] = (fac[i-1]*i)%MOD ;
c = max(h,w) ;
inv[c] = pow(fac[c],MOD-2) ;
for(i = c-1 ; i > 0 ; i--) {
inv[i] = inv[i+1]*(i+1)%MOD ;
}
}
int main() {
LL i , j ;
LL ans ;
while( scanf("%I64d %I64d %I64d", &h, &w, &n) != EOF ) {
init() ;
for(i = 0 ; i < n ; i++)
scanf("%I64d %I64d", &p[i].x, &p[i].y) ;
p[n].x = h ; p[n++].y = w ;
sort(p,p+n,cmp) ;
int x1 , y1 , x2 , y2 ;
for(i = 0 ; i < n ; i++) {
x1 = p[i].x-1 ; y1 = p[i].y-1 ;
sum[i] = fac[x1+y1]*inv[x1]%MOD*inv[y1]%MOD ;
for(j = 0 ; j < i ; j++) {
if( p[j].x <= p[i].x && p[j].y <= p[i].y ) {
x2 = x1 - p[j].x+1 ; y2 = y1 - p[j].y+1 ;
sum[i] = (sum[i]-fac[x2+y2]*inv[x2]%MOD*inv[y2]%MOD*sum[j]%MOD)%MOD ;
if( sum[i] <= 0 ) sum[i] = (sum[i]+MOD)%MOD;
}
}
}
printf("%I64d\n", sum[n-1]) ;
}
return 0 ;
}

最新文章

  1. PHP 图片生成文字
  2. java 环境配置
  3. codeforces 451D Count Good Substrings
  4. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
  5. win7电脑安装wamp出现httpd.exe无法找到组件MSVCR100.dll的解决办法
  6. C#事件(event)解析
  7. 百度云盘demo
  8. 子查询注意这几点, 就可以写出好的sql语句
  9. WPF AutoGeneratingColumn 绑定下拉框
  10. Qt5中QMessageBox::warning()的第一个参数写this时出错
  11. Shell工具:jsondiff.sh
  12. CSS3实现时间轴效果
  13. AndroidSlidingUpPanel 使用控制和简单的分析方法
  14. 跳水Hibernate(一)实例解说
  15. MySql数据库中,判断表、表字段是否存在,不存在就新增
  16. Mac系统下 PHP7安装Swoole扩展 教程
  17. VUE路由转场特效,WebAPP的前进与后退
  18. leetcode650&mdash;2 Keys Keyboard
  19. python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool
  20. 【矩阵快速幂】【杭电OJ1757】

热门文章

  1. Android API level 版本对应关系
  2. iOS:Objective-c的MD5/SHA1加密算法的实现
  3. ISP图像调试工程师——边缘增强(熟悉图像预处理和后处理技术)
  4. 使用Python开发SQLite代理服务器(转载)
  5. SSH 错误解决案例1:Read from socket failed: Connection reset by peer
  6. 使用history.pushState()和popstate事件实现AJAX的前进、后退功能
  7. C语言-编译
  8. YII用户注冊表单的实现熟悉前台各个表单元素操作方式
  9. 保存Hive查询结果的方法
  10. 【LeetCode OJ 016】3Sum Closest