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 ...
随机推荐
- <<Effective Java>>之Comparable接口的实现约定
对于BigDecimal类在HashSet和TreeSet中 new BigDecimal("1.00") new BigDecimal("1.0") 在Has ...
- mysqli常用错误处理函数
mysqli扩展库包含三个类库,分别是mysqli连接库,mysqli_result处理结果集库和预处理库: 当使用select语句返回的结果集就是mysqli_result类库的对象,所以就可以用这 ...
- 11Mybatis_mybatis开发Dao的方法
在介绍开发Dao的方法之前先介绍下SqlSession. 1.先介绍一下SqlSessionFactoryBuilder:通过SqlSessionFactoryBuilder创建会话工厂SqlSess ...
- 16SpringMvc_在业务控制方法中写入User,Admin多个模型收集参数——引出问题
上面文章时普通的业务那个方法中收集一个实体类,这篇文章想收集两个实体类. 文本要做的是:在person.jsp页面上,有两个表单.分别是普通用户和管理员用户的表单(普通用户的表单和管理员用户的表单里面 ...
- PHP基础13:数组排序
<?php //1.对数组进行升序排序 - sort() $cars=array("VOLVO","BMW","BYD"); $fri ...
- python数字图像处理(2):图像的读取、显示与保存
skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的.为了方便练习,也提供一个data模块,里面嵌套了一些示例图片,我们可以直接使用. 引入skimage模块可用: from sk ...
- 三层ViewPager嵌套 的事件处理
这么多ViewPager嵌套在一起肯定会遇到冲突 不信你试试(笑脸) 下面来说怎么解决.....太为难我这个菜b了 设置外部的父控件不要拦截我子控件的事件,通过重写ViewPager的 @Overri ...
- MSSQL 分组后取每组第一条(group by order by)
查询中经常遇到这种查询,分组后取每组第一条.分享下一个SQL语句: --根据 x 分组后.根据 y 排序后取第一条 select * from ( select ROW_NUMBER() over(p ...
- Bootstrap系列 -- 26. 下拉菜单标题
Bootstrap下拉菜单中使用 dropdown-header 来显示菜单标题,和上一篇说道的分割线一样 <div class="dropdown"> <but ...
- Quartz.Net在windows服务中的使用
写在前面 这几天在弄一个项目,需要定时抓取一些数据,当时也想直接用timer算了.因为之前也弄过这样的项目,但是一想,已经用过了,再去使用同一种思路,未免太乏味了.就换了一种新玩法.这里将之前看到的一 ...