Link
https://atcoder.jp/contests/arc058/tasks/arc058_b
Description
一个H×W的矩形区域,它的左下角有一块A×B的矩形区域,要求中途不经过左下角区域,从大矩形的左上角走到右下角,求方案数;
Solution
如下图
该图是10×5的矩阵,如果不考虑左下角直接从s到t方案数为 C 10 − 1 + 5 − 1 5 − 1 C_{10-1+5-1}^{5-1} C10−1+5−15−1,而如果考虑左下角区域,则不妨将路径分成两部分,即s到t,可先从s到a,然后a到b,b在到t,那么这样的情况下的方案数,就是s到a的方案数乘以b到t的方案数,即 C 3 − 1 + 2 − 1 2 − 1 ∗ C 10 − 2 + 5 − 3 − 1 10 − 2 C_{3-1+2-1}^{2-1}*C_{10-2+5-3-1}^{10-2} C3−1+2−12−1∗C10−2+5−3−110−2种方案,然后把所有的情况以该种方式计算出来,即将所有的a和b的结果求和,剩下的就是求逆元和阶乘了。
Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;
#define inf 0x7f7f7f7f
#define maxn 100006
#define mod 1000000007
#define N 1
#define P 2
typedef long long ll;
typedef struct {
int u, v, next, w;
} Edge;
Edge e[1];
int cnt, head[1];
inline void add(int u, int v,int w) {
e[cnt].u = u;
e[cnt].v = v;
e[cnt].w = w;
// e[cnt].f=f;
e[cnt].next = head[u];
head[u] = cnt++;
e[cnt].u = v;
e[cnt].v = u;
e[cnt].w = w;
// e[cnt].f=-f;
e[cnt].next = head[v];
head[v] = cnt++;
}
inline void write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
inline ll read() {
ll x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
ll h,w,a,b,f[2*100005],inv[2*100005];
ll qpow(ll a,ll b){
ll res=1;
while(b){
if(b&1)res=res*a%mod;
b>>=1;
a=a*a%mod;
}
return res;
}
void solve(){
f[0]=1;
for(ll i=1;i<=h+w-2;i++){
f[i]=(f[i-1]*i)%mod;
}
inv[0]=1;
inv[h+w-2]=qpow(f[h+w-2],mod-2);
for(int i=h+w-3;i>0;i--)
inv[i]=inv[i+1]*(i+1)%mod;
}
ll C(ll a,ll b){
return f[a]*inv[b]%mod*inv[a-b]%mod;
}
int main(){
h=read(),w=read(),a=read(),b=read();
solve();
ll res=0;
for(int i=1;i<=h-a;i++){
ll t=C(i-1+b-1,i-1)*C(h-i+w-b-1,h-i)%mod;
res=(res+t)%mod;
}
cout<<res<<endl;
return 0;
}