Question B: Counting ----------------------------- Thinking (Combinatorial Mathematics)

Title Description
Alice and Bob play chess in a rectangular coordinate system. Alice's pawn is initially at (0,0) position and must go to (a, b) position; Bob's pawn is initially at (c, 0) position and has to go to (a, d) position. The pieces can only move a number of unit lengths along the positive direction of the x-axis or y-axis. Ask how many movement schemes make the movement paths of the two pieces do not intersect.
Input
Enter a row of 4 positive integers, in order a, b, c, d.
Output
The result of modulo the total number of schemes against the prime number 100000007.
Sample input Copy
3 2 1 1
Sample output Copy
6
Prompt
[Sample explanation] When
A walks (0,0) → (0,2) → (3,2), B has 3 ways to go:
(1, 0) → (1,1) → (3,1)
(1,0) → (2,0) → (2,1) → (3,1)
(1,0) → (3,0) → ( 3,1) When
A walks (0,0) → (0,1) → (1,1) → (1,2) → (3,2), B has 2 ways of walking.
When A walks (0,0) → (0,1) → (2,1) → (2,2) → (3,2), B has 1 way of walking.

[Data Range]
For 50% of the data, a + b <= 20.
For 70% of the data, a + b <= 2e4.
For 100% of the data, a + b <= 2e5 and a> c, b> d.

Analysis: According to the
meaning of the question: A must be on top of B (a very important breakthrough, enumerating the number of x-axis schemes).
Therefore, the number of combinations of A to (a, b) C (a + b, a) from a + b In step, choose a to take the x-axis

Then why the combination number C (a-c + d, ac) of B to (a, d) is reduced by c, because we find that they are disjoint, and A is above B, then the lines above B are Can't go, so subtract.

Combined, C (a + b, a) * C (a-c + d, ac)
will be repeated.
Repeating the situation is that AB goes to the end point and exchanges with each other.
Then the number of end point combinations from A to B is C (a + d, a);
then the number of end point combinations from B to A is C (a-c + b, ac) ;

So the final answer is: C (a + b, a) * C (a-c + d, ac) -C (a + d, a) * C (a-c + b, ac)

Insert picture description here

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
const int MOD=100000007;
ll fact[N];
int a,b,c,d;
void init()
{
	fact[1]=1;
	for(int i=2;i<=N;i++)
	{
		fact[i]=(fact[i-1]*i)%MOD;
	}
}
ll quick(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		b>>=1; 
	 } 
	 return res;
}
int main()
{
	init();
	scanf("%d %d %d %d",&a,&b,&c,&d);
	ll res1=(fact[a+b]%MOD*quick(fact[a],MOD-2)%MOD*quick(fact[a+b-a],MOD-2)%MOD*fact[a-c+d]%MOD*quick(fact[a-c],MOD-2)%MOD*quick(fact[a-c+d-a+c],MOD-2)%MOD);
	ll res2=(fact[a+d]%MOD*quick(fact[a],MOD-2)%MOD*quick(fact[a+d-a],MOD-2)%MOD*fact[a-c+b]%MOD*quick(fact[a-c],MOD-2)%MOD*quick(fact[a-c+b-a+c],MOD-2)%MOD);
	ll sum=(res1-res2+MOD)%MOD;
	cout<<sum<<endl;
}
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105211842