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 ...
随机推荐
- Sublime Text 3 文本编辑器
1.安装下载 下载地址:http://www.cr173.com/soft/121149.html http://www.xiazaiba.com/html/24343.html 官网 http:// ...
- Xcode6与Xcode5中沙盒的变动以及偏好设置目录的变动
1.Xcode6模拟器路径与Xcode5模拟器路径对比: (1)Xcode5中模拟器路径为:/Users/用户名/Library/Application Support/iPhone Simulato ...
- Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【转】使用sklearn优雅地进行数据挖掘
这里是原文 目录 使用sklearn进行数据挖掘 1.1 数据挖掘的步骤 1.2 数据初貌 1.3 关键技术并行处理 并行处理 2.1 整体并行处理 2.2 部分并行处理流水线处理自动化调参持久化回顾 ...
- 【.NET】传智播客第【19】期就业班视频(高清无加密)
[.NET]传智播客第[19]期就业班视频(高清无加密) 下载地址:http://fu83.cn/thread-85-1-1.html
- win7系统cmd命令切换到指定文件夹目录
win7 系统下的cmd命令,直接cd命令切换盘符和以往有些不同,现在默认只能在当前盘符中改变目录,如果要改变盘符则需要多加一个/d命令.如下图所示:(对cd命令的帮助 大家可借助help cd命令进 ...
- vim环境设置和自动对齐
只要在 /etc/vimrc中加上这两句就行了set autoindentset smartindent------------------------------------------------ ...
- bt协议详解 DHT篇(下)
bt协议详解 DHT篇(下) 最近开发了一个免费教程的网站,产生了仔细了解bt协议的想法,这篇文章是bt协议详解系列的第三篇,后续还会写一些关于搜索和索引的东西,都是在开发这个网站的过程中学习到的技术 ...
- storm基础框架分析
背景 前期收到的问题: 1.在Topology中我们可以指定spout.bolt的并行度,在提交Topology时Storm如何将spout.bolt自动发布到每个服务器并且控制服务的CPU.磁盘等资 ...
- 同步git修改文件到远端服务器脚本
#!/usr/bin/perl -w @files=`git status -s` ; @sync_files = (); foreach (@files) { ); # 固定前2个字符为状态 + 1 ...