AtCoder Regular Contest 058--D - いろはちゃんとマス目 / Iroha and a Grid(数学+组合数逆元)

D - いろはちゃんとマス目 / Iroha and a Grid


Time Limit: 2 sec / Memory Limit: 256 MB

Score : 400400 points

Problem Statement

We have a large square grid with HH rows and WW columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.

However, she cannot enter the cells in the intersection of the bottom AA rows and the leftmost BB columns. (That is, there are A×BA×B forbidden cells.) There is no restriction on entering the other cells.

Find the number of ways she can travel to the bottom-right cell.

Since this number can be extremely large, print the number modulo 109+7109+7.

Constraints

  • 1≦H,W≦100,0001≦H,W≦100,000
  • 1≦A<H1≦A<H
  • 1≦B<W1≦B<W

Input

The input is given from Standard Input in the following format:

HH WW AA BB

Output

Print the number of ways she can travel to the bottom-right cell, modulo 109+7109+7.


Sample Input 1 Copy

Copy

2 3 1 1

Sample Output 1 Copy

Copy

2

We have a 2×32×3 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".


Sample Input 2 Copy

Copy

10 7 3 4

Sample Output 2 Copy

Copy

3570

There are 1212 forbidden cells.


Sample Input 3 Copy

Copy

100000 100000 99999 99999

Sample Output 3 Copy

Copy

1

Sample Input 4 Copy

Copy

100000 100000 44444 55555

Sample Output 4 Copy

Copy

738162020

H*W格子,左下角的A*B区域不能越过,求左上到右下一共多少种走法。

分两步走,最后乘起来,上面的步数一共是:纵向H-A-1 横向I-1 一共S=H-A-2+I步,所以一共C(S)(H-A-1)中走法

下面就是纵向A-1(不是A,如果是A的话就和上面重复了),横向W-I所以一共是C(S)(A-1),乘起来就是答案。

组合数逆元模板

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define G  1100000
#define mod 1000000007
LL pri[G];
LL ni[G],ans;
LL pow(LL a,int b)
{
    LL ans=1,base=a;
    while (b>0)
    {
        if (b%2==1)
            ans=(base*ans)%mod;
        base=(base*base)%mod;
        b/=2;
    }
    return ans;
}
void s()   //打表
{
    pri[0]=1;
    ni[0]=1;
    for (int i=1;i<G ;i++)
    {
        pri[i]=pri[i-1]*i%mod;  //N!
        ni[i]=pow(pri[i],mod-2);
    }
}
LL ANS(LL n,LL m)
{
    LL ans;
    ans=((pri[n]*ni[m]%mod)*ni[n-m])%mod;
    return ans%mod;
}

题解

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define G  1100000
#define mod 1000000007
LL pri[G];
LL ni[G],ans;
LL pow(LL a,int b)
{
    LL ans=1,base=a;
    while (b>0)
    {
        if (b%2==1)
            ans=(base*ans)%mod;
        base=(base*base)%mod;
        b/=2;
    }
    return ans;
}
void s()   //打表
{
    pri[0]=1;
    ni[0]=1;
    for (int i=1;i<G ;i++)
    {
        pri[i]=pri[i-1]*i%mod;  //N!
        ni[i]=pow(pri[i],mod-2);
    }
}
LL ANS(LL n,LL m)
{
    LL ans;
    ans=((pri[n]*ni[m]%mod)*ni[n-m])%mod;
    return ans%mod;
}
int main()
{
    //cout<<(22183738317301)%mod<<endl;
    s();
    LL H,W,A,B;
    scanf("%lld%lld%lld%lld",&H,&W,&A,&B);
    LL ans=0;
    for(LL i=B+1;i<=W;i++)
    {
        ans+=(LL)(ANS(H-A+i-2,H-A-1)*ANS(W+A-i-1,A-1)%mod)%mod;
    }
    printf("%lld\n",ans%mod);
    /*
    int t,n,b,k=1;
	//scanf("%d",&t);
    while (t--)
    {
        //scanf("%d%d",&n,&b);
        //ans=((pri[n]*ni[b]%mod)*ni[n-b])%mod; // C(n,m)= n!/(m!*(n-m)!)
        printf("Case %d: %lld\n",k++,ans);
    }
    return 0;  */

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值