51nod 1486 大大走格子

1486 大大走格子
基准时间限制:1 秒 空间限制:131072 KB

有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数。

Input
单组测试数据。
第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 2000),表示棋盘的行和列,还有不能走的格子的数目。
接下来n行描述格子,第i行有两个整数ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w),表示格子所在的行和列。
输入保证起点和终点不会有不能走的格子。
Output
输出答案对1000000007取余的结果。
Input示例
3 4 2
2 2
2 3
Output示例
2
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 const int N=200005;
38 ll fac[N],inv[N],dp[2005];
39 struct node{
40     int r,c;
41 }f[2005];
42 bool cmp(node a,node b){
43     return (a.r<b.r)||(a.r==b.r && a.c<b.c);
44 }
45 inline ll C(int m,int n){
46     if (m>n) return 0;
47     return mo(mo(fac[n]*inv[m],pp)*inv[n-m],pp);
48 }
49 int main(){
50     int h=read(),w=read(),n=read(),i;
51     for (fac[0]=1,i=1;i<=h+w;i++) fac[i]=mo(fac[i-1]*i,pp);
52     for (inv[1]=1,i=2;i<=h+w;i++) inv[i]=mo((pp-pp/i)*inv[pp%i],pp);
53     for (inv[0]=1,i=1;i<=h+w;i++) inv[i]=mo(inv[i-1]*inv[i],pp);
54     for (int i=1;i<=n;i++){
55         f[i].r=read(),f[i].c=read();
56     }
57     sort(f+1,f+n+1,cmp);
58     ll ans=C(h-1,h-1+w-1);
59     for (int i=1;i<=n;i++){
60         dp[i]=C(f[i].r-1,f[i].r-1+f[i].c-1);
61         for (int j=1;j<i;j++){
62             if (f[j].r<=f[i].r && f[j].c<=f[i].c){
63                 dp[i]=mo(dp[i]-mo(dp[j]*C(f[i].r-f[j].r,f[i].r-f[j].r+f[i].c-f[j].c),pp),pp);
64             }
65         }
66         ans=mo(ans-mo(dp[i]*C(h-f[i].r,h-f[i].r+w-f[i].c),pp),pp);
67     }
68     printf("%lld",ans);
69     return 0;
70 }
View Code
原文地址:https://www.cnblogs.com/SXia/p/7654399.html