1. 程式人生 > >A Path Plan(求兩個人從y軸不想交走到x軸有多少方案數)

A Path Plan(求兩個人從y軸不想交走到x軸有多少方案數)

A Path Plan

時間限制: 1 Sec  記憶體限制: 128 MB

題目描述

WNJXYK hates Destinys so that he does not want to meet him at any time. Luckily, their classrooms and dormitories are at different places. The only chance for them to meet each other is on their way to classrooms from dormitories. 
To simple this question, we can assume that the map of school is a normal rectangular 2D net. WNJXYK’s dormitory located at (0,y_1) and his classroom located at (x_1,0). Destinys’s dormitory located at (0,y_2) and his classroom is located at (x_2,0). On their way to classrooms, they can do two kinds of movement : (x,y)→(x,y-1) and (x,y)→(x+1,y). 
WNJXYK does not want to meet Destinys so that he thinks that it is not safe to let his path to classroom from dormitory has any intersect point with Destinys ‘s. And then he wonders how many different paths for WNJXYK and Destinys arriving their classrooms from dormitories safely.

 

輸入

The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains one line with four positive integers x1,x2,y1,y2 which has been explained above.

 

輸出

For each test case, output one line containing “y” where y is the number of different paths modulo 10^9+7.

 

樣例輸入

複製樣例資料

3
1 2 1 2
2 3 2 4
4 9 3 13

樣例輸出

3
60
16886100

 

提示


T≤1000
x1<x2,y1<y2
0 < x1,x2,y1,y2≤100000
For Test Case 1, there are following three different ways.

從(0,y1)到(x1,0)有C(x1 + y1, x1)條路,因為有兩個點,所以方案數為C( y1+x1,x1 ) * C( y2+x2,x2 ),

相交的部分我是真的沒理解,方案數為C( x1+y2,x1 ) * C( x2+y1,x2 ),然後減一下,就過了??。。。

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n;
int x1, x2, Y1, y2;
LL c[200005];
LL mod = 1e9 + 7;

LL inv(LL x, LL num){
	LL res = 1 % mod;
	x %= mod;
	while(num){
		if(num & 1) res = (res * x) % mod;
		x = x * x % mod;
		num >>= 1;
	}
	return res;
}

LL C(int x, int y){
	LL dx = c[x], dy = c[x - y] * c[y] % mod;
	return dx * inv(dy, mod - 2) % mod;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	c[0] = 1;
	for (int i = 1; i <= 200000; i++) c[i] = c[i - 1] * i % mod;
	scanf("%d", &n);
	while(n--){
		scanf("%d %d %d %d", &x1, &x2, &Y1, &y2);
		LL ans = C(x1 + Y1, x1) * C(x2 + y2, x2) % mod - C(x1 + y2, x1) * C(x2 + Y1, Y1) % mod;
		printf("%lld\n", (ans % mod + mod) % mod);
	}

	return 0;
}
/**/