Codeforces Round #313 (Div. 2)E. Gerald and Giant Chess(Lucas定理+dp)

E. Gerald and Giant Chess

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.
Input

The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells ( 1h,w105,1n2000 ).

Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci ( 1rih,1ciw ) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.
Output

Print a single line — the remainder of the number of ways to move Gerald’s pawn from the upper left to the lower right corner modulo 109+7 .
Sample test(s)
Input

3 4 2
2 2
2 3

Output

2

Input

100 100 3
15 16
16 15
99 88

Output

545732279

题意:n*m的格子,有些是坏的不能走,从左上角走到右下角有多少种方法。
思路:如果格子比较小的话,那么可以dp递推出来,但是这里nm都很大,所以哪种方法不可取。dp[i]表示到第i个坏的格子,并且不经过其他格子有多少种方案数,在最后把(H,W)加上。
可得转移方程:
dp[i]=C(x[i]+y[i],x[i])(dp[j]C(x[i]x[j]+y[i]y[j],x[i]x[j]))(1j<i,y[j]y[i])

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int maxm=2010;
const int MOD=1e9+7;
LL dp[maxm],f[2*maxn];
int H,W,N;
pair<int,int> p[maxm];
LL pow_mul(LL a,LL b)
{
    LL res=1;
    while(b)
    {
        if(b&1)res=res*a%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
LL C(LL n,LL m)
{
    if(m>n)return 0;
    return f[n]*pow_mul(f[m]*f[n-m]%MOD,MOD-2)%MOD;
}
LL Lucas(LL n,LL m)
{
    if(m==0)return 1;
    return (C(n%MOD,m%MOD)*Lucas(n/MOD,m/MOD))%MOD;
}
int main()
{
    f[0]=1;
    for(int i=1;i<2*maxn;i++)
        f[i]=(f[i-1]*i)%MOD;
    while(scanf("%d%d%d",&H,&W,&N)!=EOF)
    {
        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+1+N);

        for(int i=1;i<=N;i++)
        {
            dp[i]=Lucas(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 n=p[i].first-p[j].first+p[i].second-p[j].second;
                    int m=p[i].first-p[j].first;
                    dp[i]-=dp[j]*Lucas(n,m)%MOD;
                    (dp[i]+=MOD)%=MOD;
                }
            }
        }
        cout<<dp[N]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值