|
<p>Consider two horizontal lines and a set of <b>N</b> trapezoids. A trapezoid <b>T[i]</b> between these lines has two vertices situated on the upper line and the other two vertices on the lower line. We will denote by <b>a[i], b[i], c[i]</b> and <b>d[i]</b> the upper left, upper right, lower left and respectively lower right vertices of the trapezoid <b>T[i]</b>. Assume that no two trapezoids share a common vertex (meaning that all <b>a [i]</b> and <b>b [i]</b> coordinates on the upper line are different, and the same holds for the bottom line and coordinates <b>c [i]</b> and <b>d [i]</b>). A trapezoid set is called <b>disconnected</b> if one can separate the trapezoids in two or more groups such that no two trapezoids from different groups intersect. Determine the smallest number of trapezoids to remove, such that the remaining trapezoids form a disconnected set. If the solution does not exist, output <b>-1</b>.</p> |
|
|
|
<h3>Input</h3> |
|
<p>The first line of the input file contains an integer <b>T</b>, and this is followed by <b>T</b> test cases. Each test case is given in the compressed format. |
|
|
|
<p>The first line of each test case contains the number of trapezoids <b>N</b>, an integer <b>K</b>, and integer parameters <b>X, A, B, M, p, q</b>. In the next <b>K</b> lines are given integer numbers <b>aa[i], bb[i], cc[i], dd[i]</b>. The following code is used for generating the next random number using linear congruential generator with the starting value <b>X</b> and parameters <b>A, B</b> and modulo <b>M</b>:</br> |
|
|
|
<pre> |
|
long long prior = X; |
|
long long next() { |
|
prior = (A * prior + B) % M; |
|
return prior; |
|
} |
|
</pre> |
|
|
|
The following code is used for extending the auxiliary sequences <b>aa, bb, cc</b> and <b>dd</b>: |
|
<pre> |
|
for (int i = K; i < N; i++) { |
|
aa [i] = aa [i - K] + next() % (2 * p) - p; |
|
bb [i] = aa [i] + 1 + next() % (2 * (bb [i % K] - aa [i % K])); |
|
cc [i] = cc [i - K] + next() % (2 * q) - q; |
|
dd [i] = cc [i] + 1 + next() % (2 * (dd [i % K] - cc [i % K])); |
|
} |
|
</pre> |
|
|
|
The final coordinates of the trapezoids are given by:</br> |
|
<pre> |
|
for (int i = 0; i < N; i++) { |
|
a [i] = aa [i] * 1000000 + i; |
|
b [i] = bb [i] * 1000000 + i; |
|
c [i] = cc [i] * 1000000 + i; |
|
d [i] = dd [i] * 1000000 + i; |
|
} |
|
</pre> |
|
|
|
Note that above code generates trapezoids that satisfy all conditions of the problem, and '%' denotes the remainder of division. |
|
|
|
<h2>Output</h2> |
|
<p>For each of the test cases numbered in order from <b>1</b> to <b>T</b>, output "Case #i: " followed by a single integer, the minimum number of trapezoids that need to be removed such that the remaining trapezoids form a disconnected set. </p> |
|
|
|
<h2>Constraints</h2> |
|
<p> |
|
1 ≤ <b>T</b> ≤ 20<br/> |
|
1 ≤ <b>N</b> ≤ 300,000<br/> |
|
1 ≤ <b>K</b> ≤ <b>N</b><br/> |
|
0 ≤ <b>X, A, B</b> ≤ 2,000,000,000<br/> |
|
-2,000,000,000 ≤ <b>aa [i], bb [i], cc [i], dd [i]</b> ≤ 2,000,000,000<br/> |
|
1 ≤ <b>p, q, M</b> ≤ 2,000,000,000<br/> |
|
|
|
<h3>Examples</h3> |
|
|
|
<p>In the first example, one can remove trapezoids 5 and 6 leaving two disconnected sets with trapezoids (1, 2, 3, 4) and (7, 8).</p> |
|
|
|
<p>In the second example, the coordinates of ten trapezoids are |
|
<pre> |
|
1000000 4000000 3000000 5000000 |
|
7000001 8000001 2000001 6000001 |
|
2000002 7000002 2000002 5000002 |
|
5000003 7000003 2000003 6000003 |
|
4 6000004 2000004 3000004 |
|
4000005 5000005 3000005 8000005 |
|
-999994 6 6 2000006 |
|
4000007 6000007 1000007 7000007 |
|
-999992 8 -999992 2000008 |
|
5000009 6000009 9 7000009 |
|
</pre> |
|
</p> |
|
|
|
|