Watson and Intervals (Large)

Time limit5sMemory limit512 MB

Summary
Generate N intervals from a recurrence, then find the minimum covered integer count after removing exactly one interval.
Level

Medium7 of 10

Topics
Intervals, Sorting, Simulation, Implementation
Solved
No attempts yet

Problem

Sherlock and Watson have mastered the C++ language in their programming course, so they have moved on to algorithmic problems. In today's class, the tutor introduced the problem of merging one-dimensional intervals. NN intervals are given, and the ii-th 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 such that Lj≤p≤RjL_j \le p \le R_j.

Watson always 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. Help Sherlock find this minimum possible covered area after removing exactly one of the NN intervals.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains one test case.

Each test case is one line with eight integers NN, L1L_1, R1R_1, AA, BB, C1C_1, C2C_2, and MM. NN is the number of intervals, and the other seven values are parameters that you use to generate the remaining intervals, as follows.

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

For every ii from 2 to NN, define Li=min⁡(xi,yi)L_i = \min(x_i, y_i) and Ri=max⁡(xi,yi)R_i = \max(x_i, y_i).

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum possible covered area of all the intervals that remain after removing exactly one interval.

Constraints

  • 1≤T≤501 \le T \le 50
  • 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
  • 1≤N≤5×1051 \le N \le 5 \times 10^5 (500000)

Hint

In case 1, the generation method produces the single interval [1,1][1, 1]. Removing the only interval leaves a covered area of 0.

In case 2, the generated intervals are [2,5][2, 5], [3,5][3, 5], and [4,7][4, 7]. Removing the first, second, or third interval makes the covered area of the remaining intervals 5, 6, and 4, respectively.

In case 3, the generated intervals are [3,4][3, 4], [1,9][1, 9], [0,8][0, 8], and [2,4][2, 4]. Removing the first, second, third, or fourth interval makes the covered area of the remaining intervals 10, 9, 9, and 10, 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