313.C

来源:互联网 发布:airlaunch 类似软件 编辑:程序博客网 时间:2024/06/08 12:47

C. Gerald and Giant Chess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

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.

Sample test(s)
input
3 4 22 22 3
output
2
input
100 100 315 1616 1599 88
output
545732279

题意 : 10^5加强过河卒(不能用递推O(n^2)好伤心。。)注意到忽略黑点时从左上到任意点(x,y)方案数为组合数C(x+y-2)(x-1)。记第i个黑点方案数为sum[i],则可行方案数为当前组合数减去每个在其左上角黑点走到当前黑点方案数乘以左上角黑点方案数即sum[j] * C(x+y-xx-yy)(x-xx)  (j < i);

另用 乘法逆元(a/b) mod p = (a * b^(p-2)) mod p;


#include<cstdio>

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<map>
#include<cmath>
#include<set>
using namespace std;


const int maxn = 2010;
const long long mo = 1e9 + 7;


struct data{
int x,y;
bool operator < (const data &b) const{
   return x < b.x || (x == b.x && y < b.y);
}
}black[maxn];


long long fac[500010],n,m,k,i,j,sum[maxn];


long long mi(long long x,long long y)
{
long long ret = 1;
for (; y; y >>= 1)
{
if (y & 1) ret = ret * x % mo;
x = x * x % mo;
}
return ret;
}


long long C(int x,int y)
{
return fac[x] * mi(fac[y] * fac[x - y] % mo,mo - 2) % mo;
}


int main()
{
#ifndef ONLINE_JUDGE
#ifndef YZY
 freopen(".in","r",stdin);
 freopen(".out","w",stdout);
#else
 freopen("yzy.txt","r",stdin);
#endif
#endif

cin >> n >> m >> k;
fac[0] = 1;
for (i = 1; i <= m + n; i++) fac[i] = fac[i - 1] * i % mo;
for (i = 1; i <= k; i++) scanf("%d%d",&black[i].x,&black[i].y);
black[k + 1].x = n; black[k + 1].y = m;
sort (black + 1,black + k + 2);
for (i = 1; i <= k + 1; i++)
{
int x = black[i].x;
int y = black[i].y;
sum[i] = C(x + y - 2,x - 1);
for (j = 1; j < i; j++)
if (black[j].x <= x && black[j].y <= y)
{
int xx = black[j].x;
int yy = black[j].y;
sum[i] = (sum[i] - sum[j] * C(x + y - xx - yy,x - xx) % mo + mo) % mo;
}
}
cout << sum[k + 1];
return 0;
}
0 0
原创粉丝点击