CF559C Gerald and Giant Chess

Gerald and Giant Chess

CF599C (Luogu)

题面翻译

给定一个H*W的棋盘,棋盘上只有N个格子是黑色的,其他格子都是白色的。在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动到黑色格子中。求这个卒从左上角移动到右下角,一共有多少种可能的路线。

题目描述

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<=10^{5},1<=n<=2000\) ).

Next \(n\) lines contain the description of black cells. The \(i\) -th of these lines contains numbers \(r_{i},c_{i}\) ( \(1<=r_{i}<=h,1<=c_{i}<=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 \(10^{9}+7\) .

样例 #1

样例输入 #1

3 4 2
2 2
2 3

样例输出 #1

2

样例 #2

样例输入 #2

100 100 3
15 16
16 15
99 88

样例输出 #2

545732279

Solution

一道用到容斥的 DP 题,算是 CF722F 的弱化版了。

虽然做法在 CF722F 那篇题解中已经提到过了,不过为了加深印象还是再写一遍。

因为 \(n\times m\) 的方格图中 \(n,m\) 的数据规模在 \(1e5\) 这一数量级,所以显然是不能够使用同过河卒那道题相同的递推法,注意到 \(k\) 的规模仅有 \(2e3\),所以考虑在特殊点上下文章。

先考虑如果不做任何限制从 \((1,1)\) 到达 \((n,m)\) 的方案数,可以知道是 \(\begin{pmatrix}n+m-2\\n-1\end{pmatrix}\)。那么在这基础上定义 \(way(j,i)\) 表示从点 \(j\) 到点 \(i\) 的方案数,那么可以推导得出是 \(\begin{pmatrix}x_i-x_j+y_i-y_j\\x_i-x_j\end{pmatrix}\)

\(f_i\) 表示从起点开始仅经过第 \(i\) 个特殊点的方案数,用容斥的方法,先计算出总路径数,然后减去经过其它特殊点的路径数即可。可以推导出:

\[f_i=\begin{pmatrix} x_i+y_i-2\\ x_i-1 \end{pmatrix} - \sum \limits _{j=1}^{i-1} f_j \times way(j,i) \]

因为用到了组合数,所以需要计算逆元,如果在转移的过程中计算逆元会导致时间复杂度到 \(\mathcal O(n^2 \log)\),并且注意到组合数中用到的逆元都是阶乘的逆元,所以可以预处理出阶乘和阶乘的逆元。将 \(x!\) 表示成 \(fac_i\)\(x!^{-1}\) 表示为 \(inv_x\),那么可以得出:

\[fac_i=fac_{i-1}\times i \]

\[inv_{i-1}=inv_i\times i \bmod 1e9+7 \]

边界值:

\[fac_0=1 \]

\[inv_{max}=fac_{max}\ ^{1e9+5} \bmod 1e9+7 \]

最大值的逆元用快速幂算出即可。

Code

#include<bits/stdc++.h>
#define int long long
#define X(a) bar[a].x
#define Y(a) bar[a].y
using namespace std;
template<typename T> void read(T &k)
{
	k=0;T flag=1;char b=getchar();
	while (!isdigit(b)) {flag=(b=='-')?-1:1;b=getchar();}
	while (isdigit(b)) {k=k*10+b-48;b=getchar();}
	k*=flag;
}
template<typename T> void write(T k) {if (k<0) {putchar('-'),write(-k);return;}if (k>9) write(k/10);putchar(k%10+48);}
template<typename T> void writewith(T k,char c) {write(k);putchar(c);}
int n,m,k;
const int barrierSize=2e3,mod=1e9+7,mapSize=2e5;
struct POINT{
	int x,y;
	bool operator< (const POINT &a) const {return x!=a.x?x<a.x:y<a.y;}
}bar[barrierSize+5];
int Fpow(int a,int b)
{
	int res=1,base=a%mod;
	while (b)
	{
		if (b&1) res=res*base%mod;
		base=base*base%mod,b>>=1;
	}
	return res;
}
int fac[mapSize+5],inv[mapSize+5];
int f[barrierSize+5];
bool flag=0;
void init()
{
	fac[0]=1;
	for (int i=1;i<=mapSize;i++) fac[i]=fac[i-1]*i%mod;
	inv[mapSize-1]=Fpow(fac[mapSize-1],mod-2);
	for (int i=mapSize-1;i;i--) inv[i-1]=inv[i]*i%mod;
}
int C(int x,int y) {return fac[x]*inv[y]%mod *inv[x-y]%mod;}
int way(POINT b,POINT a) {return C(a.x-b.x+a.y-b.y,a.x-b.x);}
signed main()
{
	init();
	read(n),read(m),read(k);
	for (int i=1;i<=k;i++)
	{
		int x,y;read(x),read(y);
		if (x==n && y==m) flag=1;
		bar[i].x=x,bar[i].y=y;
	}
	if (!flag) bar[++k]=(POINT){n,m};
	sort(bar+1,bar+k+1);
	for (int i=1;i<=k;i++)
	{
		f[i]=C(X(i)+Y(i)-2,X(i)-1);
		for (int j=1;j<i;j++) 
			if (X(j)<=X(i) && Y(j)<=Y(i)) 
				f[i]=((f[i]-f[j]*way(bar[j],bar[i]))%mod+mod)%mod;
	}
	writewith(f[k],'\n');
	return 0;
}
posted @ 2022-08-15 09:19  Hanx16Msgr  阅读(31)  评论(0编辑  收藏  举报