ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF559C Gerald and Giant Chess

2022-08-15 09:32:34  阅读:155  来源: 互联网

标签:Giant int Gerald 样例 逆元 pmatrix Chess fac


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;
}

标签:Giant,int,Gerald,样例,逆元,pmatrix,Chess,fac
来源: https://www.cnblogs.com/hanx16msgr/p/16587090.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有