参考题解:http://blog.csdn.net/mrazer/article/details/52047436
看到题首先是要想到容斥,因为之前看的一篇容斥的文章讲到过。不过里面把这部分内容划掉了,因为讲的有问题。。。。其次呢,想到机器人走方格的题目,毕竟这只是比那个多了个不能走的格子,不过想了想,还是不会做啊
还是好好看题解吧。。。
我把题解中的状态转移方程展开了几项,正好是++–的容斥形式。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
const int MAXN = 1e5+10;
const int MAXP = 2010;
struct Point
{
int x,y;
} p[MAXP];
LL fac[MAXN*2];
LL inv[MAXN*2];
LL res[MAXP];
int h,w,n;
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
inline void out(LL x)
{
if(x>9) out(x/10);
putchar(x%10+'0');
}
void println(LL num)
{
out(num);
puts("");
}
LL mpow(LL a,LL b)
{
LL ret = 1;
while(b)
{
if(b&1) ret = ret*a%mod;
a = a*a%mod;
b >>= 1;
}
return ret;
}
LL calc(LL n, LL m)
{
n = n+m;
LL ret = (fac[n]*inv[m]%mod)*inv[n-m]%mod;
return ret;
}
bool cmp(const Point& a, const Point& b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
int main()
{
scan_d(h);
scan_d(w);
scan_d(n);
for(int i = 0; i < n; ++i)
{
scan_d(p[i].x);
scan_d(p[i].y);
}
fac[0] = inv[0] = 1;
for(int i = 1; i <= h+w; ++i)
{
fac[i] = fac[i-1]*(LL)i%mod;
inv[i] = mpow(fac[i],mod-2);
}
p[n].x = h,p[n].y = w;
sort(p,p+n,cmp);
for(int i = 0; i <= n; ++i)
{
res[i] = calc(p[i].x-1,p[i].y-1);
for(int j = 0; j < i; ++j)
{
if(p[i].x >= p[j].x && p[i].y >= p[j].y)
{
res[i] = ((res[i]-res[j]*calc(p[i].x-p[j].x,p[i].y-p[j].y)%mod)%mod+mod)%mod;
}
}
}
println(res[n]);
return 0;
}