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;
}
/**/

 

全部评论

相关推荐

小红书 后端开发 总包n+8w+期权
点赞 评论 收藏
分享
有工作后先养猫:太好了,是超时空战警,我们有救了😋
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务