codeforces(559C)--C. Gerald and Giant Chess(组合数学)
2 seconds
256 megabytes
standard input
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.
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.
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.
3 4 2
2 2
2 3
2
100 100 3
15 16
16 15
99 88
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 ;
}
codeforces(559C)--C. Gerald and Giant Chess(组合数学)的更多相关文章
- 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 ...
- 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的网格,让你 ...
- 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 ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- Gerald and Giant Chess
Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- CF559C Gerald and Giant Chess
题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- Codeforces 559C Gerald and Giant Chess【组合数学】【DP】
LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...
- 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)不经过坏点有多少条路径. 先把这些坏点排 ...
随机推荐
- 一个简单的假vue全家桶(vue+vue-router+require)
首先说明我觉得这是一个比较好理解的vue全家桶(虽然是假的),模块化也是用require来做的,而且如果后期有必要压缩我也会用gulp来做 1.依赖个个本地模块,require只是用来载入page,这 ...
- jquery如何判断元素是否被点击、属性操作、class操作
1.通过点击事件发生后,改变标志位的值,记录点击状态 function(){ var isClick = false; $('#test').click(function(){isClick = tr ...
- crontab定时任务详解
1.安装crontab:yum install crontabs 说明:/sbin/service crond start //启动服务/sbin/service crond stop //关闭服务/ ...
- centos7 ping127.0.0.1不通
ping 127.0.0.1,localhost和本地ip都不通,所有的配置也是正确的 检查下是否禁止了ping vim /proc/sys/net/ipv4/icmp_echo_ignore_all ...
- pgmagick,pil不保存图片并且获取图片二进制数据记录
PIL和pgmagick都是python中图像处理的库,只不过PIL功能更强大 pgmagick和PIL中对数据进行调整后经常需要调用write或者save方法保存图片,然后在读取图片的内容,这样很麻 ...
- java 把json对象中转成map键值对
相关:Json对象与Json字符串的转化.JSON字符串与Java对象的转换 本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据 比如json数据如下: {responseHeade ...
- [Linux]屏幕输出控制
专门的术语叫做ANSI Escape sequences(ANSI Escape codes),题目并不恰当,与其说是屏幕输出控制,不如说是通过bash在兼容VT100的终端上进行输出. 主要有以下类 ...
- springboot集成mybatis-generator
首先上下成功后的效果: 配置非常简单,我们是通过maven插件来进行的,一共只需要3步: 第一步添加mysql依赖和mysql的maven插件: 由于是非常简单的spring+mysql的原始项目,我 ...
- Java并发包——Blockingqueue,ConcurrentLinkedQueue,Executors
背景 通过做以下一个小的接口系统gate,了解一下mina和java并发包里的东西.A系统为javaweb项目,B为C语言项目,gate是本篇须要完毕的系统. 需求 1. A为集群系统,并发较高,会批 ...
- Linux——CentOS 6.3下PostgreSQL 的安装与配置
一.简介 PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统.有些特性甚至连商业数据库 都不具备.这个起源于伯 ...