AtCoder Grand Contest 018 E - Sightseeing Plan

Problem Statement

Joisino is planning on touring Takahashi Town. The town is divided into square sections by north-south and east-west lines. We will refer to the section that is the x-th from the west and the y-th from the north as (x,y).

Joisino thinks that a touring plan is good if it satisfies the following conditions:

  • Let (p,q) be the section where she starts the tour. Then, X1pX2 and Y1qY2 hold.

  • Let (s,t) be the section where she has lunch. Then, X3sX4 and Y3tY4 hold.

  • Let (u,v) be the section where she ends the tour. Then, X5uX6 and Y5vY6 hold.

  • By repeatedly moving to the adjacent section (sharing a side), she travels from the starting section to the ending section in the shortest distance, passing the lunch section on the way.

Two touring plans are considered different if at least one of the following is different: the starting section, the lunch section, the ending section, and the sections that are visited on the way. Joisino would like to know how many different good touring plans there are. Find the number of the different good touring plans. Since it may be extremely large, find the count modulo 109+7.

Constraints

  • 1X1X2<X3X4<X5X6106
  • 1Y1Y2<Y3Y4<Y5Y6106

Input

Input is given from Standard Input in the following format:

X1 X2 X3 X4 X5 X6
Y1 Y2 Y3 Y4 Y5 Y6

Output

Print the number of the different good touring plans, modulo 109+7.


Sample Input 1

Copy
1 1 2 2 3 4
1 1 2 2 3 3

Sample Output 1

Copy
10

The starting section will always be (1,1), and the lunch section will always be (2,2). There are four good touring plans where (3,3) is the ending section, and six good touring plans where (4,3) is the ending section. Therefore, the answer is 6+4=10.


Sample Input 2

Copy
1 2 3 4 5 6
1 2 3 4 5 6

Sample Output 2

Copy
2346

Sample Input 3

Copy
77523 89555 420588 604360 845669 973451
2743 188053 544330 647651 709337 988194

Sample Output 3

Copy
137477680

题意:给三个矩形,从三个里面任取一个点,记为A,B,C,求从A到C经过B的方案数

题解:

记F(x, y)表示从(0, 0)走到(x, y)的方案数

那么有∑[x = 0 to X]F(x, Y) = F(X, Y + 1)

这个证明直接考虑从(0, 0)到(X, Y + 1)一定要从某纵坐标为Y的位置走到纵坐标Y+1的位置

然后就有∑[x = 0 to X][y = 0 to Y]F(x, y) = F(X + 1, Y + 1) - 1

故∑[x = x1 to x2][y = y1 to y2]F(x, y) = F(x2 + 1, y2 + 1) + F(x1, y1) - F(x1, y2 + 1) - F(x2 + 1, y1)

所以问题转为给一个点start,一个点end,中间一个给定矩形,枚举中间矩形内的每个点,求start到end经过这个点的方案数之和

我们可以计算一条start到end的路径跟矩形相交的部分的点数,那么这条路径的权值就是这个点数,我们就可以转为求路径权值和

再转化一下,一条路径跟矩形相交一定会从某个地方进去,某个地方出来,问题可以转为到4条直线的方案数*某个权值,这个直接统计就好了

权值很简单了,怎么构造见代码

#include 
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair  pii;

inline int Read()
{
	int x = 0, f = 1, c = getchar();
	for (; !isdigit(c); c = getchar())
		if (c == '-')
			f = -1;
	for (;  isdigit(c); c = getchar())
		x = x * 10 + c - '0';
	return x * f;
}

const int MAXN = 2000005;
const int mod = 1e9 + 7;

int x[7], y[7], fac[MAXN], inv[MAXN], ans;

inline int C(int x, int y) { return 1LL * fac[x + y] * inv[x] % mod * inv[y] % mod; }
inline int F(int x1, int y1, int x2, int y2) { return (1LL * C(x2 + 1, y2 + 1) + C(x1, y1) - C(x1, y2 + 1) - C(x2 + 1, y1) + mod + mod) % mod; }

int main()
{
#ifdef wxh010910
	freopen("data.in", "r", stdin);
#endif
	for (int i = 1; i <= 6; i ++)
		x[i] = Read();
	for (int i = 1; i <= 6; i ++)
		y[i] = Read();
	fac[0] = inv[0] = fac[1] = inv[1] = 1;
	for (int i = 2; i <= 2000000; i ++)
		fac[i] = 1LL * fac[i - 1] * i % mod, inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
	for (int i = 2; i <= 2000000; i ++)
		inv[i] = 1LL * inv[i - 1] * inv[i] % mod;
	for (int i = x[3]; i <= x[4]; i ++)
		ans = (1LL * (mod - y[3] - i) * F(i - x[2], y[3] - 1 - y[2], i - x[1], y[3] - 1 - y[1]) % mod * F(x[5] - i, y[5] - y[3], x[6] - i, y[6] - y[3]) + ans) % mod;
	for (int i = x[3]; i <= x[4]; i ++)
		ans = (1LL * (i + y[4] + 1) * F(i - x[2], y[4] - y[2], i - x[1], y[4] - y[1]) % mod * F(x[5] - i, y[5] - y[4] - 1, x[6] - i, y[6] - y[4] - 1) + ans) % mod;
	for (int i = y[3]; i <= y[4]; i ++)
		ans = (1LL * (mod - x[3] - i) * F(i - y[2], x[3] - 1 - x[2], i - y[1], x[3] - 1 - x[1]) % mod * F(y[5] - i, x[5] - x[3], y[6] - i, x[6] - x[3]) + ans) % mod;
	for (int i = y[3]; i <= y[4]; i ++)
		ans = (1LL * (i + x[4] + 1) * F(i - y[2], x[4] - x[2], i - y[1], x[4] - x[1]) % mod * F(y[5] - i, x[5] - x[4] - 1, y[6] - i, x[6] - x[4] - 1) + ans) % mod;
	return printf("%d\n", ans), 0;
}


你可能感兴趣的:(AtCoder Grand Contest 018 E - Sightseeing Plan)