2012 Multi-School Training Competition 8 A Path Plan

Link: HDU-6482

Title

Given x1, x2, y1, y2, find the number of disjoint solutions from (0, y1) to (x1,0) and from (0, y2) to (x2, 0) (only to the right Go down)

answer

Suppose the number of paths from (0,y) to (x,0) is f(y,x), and the number of steps is x+y, where x times to the right are easy to get f(y,x)=C_{x+y}^{x},

It is easy to think of tolerance and exclusion, the number of schemes is f(y1,x1)*f(y2,x2)-B, B is the number of schemes where two paths intersect,

The intersection of two paths means: passing the point of intersection, you must be able to walk from (0,y1) to (x2,0), and from (0,y2) to (x1,0),

Conversely, any two paths from (0,y1) to (x2,0), from (0,y2) to (x1,0) must have intersections (because x1<x2, y1<y2)

So we prove:B=f(y1,x2)*f(y2,x1)

answer=f(y1,x1)*f(y2,x2)-f(y1,x2)*f(y2,x1)

Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define MAXN 200005
#define mod 1000000007ll
using namespace std;
inline ll read(){
	ll x=0,f=1;char s=getchar();
	while((s<'0'||s>'9')&&s>0){if(s=='-')f*=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=(x<<1)+(x<<3)+s-'0';s=getchar();}
	return x*f;
}
ll x1,y_,x2,y2,jc[MAXN];
inline ll ksm(ll a,ll b){
	ll res=1;
	for(;b;b>>=1){
		if(b&1)res=res*a%mod;
		a=a*a%mod;
	}
	return res;
}
inline ll C(ll n,ll m){
	if(m>n)return 0;
	return jc[n]*ksm(jc[m],mod-2)%mod*ksm(jc[n-m],mod-2)%mod;
}
int main()
{
	jc[0]=1;
	for(ll i=1;i<MAXN-4;i++)jc[(int)i]=jc[int(i-1)]*i%mod;
	for(int T=read();T>0;T--){
		x1=read(),x2=read(),y_=read(),y2=read();
		printf("%lld\n",(C(x1+y_,x1)*C(x2+y2,x2)%mod-C(x1+y2,x1)*C(x2+y_,x2)%mod+mod)%mod);
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43960287/article/details/108465072