Watson and Intervals (Small)

Time limit5sMemory limit512 MB

Summary
Generate N intervals from a recurrence, then remove exactly one interval so the number of integers covered by the rest is minimized.
Level

Medium5 of 10

Topics
Intervals, Sorting, Brute force
Solved
No attempts yet

Problem

Sherlock and Watson have learned enough C++ in their programming course to move on to algorithmic problems. In today's class the tutor introduced the problem of merging one dimensional intervals. NN intervals are given, and the iith interval is defined by the inclusive endpoints [Li,Ri][L_i, R_i], where Li≤RiL_i \le R_i.

The tutor defined the covered area of a set of intervals as the number of integers that appear in at least one of the intervals. Formally, an integer pp contributes to the covered area if there is some jj with Lj≤p≤RjL_j \le p \le R_j.

Watson likes to challenge Sherlock. This time he asked Sherlock to remove exactly one interval so that the covered area of the remaining intervals is as small as possible. Find that minimum possible covered area after removing exactly one of the NN intervals.

Input

The first line contains the number of test cases TT.

Each test case consists of one line with eight integers NN, L1L_1, R1R_1, AA, BB, C1C_1, C2C_2, and MM, separated by spaces. NN is the number of intervals and the first interval is [L1,R1][L_1, R_1]. The other seven values are the parameters used to generate the remaining intervals.

First set x1=L1x_1 = L_1 and y1=R1y_1 = R_1. Then use the recurrences below to generate xix_i and yiy_i for i=2i = 2 to NN.

  • xi=(A×xi−1+B×yi−1+C1) mod Mx_i = (A \times x_{i-1} + B \times y_{i-1} + C_1) \bmod M
  • yi=(A×yi−1+B×xi−1+C2) mod My_i = (A \times y_{i-1} + B \times x_{i-1} + C_2) \bmod M

Define Li=min⁡(xi,yi)L_i = \min(x_i, y_i) and Ri=max⁡(xi,yi)R_i = \max(x_i, y_i) for all i=2i = 2 to NN.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number starting from 1 and yy is the minimum possible covered area of the intervals remaining after removing exactly one interval.

Constraints

  • 1≤T≤501 \le T \le 50
  • 1≤N≤10001 \le N \le 1000
  • 0≤L1≤R1≤1090 \le L_1 \le R_1 \le 10^9
  • 0≤A≤1090 \le A \le 10^9
  • 0≤B≤1090 \le B \le 10^9
  • 0≤C1≤1090 \le C_1 \le 10^9
  • 0≤C2≤1090 \le C_2 \le 10^9
  • 1≤M≤1091 \le M \le 10^9

Hint

In the first case of the example, the generation rule produces the single interval {[1,1]}\{[1, 1]\}. Removing the only interval leaves a covered area of 00.

In the second case the generated intervals are {[2,5],[3,5],[4,7]}\{[2, 5], [3, 5], [4, 7]\}. Removing the first, second or third interval leaves a covered area of 55, 66 and 44 respectively.

In the third case the generated intervals are {[3,4],[1,9],[0,8],[2,4]}\{[3, 4], [1, 9], [0, 8], [2, 4]\}. Removing the first, second, third or fourth interval leaves a covered area of 1010, 99, 99 and 1010 respectively.

Examples1

  1. Example 1

    Input
    3
    1 1 1 1 1 1 1 1
    3 2 5 1 2 3 4 10
    4 3 4 3 3 8 10 10
    
    Expected output
    Case #1: 0
    Case #2: 4
    Case #3: 9