【Codeforces Round #313 Div. 1】 559C Gerald and Giant Chess

Description
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 × w field, 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.


【题目大意】
给出一个棋盘为h*w,现在要从(1,1)到(h,w),其中有n个黑点不能走,问有多少种可能从左上到右下(1,1和h,w永远是可以走的)


【题目分析】
有一个很显然的结论,从一个a*b的矩阵的左上角走到右下角,总共的方案数是C(a,a+b)的。

证明如下:
无论如何从左上角走到右下角,总是要经过a条横边,b条竖边的。显然,总共要走a+b步。这样就把问题转化成了有a+b个步骤,选取其中的a个横这走,其余的b个显然竖着走就可以了。

然后我们很容易想到比较有趣的做法,统计经过每一个黑点的路径数然后再用总的方案数减去。显然会有许许多多的重复的情况,这样子有可能会用到容斥定理这种丧心病狂的东西。
稍作思考,如果我们加大限制的力度,比如说对于一个黑点,我们规定他是从左上角出来之后第一个经过(不允许经过其他的黑点)的黑点,这样子,就没有重复的路径,也就不需要容斥定理了。
   
    之后就是一些细节的问题了,比如说计算组合数如何进行取模意义下的除法,就需要用到乘法逆元这一类的东西,这里就不再论述了。


【代码】(借鉴了网上的代码)

#include <cstdio>
#include <algorithm>
#include <pair>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define LL long long
const int maxn=400001;
const int maxm=4001;
const int mod=1e9+7;
LL dp[maxm],f[maxn];
int h,w,n;
pair <int,int> p[maxm];
LL powm(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 C(LL a,LL b)
{
    if (b>a) return 0;
    return f[a]*powm(f[b]*f[a-b]%mod,mod-2)%mod;
}
LL Lu(LL n,LL m)
{
    if (m==0) return 1;
    return (C(n%mod,m%mod)*Lu(n/mod,m/mod))%mod;
}
int main()
{
    f[0]=1;
    for (int i=1;i<=200010;++i) f[i]=(f[i-1]*i)%mod;
    scanf("%d%d%d",&h,&w,&n);
    for (int i=1;i<=n;++i)
        scanf("%d%d",&p[i].first,&p[i].second);
    p[n+1].first=h,p[n+1].second=w;
    n++;
    sort(p+1,p+n+1);
    for (int i=1;i<=n;++i)
    {
        dp[i]=Lu(p[i].first+p[i].second-2,p[i].first-1);
        for (int j=1;j<i;++j)
        {
            if (p[j].second<=p[i].second)
            {
                int a=p[i].first-p[j].first+p[i].second-p[j].second;
                int b=p[i].first-p[j].first;
                dp[i]-=dp[j]*Lu(a,b)%mod;
                (dp[i]+=mod)%=mod;
            }
        }
    }
    printf("%I64d\n",dp[n]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值