|
<p> |
|
The Earth is gripped by an energy crisis. There's simply not enough to go around! Desperate to unlock additional sources of energy, James has decided to direct his attention downwards. |
|
Perhaps enough fossils can be found underground and harvested for just a bit more fuel? |
|
</p> |
|
|
|
<p> |
|
James has surveyed a linear stretch of ground, and has fortunately discovered <strong>N</strong> fossils beneath the surface! |
|
From a cross-sectional view, the <em>i</em>th fossil is at position <strong>P<sub>i</sub></strong> metres along the stretch of ground, |
|
at a depth of <strong>D<sub>i</sub></strong> metres below the surface. No two fossils are in exactly the same spot (at both the same position and depth). |
|
</p> |
|
|
|
<p> |
|
The next question is, of course, how the fossils might be unearthed most efficiently. James plans to dig one or more vertical mine shafts in order to access all <strong>N</strong> fossils. |
|
A mine shaft is defined by a position <em>p</em> and a depth <em>d</em>. |
|
Such a mine shaft can be represented as a vertical line segment at position <em>p</em> metres along the stretch of ground, |
|
running from the surface to a point <em>d</em> metres below the surface. |
|
James can dig such a shaft at a cost of <strong>S</strong> + <em>d</em> dollars, where <strong>S</strong> is a constant. |
|
Once it has been dug, it's possible to reach fossils from the shaft by descending to the correct depth and then digging horizontally through the earth at no additional cost, |
|
up to a distance of at most <strong>M</strong> metres away from the shaft, where <strong>M</strong> is another constant. |
|
In other words, each fossil <em>i</em> is accessible from the shaft if <strong>D<sub>i</sub></strong> ≤ <em>d</em> |
|
and |<strong>P<sub>i</sub></strong> - <em>p</em>| ≤ <strong>M</strong>. |
|
Note that a mine shaft is permitted to pass directly through a fossil (such that <em>p</em> = <strong>P<sub>i</sub></strong> for some <em>i</em>). |
|
</p> |
|
|
|
<p> |
|
Help James determine the minimum total cost of mine shafts which he must dig, such that each of the <strong>N</strong> fossils will end up being accessible from at least one of the shafts. |
|
</p> |
|
|
|
<p> |
|
In order to reduce the size of the input data, the sequences <strong>P<sub>1..N</sub></strong> and <strong>D<sub>1..N</sub></strong> will be derived from a series of temporary sequences |
|
<strong>A<sub>1..(2*K)</sub></strong>, the <em>i</em>th of of which has a length of <strong>L<sub>i</sub></strong>. |
|
<strong>P<sub>1..N</sub></strong> can be constructed by concatenating together <strong>A<sub>1..K</sub></strong>, |
|
while <strong>D<sub>1..N</sub></strong> can be constructed by concatenating together <strong>A<sub>(K+1)..(2*K)</sub></strong>. |
|
It's guaranteed that the sum of <strong>L<sub>1..K</sub></strong> is equal to <strong>N</strong>, as is the sum of <strong>L<sub>(K+1)..(2*K)</sub></strong>. |
|
For each sequence <strong>A<sub>i</sub></strong>, you're given <strong>A<sub>i,1</sub></strong>, |
|
and <strong>A<sub>i,2..L<sub>i</sub></sub></strong> may then be generated as follows using given constants |
|
<strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>, and <strong>Z<sub>i</sub></strong> (please watch out for integer overflow during this process): |
|
<p> |
|
|
|
<p> |
|
<strong>A<sub>i,j</sub></strong> = |
|
((<strong>X<sub>i</sub></strong> * <strong>A<sub>i,j-1</sub></strong> + <strong>Y<sub>i</sub></strong>) % <strong>Z<sub>i</sub></strong>) + 1 |
|
(for <em>j</em> = 2..<strong>L<sub>i</sub></strong>) |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of sets of fossils. |
|
For each set of fossils, there is first a line containing the space-separated integers <strong>N</strong>, <strong>S</strong>, <strong>M</strong>, and <strong>K</strong>. |
|
Then, 2 * <strong>K</strong> lines follow. The <em>i</em>th of these contains the space-separated integers |
|
<strong>L<sub>i</sub></strong>, <strong>A<sub>i,1</sub></strong>, <strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>, and <strong>Z<sub>i</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th set of fossils, output a line containing "Case #<em>i</em>: " followed by the minimum total cost of mine shafts which James must dig (in dollars). |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 40 <br /> |
|
1 ≤ <strong>N</strong> ≤ 1,000,000 <br /> |
|
0 ≤ <strong>S</strong>, <strong>M</strong> ≤ 1,000,000,000 <br /> |
|
1 ≤ <strong>K</strong> ≤ 10 <br /> |
|
1 ≤ <strong>P<sub>i</sub></strong>, <strong>D<sub>i</sub></strong>, <strong>A<sub>i,j</sub></strong> ≤ 1,000,000,000 <br /> |
|
1 ≤ <strong>L<sub>i</sub></strong> ≤ <strong>N</strong> <br /> |
|
0 ≤ <strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong> < <strong>Z<sub>i</sub></strong> ≤ 1,000,000,000 <br /> |
|
</p> |
|
|
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, <strong>P</strong> = [5, 25] and <strong>D</strong> = [3, 4]. |
|
James should dig a single mine shaft with <em>p</em> = 15 and <em>d</em> = 4 (at a cost of 5 + 4 = 9 dollars), from which both fossils are barely accessible. |
|
</p> |
|
|
|
<p> |
|
In the second case, <strong>P</strong> = [5, 26] and <strong>D</strong> = [3, 4]. |
|
James now requires two mine shafts (for example one with <em>p</em> = 5 and <em>d</em> = 3 and another with <em>p</em> = 26 and <em>d</em> = 4), |
|
at a total cost of (5 + 3) + (5 + 4) = 17 dollars. |
|
</p> |
|
|
|
<p> |
|
In the third case, <strong>P</strong> = [27, 11, 19, 15, 67] and <strong>D</strong> = [41, 34, 67, 40, 53]. |
|
</p> |
|
|
|
<p> |
|
In the fourth case, <strong>P</strong> = [555, 776, 673, 654, 339, 832, 887, 370, 555, 3794, 334, 180, 2491, 2018, 2805, 2 |
|
427, 1986, 3138, 1149, 495] and <strong>D</strong> = [20814, 20463, 40527, 38076, 18468, 10958, 25830, 23128, 28505, 10884, 32935, 116 |
|
66, 8264, 771, 1207, 8930, 1299, 8521, 7277, 7440]. |
|
</p> |
|
|