题目地址


易错点:

  • 输出时需要用(f[n+1]+mod)%mod.(防止负数)
  • f[i]转移时需要开long long.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int SIZE=4e3,MOD=1e9+7,MAXH=4e5;
ll jc[MAXH],jcinv[MAXH];
int C(int n,int m){
	return jc[n]*jcinv[m]%MOD*jcinv[n-m]%MOD;
}
struct Node{
	int x,y;
	bool operator <(Node another)const{
		return x==another.x?y<another.y:x<another.x;
	}
}a[SIZE];
ll power(ll a,int b){
	ll ans=1;
	while(b){
		if(b&1)ans=ans*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return ans;
}
int f[SIZE];
int main(){
	jc[0]=1,jcinv[0]=1;
	for(int i=1;i<=2e5;i++){
		jc[i]=jc[i-1]*i%MOD;
		jcinv[i]=power(jc[i],MOD-2);
	}
	int h,w,n;
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
	}
	sort(a+1,a+1+n);
	a[n+1].x=h,a[n+1].y=w;
	for(int i=1;i<=n+1;i++){
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
		for(int j=1;j<i;j++){
			if(a[j].x>a[i].x||a[j].y>a[i].y)continue;
			f[i]=(f[i]-(ll)f[j]*C(a[i].x+a[i].y-(a[j].x+a[j].y),a[i].x-a[j].x))%MOD;
		}
	}
	printf("%d\n",(f[n+1]+MOD)%MOD);
	return 0;
}