Codeforces Round #313 (Div. 1) C - Gerald and Giant Chess dp

25 篇文章 0 订阅

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 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.

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 modulo 109 + 7.

Sample test(s)
Input
3 4 2
2 2
2 3
Output
2
Input
100 100 3
15 16
16 15
99 88
Output
545732279
题意:给出h*w的矩阵,里面有n个坏点,求从1,1位置走到h,w位置不经过任何一坏点的方案数。

设dp【i】表示起点不经过任何换点到坏点i (假设位置为x1,y1)时的方案数,那么对于之前所以位置满足(x2<=x1&&y2<=y1)的坏点j,即有 dp【i】 -= dp【j】 * j到i的方案数(想一想,这么走保证不重复)为到达(x1,y1)的方案数最后输出dp【n+1】即可,也就是将hw这个点也看成一个坏点。据说bzoj上有原题- -

C(x,y)表示向右向下一共走了x步,其中走y步水平到达当前位置

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <time.h>
#include <vector>
#include <cstdio>
#include <string>
#include <iomanip>
///cout << fixed << setprecision(13) << (double) x << endl;
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define Mp(a, b) make_pair(a, b)
#define asd puts("asdasdasdasdasdf");
typedef long long ll;
//typedef __int64 LL;
const int inf = 0x3f3f3f3f;
const int N = 200100;
const ll mod = 1e9+7;

ll fac[N];
ll dp[2020];	//dp【i】表示起点不经过任何换点到坏点i时的方案数
ll h, w, n;
struct node{
	ll x, y;
	bool operator < (const node &rhs) const {
		if( rhs.x == x )
			return y < rhs.y;
		return x < rhs.x;
	}
}a[2020];

ll exgcd( ll a, ll b, ll &x, ll &y )
{
	if( !b ) {
		x = 1;
		y = 0;
		return a;
	}
	ll gcd = exgcd( b, a%b, x, y );
	ll t = x;
	x = y;
	y = t - (a/b) * x;
	return gcd;
}

ll inverse( ll num )
{
	ll x, y;
	exgcd( num, mod, x, y );
	return (x % mod + mod) % mod;
}

ll C( int a, int b )
{
	ll t1 = fac[a];
	ll t2 = (ll)fac[a-b] * fac[b] % mod;
	ll inv = inverse( t2 );
	return t1 * inv % mod;
}

void fac_init()
{
	fac[0] = 1;
	for( int i = 1; i <= N-90; ++i )
		fac[i] = fac[i-1] * i % mod;
}

int main()
{
	fac_init();
	while( ~scanf("%lld%lld%lld", &h, &w, &n ) ) {
		for( int i = 1; i <= n; ++i ) {
			scanf("%lld%lld", &a[i].x, &a[i].y);
		}
		sort( a+1, a+1+n );
		a[++n].x = h;
		a[n].y = w;
		//for( int i = 1; i <= n; ++i )
		//	printf("%lld %lld\n", a[i].x, a[i].y);
		for( int i = 1; i <= n; ++i ) {
			ll x1 = a[i].x, y1 = a[i].y;
			dp[i] = C( x1+y1 - 2, x1 - 1 );
			for( int j = 1; j < i; ++j ) {
				ll x2 = a[j].x, y2 = a[j].y;
				if( x2 <= x1 && y2 <= y1 ) {
					dp[i] -= ( dp[j] * C( x1+y1 - x2-y2, x1 - x2 ) ) % mod;
					dp[i] = (dp[i] % mod + mod) % mod;
				}
			}
		}
		printf("%lld\n", dp[n]);
	}
	return 0;
}

感谢我A逆元姿势--  --


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值