51nod 1486 大大走格子(dp,容斥)

参考题解: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值