| <p> | |
| Mr. Fox sure loves his socks! He stores his many indistinguishable socks in a set of <strong>N</strong> bins, | |
| which are arranged in a circle and numbered in clockwise order. | |
| For every 1 ≤ <strong>i</strong> < <strong>N</strong>, the next bin clockwise of bin | |
| <strong>i</strong> is bin <strong>i</strong>+1, and the next bin clockwise of bin | |
| <strong>N</strong> is bin 1. Initially, the <strong>i</strong>th bin contains <strong>S<sub>i</sub></strong> socks. | |
| </p> | |
| <p> | |
| Never being quite satisfied with his sock collection, Mr. Fox would like to perform <strong>M</strong> operations on it, | |
| one after another. Each operation <strong>i</strong> may be of one of the following 4 types, determined by the value of <strong>O<sub>i</sub></strong>: | |
| </p> | |
| <ol> | |
| <li><p> | |
| Given integers <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, | |
| <strong>C<sub>i</sub></strong>, and <strong>D<sub>i</sub></strong>, | |
| add <strong>C<sub>i</sub></strong> + 0*<strong>D<sub>i</sub></strong> | |
| socks to bin <strong>A<sub>i</sub></strong>, | |
| add <strong>C<sub>i</sub></strong> + 1*<strong>D<sub>i</sub></strong> | |
| socks to the next bin clockwise of bin <strong>A<sub>i</sub></strong>, | |
| add <strong>C<sub>i</sub></strong> + 2*<strong>D<sub>i</sub></strong> | |
| to the next bin clockwise of that one, and so on until this has been done for <strong>B<sub>i</sub></strong> bins. | |
| Determine the total number of socks added in this process. | |
| </p></li> | |
| <li><p> | |
| Given integers <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, | |
| and <strong>C<sub>i</sub></strong>, remove all of the socks from bin <strong>A<sub>i</sub></strong> | |
| and then add <strong>C<sub>i</sub></strong> socks to it. | |
| Do the same for the next bin clockwise of <strong>A<sub>i</sub></strong>, | |
| and so on until this has been done for <strong>B<sub>i</sub></strong> bins. | |
| Determine the sum of two values: | |
| the total number of socks removed in this process, and the total number of socks added in this process. | |
| </p></li> | |
| <li><p> | |
| Given integers <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>, | |
| count the number of socks in bin <strong>A<sub>i</sub></strong> (without removing them), | |
| the number of socks in the next bin clockwise of <strong>A<sub>i</sub></strong>, | |
| and so on until the socks in <strong>B<sub>i</sub></strong> bins have been counted. | |
| Determine the total number of socks counted in this process. | |
| </p></li> | |
| <li><p> | |
| Given integers <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>, | |
| check if bin <strong>A<sub>i</sub></strong> contains an odd number of socks. | |
| Do the same for the next bin clockwise of <strong>A<sub>i</sub></strong>, | |
| and so on until this has been done for <strong>B<sub>i</sub></strong> bins. | |
| Determine the total number of these bins that contain an odd number of socks. | |
| </p></li> | |
| </ol> | |
| <p> | |
| Can you help Mr. Fox keep track of his socks? | |
| Note the value calculated during each of the <strong>M</strong> operations as they're performed, | |
| and then output the sum of all <strong>M</strong> of the values modulo 10<sup>9</sup>. | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of sock collections Mr. Fox has. | |
| For each sock collection, there are 7 lines containing the following space-separated integers: | |
| <ol> | |
| <li><strong>N</strong> <strong>M</strong></li> | |
| <li><strong>S<sub>1</sub></strong> <strong>S<sub>2</sub></strong> | |
| <strong>X<sub>S</sub></strong> <strong>Y<sub>S</sub></strong> <strong>Z<sub>S</sub></strong></li> | |
| <li><strong>O<sub>1</sub></strong> <strong>O<sub>2</sub></strong> | |
| <strong>X<sub>O</sub></strong> <strong>Y<sub>O</sub></strong> <strong>Z<sub>O</sub></strong></li> | |
| <li><strong>A<sub>1</sub></strong> <strong>A<sub>2</sub></strong> | |
| <strong>X<sub>A</sub></strong> <strong>Y<sub>A</sub></strong> <strong>Z<sub>A</sub></strong></li> | |
| <li><strong>B<sub>1</sub></strong> <strong>B<sub>2</sub></strong> | |
| <strong>X<sub>B</sub></strong> <strong>Y<sub>B</sub></strong> <strong>Z<sub>B</sub></strong></li> | |
| <li><strong>C<sub>1</sub></strong> <strong>C<sub>2</sub></strong> | |
| <strong>X<sub>C</sub></strong> <strong>Y<sub>C</sub></strong> <strong>Z<sub>C</sub></strong></li> | |
| <li><strong>D<sub>1</sub></strong> <strong>D<sub>2</sub></strong> | |
| <strong>X<sub>D</sub></strong> <strong>Y<sub>D</sub></strong> <strong>Z<sub>D</sub></strong></li> | |
| </ol> | |
| </p> | |
| <p> | |
| The first two elements of each sequence of integers (<strong>S</strong>, <strong>O</strong>, <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, and <strong>D</strong>) are given, and the rest are computed with the following | |
| pseudorandom generators: | |
| <ul> | |
| <li><strong>S<sub>i</sub></strong> = (<strong>X<sub>S</sub></strong>*<strong>S<sub>i-2</sub></strong> + | |
| <strong>Y<sub>S</sub></strong>*<strong>S<sub>i-1</sub></strong> + <strong>Z<sub>S</sub></strong>) | |
| modulo 10<sup>9</sup>, for 3 ≤ i ≤ <strong>N</strong> | |
| </li> | |
| <li><strong>O<sub>i</sub></strong> = ((<strong>X<sub>O</sub></strong>*<strong>O<sub>i-2</sub></strong> + | |
| <strong>Y<sub>O</sub></strong>*<strong>O<sub>i-1</sub></strong> + <strong>Z<sub>O</sub></strong>) | |
| modulo 4) + 1, for 3 ≤ i ≤ <strong>M</strong> | |
| </li> | |
| <li><strong>A<sub>i</sub></strong> = ((<strong>X<sub>A</sub></strong>*<strong>A<sub>i-2</sub></strong> + | |
| <strong>Y<sub>A</sub></strong>*<strong>A<sub>i-1</sub></strong> + <strong>Z<sub>A</sub></strong>) | |
| modulo <strong>N</strong>) + 1, for 3 ≤ i ≤ <strong>M</strong> | |
| </li> | |
| <li><strong>B<sub>i</sub></strong> = ((<strong>X<sub>B</sub></strong>*<strong>B<sub>i-2</sub></strong> + | |
| <strong>Y<sub>B</sub></strong>*<strong>B<sub>i-1</sub></strong> + <strong>Z<sub>B</sub></strong>) | |
| modulo <strong>N</strong>) + 1, for 3 ≤ i ≤ <strong>M</strong> | |
| </li> | |
| <li><strong>C<sub>i</sub></strong> = (<strong>X<sub>C</sub></strong>*<strong>C<sub>i-2</sub></strong> + | |
| <strong>Y<sub>C</sub></strong>*<strong>C<sub>i-1</sub></strong> + <strong>Z<sub>C</sub></strong>) | |
| modulo 10<sup>9</sup>, for 3 ≤ i ≤ <strong>M</strong> | |
| </li> | |
| <li><strong>D<sub>i</sub></strong> = (<strong>X<sub>D</sub></strong>*<strong>D<sub>i-2</sub></strong> + | |
| <strong>Y<sub>D</sub></strong>*<strong>D<sub>i-1</sub></strong> + <strong>Z<sub>D</sub></strong>) | |
| modulo 10<sup>9</sup>, for 3 ≤ i ≤ <strong>M</strong> | |
| </li> | |
| </ul> | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <strong>i</strong>th sock collection, print a line containing "Case #<strong>i</strong>: " | |
| followed by the sum of all values calculated during each operation, modulo 10<sup>9</sup>. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 20 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 1,000,000 <br /> | |
| 2 ≤ <strong>M</strong> ≤ 1,000,000 <br /> | |
| 0 ≤ <strong>S<sub>i</sub></strong> < 10<sup>9</sup> <br /> | |
| 1 ≤ <strong>O<sub>i</sub></strong> ≤ 4 <br /> | |
| 1 ≤ <strong>A<sub>i</sub></strong> ≤ <strong>N</strong> <br /> | |
| 1 ≤ <strong>B<sub>i</sub></strong> ≤ <strong>N</strong> <br /> | |
| 0 ≤ <strong>C<sub>i</sub></strong> < 10<sup>9</sup> <br /> | |
| 0 ≤ <strong>D<sub>i</sub></strong> < 10<sup>9</sup> <br /> | |
| 0 ≤ <strong>X<sub>S</sub></strong>, <strong>X<sub>O</sub></strong>, <strong>X<sub>A</sub></strong> | |
| <strong>X<sub>B</sub></strong>, <strong>X<sub>C</sub></strong>, <strong>X<sub>D</sub></strong> | |
| < 10<sup>9</sup> <br /> | |
| 0 ≤ <strong>Y<sub>S</sub></strong>, <strong>Y<sub>O</sub></strong>, <strong>Y<sub>A</sub></strong> | |
| <strong>Y<sub>B</sub></strong>, <strong>Y<sub>C</sub></strong>, <strong>Y<sub>D</sub></strong> | |
| < 10<sup>9</sup> <br /> | |
| 0 ≤ <strong>Z<sub>S</sub></strong>, <strong>Z<sub>O</sub></strong>, <strong>Z<sub>A</sub></strong> | |
| <strong>Z<sub>B</sub></strong>, <strong>Z<sub>C</sub></strong>, <strong>Z<sub>D</sub></strong> | |
| < 10<sup>9</sup> <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| The first collection has 5 bins that all have 0 socks. None of the operations involve any socks at all, so the answer is 0. | |
| </p> | |
| <p> | |
| The second collection has 5 bins with 1, 2, 3, 4, and 5 socks. Mr. Fox performs the operations 1, 2, 3, and 4 in order. For each operation, A = 1, B = 5, C = 0, D = 0. He first adds 0 socks to the bins, then removes all 15 socks, then counts the 0 remaining socks, and then counts 0 odd bins, for a total of 15. | |
| </p> | |
| <p> | |
| The third collection also has 5 bins with 1, 2, 3, 4, and 5 socks. Mr. Fox performs the same operations, but this time C and D take on the values 1, 2, 3, and 4 in that order. He adds 15 socks to the bins, then removes all 30 socks and adds 2 socks to each bin, then counts those 10 socks, and then counts 0 odd bins. The total is then 15 + 30 + 10 + 10 = 65. | |
| </p> | |