E. Gerald and Giant Chess
2 seconds
256 megabytes2015-09-09
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 我们对着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的更多相关文章
- 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 559C Gerald and Giant Chess
C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 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 ...
- 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 ...
- 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 ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
这场CF又掉分了... 这题题意大概就给一个h*w的棋盘,中间有一些黑格子不能走,问只能向右或者向下走的情况下,从左上到右下有多少种方案. 开个sum数组,sum[i]表示走到第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)不经过坏点有多少条路径. 先把这些坏点排 ...
随机推荐
- 洛谷P4247 序列操作 [清华集训] 线段树
正解:线段树 解题报告: 传送门! 通过这题我get了一个神奇的,叫,线段树五问的东西hhhh 听起来有点中二但感觉真正做题的时候还是比较有用的,,,?感觉会让条理清晰很多呢,所以放一下QwQ →每个 ...
- 使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径
使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径 经常地,我们要在jsp等页面引入像js,css这样的文件,但是在服务器来访问的时候,这时间就有关到相对路径与绝对路径了.像网页 ...
- 【SQL】SQL存储过程相关当前理解。(@temp=……)
1.下图左侧红框中的是SQL的存储过程,是程序同SQL之间互相调用的函数.——这里先作为了解不做深入研究. 比如客户通过ATM提款机取200元钱,程序中提交取200元钱操作,后续可能会进入SQL进行一 ...
- python基于django编写api+前端后端分离
有用 https://segmentfault.com/a/1190000016049962#articleHeader2 python的前后端分离(一):django+原生js实现get请求 htt ...
- zabbix 3.2源码安装
环境: centos7.2 nginx 1.10.3 mysql 5.5.38 php 5.5.38 一.zabbix介绍 1.Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企 ...
- HBase 的MOB压缩分区策略介绍
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zNZQhb07Nr/article/details/79832392 HBase应用场景很广泛.社区 ...
- 012-Future、FutureTask、CompletionService 、CompletableFuture
一.概述 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这两种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果.如果需要获取执行结果,就必须通过共享变量或 ...
- sap 创建odata服务,通过http向数据库 进行增删改查
https://blog.csdn.net/stone0823/article/details/71057172 1:通过 事物码 se11 创建 数据库表 zemp.表 zemp中 含有empid ...
- 虚拟货币ICO是什么意思 看完秒懂
有这样一个市场,与90年代的互联网泡沫及其相似,它被许多金融界大咖怒指为丧失道德底线的圈钱工具,更被投资者而疯狂追捧.是的,没错,它就是近二年火遍全球的虚拟货币ICO.那么,对于很多投资小白来说,虚拟 ...
- 【Linux】-NO.7.Linux.3.Maven.1.001-【CentOS 7 Install Maven 3.5】-
1.0.0 Summary Tittle:[Linux]-NO.7.Linux.3.Maven.1.001-[CentOS 7 Install Maven 3.5]- Style:Linux Seri ...