列表

详情


NC51208. Gerald and Giant Chess

描述

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×wfield, 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 .
Next n lines contain the description of black cells. The i-th of these lines contains numbers — 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 

示例1

输入:

3 4 2
2 2
2 3

输出:

2

示例2

输入:

100 100 3
15 16
16 15
99 88

输出:

545732279

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 70ms, 内存消耗: 3560K, 提交时间: 2020-01-16 22:57:37

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod=1e9+7;
int dp[2010];
long long fac[200010],inv[200010];
long long quick_pow(long long a,long long b)
{
	long long ans=1;
	while(b)
	{
		if(b&1)
			ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int C(int n,int m)
{
	return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
struct point
{
	int x,y;
	 bool operator < (const point &u)
    {
        return x==u.x ? y<u.y:x<u.x;
    }
}p[2010];
bool cmp(point A,point B)
{
	if(A.x==B.x)
		return A.y<B.y;
	return A.x<B.x;
}
int main()
{
	fac[0]=1;
	inv[0]=1;
	for(int i=1;i<=200000;i++)	
	{
		fac[i]=fac[i-1]*i%mod;
		inv[i]=quick_pow(fac[i],mod-2);
	}
	int h,w,n;
	cin>>h>>w>>n;
	for(int i=1;i<=n;i++)
		scanf("%d %d",&p[i].x,&p[i].y);
	sort(p+1,p+1+n,cmp);
	p[n+1].x=h;p[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		dp[i]=C(p[i].x+p[i].y-2,p[i].x-1);
		for(int j=1;j<=i-1;j++)
		{
			if(p[j].x>p[i].x||p[j].y>p[i].y)
				continue;
			dp[i]=(dp[i]-(long long)dp[j]*C(p[i].x+p[i].y-p[j].x-p[j].y,p[i].x-p[j].x))%mod;
		}
	}
	cout<<(dp[n+1]+mod)%mod<<endl;
	return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 48ms, 内存消耗: 3548K, 提交时间: 2022-08-12 16:56:42

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=25000+7,INF = 0x3f3f3f3f;
const int N=2010;
int f[N],h,w,n;
ll jc[200010],jcinv[200010];
pair<int,int>a[N];
#define x first
#define y second
int C(int n,int m){return jc[n]*jcinv[m]%mod*jcinv[n-m]%mod;}
ll power(ll a,int b)
{
    ll c=1;
    for(;b;b>>=1)
    {
        if(b&1)c=c*a%mod;
        a=a*a%mod;
    }
    return c;
}
int main()
{
    jc[0]=1,jcinv[0]=1;
    for(int i=1;i<=200001;i++)
    {
        jc[i]=jc[i-1]*i%mod;
        jcinv[i]=power(jc[i],mod-2);
    }
    cin>>h>>w>>n;
    for(int i=1;i<=n;i++)scanf("%d%d",&a[i].first,&a[i].second);
    sort(a+1,a+1+n);
    a[n+1].first=h,a[n+1].second=w;
    for(int i=1;i<=n+1;i++)
    {
        int ix=a[i].first,iy=a[i].second;
        f[i]=C(ix+iy-2,ix-1);
        for(int j=1;j<i;j++)
        {
            int jx=a[j].first,jy=a[j].second;
            if(jx>ix||jy>iy)continue;
            f[i]=(f[i]-(ll)f[j]*C(ix+iy-jx-jy,ix-jx))%mod;
        }
    }
    cout<<(f[n+1]+mod)%mod<<endl;
    return 0;
}

C++ 解法, 执行用时: 192ms, 内存消耗: 3560K, 提交时间: 2021-09-21 17:14:48

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
	int x,y;
};
int cmp(node x,node y){
	if(x.x==y.x) return x.y<y.y;
	return x.x<y.x;
}
node p[2010];
ll dp[2010],mod=1e9+7;
ll a,b,n,jc[200010],inv[200010];
ll qm(ll x,int y);
int main(){
	scanf("%lld%lld%lld",&a,&b,&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&p[i].x,&p[i].y);
	}
	jc[0]=1;
	for(int i=1;i<=200000;i++) jc[i]=jc[i-1]*i%mod;
	for(int i=0;i<=200000;i++) inv[i]=qm(jc[i],mod-2);
	sort(p+1,p+n+1,cmp);
	p[n+1].x=a,p[n+1].y=b;
	for(int i=1;i<=n+1;i++){
		int x=p[i].x,y=p[i].y;
		dp[i]=jc[x+y-2]*inv[x-1]%mod*inv[y-1]%mod;
		for(int j=1;j<i;j++){
			int xx=p[j].x,yy=p[j].y;
			if(x>=xx&&y>=yy) dp[i]=((dp[i]-dp[j]*jc[x-xx+y-yy]%mod*inv[x-xx]%mod*inv[y-yy]%mod)%mod+mod)%mod;
		}
	//	cout<<i<<" "<<dp[i]<<endl;
	}
	printf("%lld",dp[n+1]);
	return 0;
} 
ll qm(ll x,int y){
	ll ans=1;
	while(y){
		if(y&1) ans=ans*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ans;
}

上一题