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
分析:把不能走的格子和终点都设为要达到的目标;
则要到达当前目标,就是从总方案数中减去从之前到达目标到达现在目标的方案数;
预处理排序,阶乘,逆元即可;
另外O(n)求1-n关于MOD的乘法逆元:inv[i] = ( MOD - MOD / i ) * inv[MOD%i] % MOD;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t;
ll fac[maxn],inv[maxn];
void init()
{
fac[]=inv[]=fac[]=inv[]=1LL;
for(int i=;i<=maxn-;i++)
{
fac[i]=fac[i-]*i%mod;
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
}
for(int i=;i<=maxn-;i++)
inv[i]=inv[i]*inv[i-]%mod;
}
struct node
{
int x,y;
ll num;
bool operator<(const node&p)const
{
return x==p.x?y<p.y:x<p.x;
}
}a[maxn];
ll gao(int x,int y)
{
int i,j;
ll ans=;
ans=ans*fac[x+y-]%mod;
ans=ans*inv[x-]%mod;
ans=ans*inv[y-]%mod;
return ans;
}
int main()
{
int i,j;
init();
scanf("%d%d%d",&n,&m,&k);
rep(i,,k-)scanf("%d%d",&a[i].x,&a[i].y);
a[k].x=n,a[k].y=m;
sort(a,a+k+);
rep(i,,k)
{
a[i].num=gao(a[i].x,a[i].y);
rep(j,,i-)a[i].num=(a[i].num-a[j].num*gao(a[i].x-a[j].x+,a[i].y-a[j].y+)%mod+mod)%mod;
}
printf("%lld\n",a[k].num);
//system("Pause");
return ;
}
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 ...
- 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 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)不经过坏点有多少条路径. 先把这些坏点排 ...
随机推荐
- web开发——水到渠成
1.tomcat中设置编码格式: 打开server.xml,添加下面红色框框里面的内容.
- C#项目转php工作记录
1.Visual Studio Ultimate 2012 静态激活密钥 RBCXF-CVBGR-382MK-DFHJ4-C69G8 http://blog.csdn.net/jpzy520/arti ...
- python常用正则表达式
匹配特定数字:^[1-9]\d*//匹配正整数−[1−9]\d∗ //匹配负整数^-?[1-9]\d*//匹配整数[1−9]\d∗|0 //匹配非负整数(正整数 + 0)^-[1-9]\d*|0// ...
- VBS获取Ini配置文件一个节点下的所有字段的值
''* 功能:使用VBS读取ini文件中指定节点下的所有值'* 输入参数:inipath :ini文件的地址'* initypes :ini文件中包含在"["和 ...
- IoC容器Autofac正篇之依赖注入(七)
依赖注入,这个专业词我们可以分为两个部分来理解: 依赖,也就是UML中描述事物之间关系的依赖关系,依赖关系描述了事物A在某些情况下会使用到事物B,事物B的变化会影响到事物A: 注入,医生通过针头将药物 ...
- ECOS高可用集群
此架构由8台PC .2台防火墙.2台24口三层交换机组成, 注意点: 1)Load Balance:Haprxoy+keepalived 实现高可用. 2)web:Nginx+php-fpm 3)DB ...
- iOS 上架提示ipad需要显示四个方位,而我们只能竖屏的时候的解决办法
勾选requires sull screen
- Redis配置文件 翻译 V3.2版本
# Redis配置文件例子. # # 注意:为了能读取到配置文件,Redis服务必须以配置文件的路径作为第一个参数启动 # ./redis-server /path/to/redis.conf # 关 ...
- vs当前不会命中断点,还没有为该文档加载任何符号
今天发布网站之后,附加进程却怎么页不能命中断点,后来发现原来,我将发布的web.config文件覆盖掉了新生成的配置文件,其中一项:<compilation debug="false& ...
- spring securiy使用总结
我们常见的几个功能: 注册后直接登录,并且remember-me这种在网上找到很多注册后登录的,但是remember-me没有.其实解决方案还是看源码比较方便.a. 装载authenticationM ...