CodeForces 559C 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 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.
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 modulo 109 + 7.
3 4 2
2 2
2 3
2
100 100 3
15 16
16 15
99 88
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的更多相关文章
- Codeforces 559C Gerald and Giant Chess【组合数学】【DP】
LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...
- CodeForces 540E - Gerald and Giant Chess(数论)
给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法. 首先要知道从一个点A到另一个点B在没有障碍下有多少种走法.保证A在B的左上方,如图 一共需要走(X+Y)步(图中△x,△y), ...
- CF 559C - Gerald and Giant Chess (组合计数)
\(C_{x+y}^y\)的公式,DP容斥删多余贡献. #include <cstdio> #include <iostream> #include <cstring&g ...
- 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 ...
- 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 ...
随机推荐
- 神奇的GO语言:空接口(interface)
对于go语言来说,设计最精妙的应该是interface了,直白点说interface是一组method的组合.至于更加详细的描述,本文不做介绍,今天谈谈空接口. 空interface(interfac ...
- [tools]神器notepad++
1,现象 notepad++编辑sh文件,放入linux后执行会有问题 2,解决: 2.1dos2unix转换文件 2,2 修改notepad++默认字符集 2,快捷键: ctrl+k 单行.多行注释 ...
- TCP&UDP协议小结
TCP和UDP 传输层功能 网络安全 Tcp可靠性 Tcp流控 Tcp拥塞控制 Tcp运输连接管理 一个网页可能很大,一个数据包传不过来,就需要分段传输. 网络可能拥塞,某段可能丢失.那必须有人监管, ...
- C语言 百炼成钢18
//题目52:用递归打印以下图形 //* //*.*. //*..*..*.. //*...*...*...*... //*....*....*....*....*.... #include<s ...
- JS自定义事件之选项卡
自定义事件是一种处理与DOM产生交互的代码逻辑片段之间耦合的很好的架构方法. 一个简单的jQuery插件——选项卡 让ul列表来响应点击事件.当用户点击一个列表项时,给这个列表项添加一个名为activ ...
- Sql语句里的递归查询
Sql语句里的递归查询 SqlServer2005和Oracle 两个版本 以前使用Oracle,觉得它的递归查询很好用,就研究了一下SqlServer,发现它也支持在Sql里递归查询举例说明:Sql ...
- LeetCode:Jump Game I II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- IOS开发之——保存图片到相册的功能实现
//保存 UIButton *saveBtn = [[UIButton alloc] init]; // saveBtn.frame = CGRectMake((screenWi ...
- IT男的”幸福”生活"续1
IT男的”幸福”生活"续1 我和LL开始下步追妹计划...... 知彼知已,方能把握机会.没想到孙子兵法太给力了,据LL了解,MM(她)是湖北人,找对象一般不会考虑外省的.高度165左右,相 ...
- QQ提醒的功能
原文出处:http://www.cnblogs.com/xiexingen/archive/2013/04/09/3009921.html http://qzs.qq.com/snsapp/app/b ...