| <p> | |
| Pierre Peintre is slaving away over a new abstract painting entitled <i>Rain Over New York</i>. | |
| This will be a simple yet powerful piece, omitting incidental details such as busy city dwellers shielding themselves with umbrellas, | |
| and instead focusing on the fundamental atmosphere of a rainy metropolitan day. | |
| It will be painted on a canvas which is subdivided into a grid of 1cm x 1cm cells, with <strong>N</strong> rows and <strong>M</strong> columns. | |
| Each cell in this grid will be filled in with a solid color, either black, white, grey, or blue. | |
| </p> | |
| <p> | |
| The lower portion of <i>Rain Over New York</i> will depict the skyline of New York City. | |
| In each column <em>i</em>, the bottom-most <strong>H<sub>i</sub></strong> cells will be painted grey to represent an austere skyscraper. | |
| </p> | |
| <p> | |
| Somewhere above the buildings, Pierre will place a single, innocent raincloud. In particular, the cloud can be any rectangle of white cells on the canvas, as long as none of them are supposed to be grey. | |
| </p> | |
| <p> | |
| Below the cloud, there must be a gentle rainfall, of course. Every cell which has a white cell somewhere directly above it and a grey cell somewhere directly below it, and which isn't supposed to be white or grey itself, should be painted blue. | |
| Note that there may be no such cells, if the cloud is immediately above the skyline. | |
| </p> | |
| <p> | |
| All of the remaining cells in the painting will be painted black, providing a serene nighttime backdrop for the scene. | |
| </p> | |
| <p> | |
| Pierre knows that every painting he can produce like this will sell for an enormous sum of money, but only if it's unique. | |
| As such, he'll paint as many different paintings as he can by varying the position and dimensions of the raincloud depicted in them. | |
| Two paintings are considered distinct if at least one cell on the canvas is a different color in one painting than it is in the other. | |
| </p> | |
| <p> | |
| As an example, below is an illustration of 1 of the 246 possible paintings for the fourth sample case: | |
| </p> | |
| <img src="{{PHOTO_ID:374610330166776}}" /> | |
| <p> | |
| Thanks to the incredible sum of money which Pierre is sure to make from these works, he'll be able to purchase all of the paint that he'll need. | |
| He always buys his paint in cans of a fixed size, each of which contains just enough to cover a surface of 1,000,000,007 cm<sup>2</sup>, | |
| and for each color, he'll buy just enough such cans in order to be able to complete all possible distinct variations of his painting, once each. | |
| Always one to plan ahead, Pierre would like to figure out exactly how much paint of each color he'll have left over when he's done. | |
| </p> | |
| <p> | |
| The sequence <strong>H<sub>1..M</sub></strong> can be constructed by concatenating <strong>K</strong> temporary | |
| sequences of values <strong>S<sub>1..K</sub></strong>, the <em>i</em>th of which has a length of <strong>L<sub>i</sub></strong>. | |
| It's guaranteed that the sum of these sequences' lengths is equal to <strong>M</strong>. | |
| For each sequence <strong>S<sub>i</sub></strong>, you're given <strong>S<sub>i,1</sub></strong>, | |
| and <strong>S<sub>i,2..L<sub>i</sub></sub></strong> may then be calculated as follows, | |
| using given constants <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>: | |
| </p> | |
| <p> | |
| <strong>S<sub>i,j</sub></strong> = ((<strong>A<sub>i</sub></strong> * <strong>S<sub>i,j-1</sub></strong> + <strong>B<sub>i</sub></strong>) % (<strong>N</strong> - 1)) + 1 | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of different base skylines Pierre wants to use. | |
| For each skyline, there is first a line containing the three space-separated integers, <strong>N</strong>, <strong>M</strong>, and <strong>K</strong>. | |
| Then <strong>K</strong> lines follow, the <em>i</em>th of which contains the four space-separated integers | |
| <strong>L<sub>i</sub></strong>, <strong>S<sub>i,1</sub></strong>, <strong>A<sub>i</sub></strong>, and <strong>B<sub>i</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th skyline, print a line containing "Case #<strong>i</strong>: " followed by four space-separated integers, | |
| the total amount of black, white, grey, and blue paint which Pierre will have left over, respectively (in cm<sup>2</sup>), after completing all possible variations of his painting. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 100 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 1,000,000,000 <br /> | |
| 1 ≤ <strong>M</strong> ≤ 200,000 <br /> | |
| 1 ≤ <strong>K</strong> ≤ 100 <br /> | |
| 1 ≤ <strong>L<sub>i</sub></strong> ≤ M <br /> | |
| 1 ≤ <strong>H<sub>i</sub></strong>, <strong>S<sub>i,j</sub></strong> ≤ <strong>N</strong> - 1 <br /> | |
| 0 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong> < <strong>N</strong> - 1 <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, there's only one possible painting, with the top cell painted white, and the remaining two cells painted grey. Pierre will buy 1 can each of white and grey paint, and have 1,000,000,006 and 1,000,000,005 cm<sup>2</sup> left over of those colors, respectively. | |
| </p> | |
| <p> | |
| In the second case, there are 6 possible paintings: three with the cloud covering one cell, two with the cloud covering two cells, and one with the cloud covering three cells. Therefore, Pierre will use 10 cm<sup>2</sup> of white paint in total. | |
| </p> | |
| <p> | |
| The fourth case corresponds to the picture shown above. | |
| </p> | |