CF559C Gerald and Giant Chess
Gerald and Giant Chess
题面翻译
给定一个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
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:
Next
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
样例 #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 那篇题解中已经提到过了,不过为了加深印象还是再写一遍。
因为
先考虑如果不做任何限制从
设
因为用到了组合数,所以需要计算逆元,如果在转移的过程中计算逆元会导致时间复杂度到
边界值:
最大值的逆元用快速幂算出即可。
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步