$("head").append('')

题解:P17700 [ICPC 2019 Nanjing R] Chessboard

题目传送门

发现如果有一个凹边就会让凹边的两边最短路变长,所以始终满足凸性长方形。

从 \(n\times m\) 开始,我们一行或一列的删除,因此 \(f(x,y)=f(x-1,y)+f(x,y-1)\)
特别地,\(f(1,1)=1,f(x,1)=f(1,y)=2\)。
因此由杨辉三角,一般的 \(x,y\) 可以得到 \(f(x,y)=4\times C_{n+m-2}^{ n-1}\)。(不难发现这里 \(n-1\) 等价于 \(m-1\))。

其他的就是很简单的预处理逆元和阶乘了。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
int n,m,t,mul[1000005];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b%2)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int C(int a,int b){
	return mul[a]*qpow(mul[b],mod-2)%mod*qpow(mul[a-b],mod-2)%mod;
}
signed main(){
	cin>>t;
	mul[1]=1;
	for(int i=2;i<=1000000;i++)mul[i]=mul[i-1]*i%mod;
	while(t--){
		cin>>n>>m;
		if(n==1&&m==1)cout<<"1\n";
		else if(n==1||m==1)cout<<"2\n";
		else cout<<C(n+m-2,n-1)*4ll%mod<<"\n";
	}
}
posted @ 2026-10-15 21:05  lain_yc  阅读(20)  评论(0)    收藏  举报