|
<p> |
|
Percy is a harlequin tuskfish who lives on a stretch of the sea floor, which may be represented as a number line. |
|
There are <strong>N</strong> objects resting on the sand beneath the waves, the <em>i</em>th of which is at a positive integral position <strong>P<sub>i</sub></strong>, |
|
is either a clam (if <strong>O<sub>i</sub></strong> = "C") or otherwise a rock (if <strong>O<sub>i</sub></strong> = "R"), |
|
and in either case has a hardness of <strong>H<sub>i</sub></strong>. No two objects are at the same position, and at least one of the objects is a clam. |
|
</p> |
|
|
|
<p> |
|
Percy initially finds himself at position 0, and can then swim in either direction along the number line at a rate of 1 unit per second. |
|
Whenever he occupies the same position as a clam, he may pick it up and begin carrying it around in his mouth. |
|
Picking up a clam requires no additional time, and Percy is talented enough to carry any number of clams at once. |
|
</p> |
|
|
|
<p> |
|
Percy would like to devour the delicious interior of each clam, but can't get to it without first somehow breaking open its hard shell. |
|
Fortunately, Percy is clever and persistent enough to have a <a href="https://www.radiotimes.com/news/2018-01-26/blue-planet-2-fish-clams-tools/" target="_blank">solution</a> to this problem. |
|
Whenever he occupies the same position as a rock, he may take each clam that he's currently carrying that has a strictly smaller hardness than that of the rock, |
|
knock the clam against the rock to break open its shell, and eat the meal packaged within! This process requires no additional time per clam. |
|
</p> |
|
|
|
<p> |
|
What's the minimum amount of time required for Percy to swim around and eat all of the clams (by picking each one up and then knocking it against a harder rock than itself), if at all possible? |
|
</p> |
|
|
|
<p> |
|
In order to reduce the size of the input, the object's positions and hardnesses will not all be provided explicitly. |
|
Instead, you'll be given <strong>P<sub>1</sub></strong>, <strong>P<sub>2</sub></strong>, <strong>H<sub>1</sub></strong>, <strong>H<sub>2</sub></strong>, |
|
as well as the 8 constants <strong>A<sub>p</sub></strong>, <strong>B<sub>p</sub></strong>, <strong>C<sub>p</sub></strong>, <strong>D<sub>p</sub></strong>, |
|
<strong>A<sub>h</sub></strong>, <strong>B<sub>h</sub></strong>, <strong>C<sub>h</sub></strong>, and <strong>D<sub>h</sub></strong>, |
|
and must then compute <strong>P<sub>3..N</sub></strong> and <strong>H<sub>3..N</sub></strong> as follows (bearing in mind that intermediate values may not fit within 32-bit integers): |
|
</p> |
|
|
|
<p> |
|
<strong>P<sub>i</sub></strong> = ((<strong>A<sub>p</sub></strong> * <strong>P<sub>i-2</sub></strong> |
|
+ <strong>B<sub>p</sub></strong> * <strong>P<sub>i-1</sub></strong> + <strong>C<sub>p</sub></strong>) modulo <strong>D<sub>p</sub></strong>) + 1, |
|
for <em>i</em> = 3 to <strong>N</strong>. |
|
</p> |
|
|
|
<p> |
|
<strong>H<sub>i</sub></strong> = ((<strong>A<sub>h</sub></strong> * <strong>H<sub>i-2</sub></strong> |
|
+ <strong>B<sub>h</sub></strong> * <strong>H<sub>i-1</sub></strong> + <strong>C<sub>h</sub></strong>) modulo <strong>D<sub>h</sub></strong>) + 1, |
|
for <em>i</em> = 3 to <strong>N</strong>. |
|
</p> |
|
|
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of days Percy goes hunting for clams. |
|
For each day, there are four lines. |
|
The first line contains the integer <strong>N</strong>. |
|
The second line contains the space-separated integers <strong>P<sub>1</sub></strong>, <strong>P<sub>2</sub></strong>, |
|
<strong>A<sub>p</sub></strong>, <strong>B<sub>p</sub></strong>, <strong>C<sub>p</sub></strong>, and <strong>D<sub>p</sub></strong>. |
|
The third line contains the space-separated integers <strong>H<sub>1</sub></strong>, <strong>H<sub>2</sub></strong>, |
|
<strong>A<sub>h</sub></strong>, <strong>B<sub>h</sub></strong>, <strong>C<sub>h</sub></strong>, and <strong>D<sub>h</sub></strong>. |
|
The fourth line contains the length-<strong>N</strong> string <strong>O<sub>1..N</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th day, print a line containing "Case #<em>i</em>: " followed by one integer, |
|
either the minimum number of seconds required for Percy to break open and eat all of the clams, or -1 if he cannot do so. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 250 <br /> |
|
2 ≤ <strong>N</strong> ≤ 800,000 <br /> |
|
0 ≤ <strong>A<sub>p</sub></strong>, <strong>B<sub>p</sub></strong>, <strong>C<sub>p</sub></strong>, |
|
<strong>A<sub>h</sub></strong>, <strong>B<sub>h</sub></strong>, <strong>C<sub>h</sub></strong> ≤ 1,000,000,000 <br /> |
|
1 ≤ <strong>D<sub>p</sub></strong>, <strong>D<sub>h</sub></strong> ≤ 1,000,000,000 <br /> |
|
1 ≤ <strong>P<sub>i</sub></strong> ≤ <strong>D<sub>p</sub></strong> <br /> |
|
1 ≤ <strong>H<sub>i</sub></strong> ≤ <strong>D<sub>h</sub></strong> <br /> |
|
</p> |
|
|
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, P = [5, 10] and H = [30, 31]. Percy should swim to position 5, pick up the clam with hardness 30, swim onwards to position 10, |
|
and break open the clam on the rock with hardness 31. |
|
</p> |
|
|
|
<p> |
|
In the second case, P = [5, 10] and H = [31, 30]. Percy should now swim to position 10 to pick up the clam, and then back to position 5 to break it open. |
|
</p> |
|
|
|
<p> |
|
In the third case, P = [5, 10] and H = [30, 30]. Once Percy picks up the clam, no rock harder than it exists on the sea floor, meaning that he can never break it open. |
|
</p> |
|
|
|
<p> |
|
In the fourth case, P = [10, 50, 11, 52] and H = [50, 10, 49, 8]. |
|
</p> |
|
|
|
<p> |
|
In the fifth case, P = [415, 711, 225, 136, 256, 469, 714, 399, 841, 697, 480, 147, 98, 837, 745, 660, 44, 226, 73, 7] and H = [9, 2, 10, 6, 6, 2, 7, 13, 4, 5, 11, 11, 4, 3, 7, 1, 6, 10, 10, 1]. |
|
</p> |
|
|