#P7499. 「HMOI R1」概率

「HMOI R1」概率

Description

After fz retired, he started studying regular school courses.

fz encountered a problem like this:

Given intervals [a,b][a, b] and [c,d][c, d], pick one integer from each interval uniformly at random, and find the probability that their sum equals ee.

fz originally wanted to change "equals ee" into "[e,f]\in [e,f]", but then he would become one of those annoying problem setters who rely on messy case discussions, so he decided not to change it.

For convenience, you only need to output the value of the answer multiplied by (ba+1)(dc+1)(b-a+1)(d-c+1). It can be proven that this number is an integer.

Input Format

The first line contains an integer TT, meaning there are TT test cases.

In the next TT lines, each line contains five integers a,b,c,d,ea, b, c, d, e.

Output Format

Output TT lines. For each test case, output one non-negative integer, representing the corresponding answer.

1
1 2 3 4 5

2

Hint

Sample explanation:

For the first test case in the sample, there are 44 ways to randomly choose one integer from [1,2][1,2] and one integer from [3,4][3,4]. Only {1,4}\{1,4\} and {2,3}\{2,3\} have sum 55, so the probability is 12\dfrac12.


Let N=max{a,b,c,d,e}N=\max\{|a|,|b|,|c|,|d|,|e|\}.

Constraints for all testdata:

  • 1T10001 \le T \le 1000.
  • 0N10180 \le N \le 10^{18}.

This problem does not use bundled tests.

No. Constraints Score
11 Same as the sample 1010
22 N300N \le 300
33 N5000N \le 5000 3030
44 N109N \le 10^9 2020
55 No further constraints 3030

  • Idea: FZzzz
  • Solution: FZzzz
  • Code: FZzzz
  • Data: FZzzz

Translated by ChatGPT 5