| <p> | |
| Wilson, everyone's favorite recurring programming contest character, has finally decided that it's time to retire. Why? | |
| After several more job changes and miraculous promotions, not only has he become incredibly wealthy, but he's | |
| ended up stuck in a boring administrative role, which was the last thing he wanted! | |
| </p> | |
| <p> | |
| Wilson is currently employed as the head of accounting at a pie delivery company, and has been given | |
| one last assignment to complete before his retirement — computing delivery truck gas usage. He | |
| wishes he could just drive one of the trucks instead... | |
| </p> | |
| <p> | |
| The pie delivery company services a network of <strong>N</strong> + 1 towns, which rather looks like a pie itself (or perhaps like an umbrella...). | |
| <strong>N</strong> of the towns form a circle around the outside of the region, and are numbered from 1 to <strong>N</strong> in clockwise order. | |
| They are connected to one another by <strong>N</strong> roads, each of which can be driven along in either direction. | |
| The <em>i</em>th of these roads requires <strong>O<sub>i</sub></strong> litres of gas to drive along, and runs between | |
| towns <em>i</em> and <em>i</em> + 1 (unless <em>i</em> = <strong>N</strong>, in which case it instead runs between towns <strong>N</strong> and 1). | |
| </p> | |
| <p> | |
| The remaining town, numbered <strong>N</strong> + 1, lies in the center of the region. There are <strong>N</strong> roads connecting it to | |
| the other towns, each of which can be driven along in either direction. | |
| The <em>i</em>th of these roads requires <strong>R<sub>i</sub></strong> litres of gas to drive along, and runs between | |
| towns <strong>N</strong> + 1 and <em>i</em>. | |
| </p> | |
| <p> | |
| A total of <strong>N</strong> * (<strong>N</strong> + 1) / 2 pie deliveries are scheduled to be carried out, one for each distinct pair of towns. | |
| In particular, for every town <em>i</em>, <em>i</em> - 1 deliveries will originate from it, with the <em>j</em>th of them having town <em>j</em> as its destination. | |
| The truck drivers will have to pay the gas expenses out of their own pockets, of course, so for each delivery, the truck driver will choose a | |
| sequence of roads to follow from the initial town to the destination such that the total amount of gas required for their trip is minimized. | |
| Wilson's task is to tally up the total amount of gas which will be used across all <strong>N</strong> * (<strong>N</strong> + 1) / 2 of the deliveries. | |
| He's been instructed to only determine the value of this sum modulo 1,000,000,007. | |
| </p> | |
| <p> | |
| You're given <strong>O<sub>1</sub></strong>, and <strong>O<sub>2..N</sub></strong> may then be calculated as follows using given constants | |
| <strong>A<sub>o</sub></strong>, <strong>B<sub>o</sub></strong>, <strong>C<sub>o</sub></strong>, and <strong>D<sub>o</sub></strong>. | |
| </p> | |
| <p> | |
| <strong>O<sub>i</sub></strong> = ((<strong>A<sub>o</sub></strong> * <strong>O<sub>i-1</sub></strong> + <strong>B<sub>o</sub></strong>) | |
| %<strong>C<sub>o</sub></strong> + <strong>D<sub>o</sub></strong> | |
| </p> | |
| <p> | |
| You're given <strong>R<sub>1</sub></strong>, and <strong>R<sub>2..N</sub></strong> may then be calculated as follows using given constants | |
| <strong>A<sub>r</sub></strong>, <strong>B<sub>r</sub></strong>, <strong>C<sub>r</sub></strong>, and <strong>D<sub>r</sub></strong>. | |
| </p> | |
| <p> | |
| <strong>R<sub>i</sub></strong> = ((<strong>A<sub>r</sub></strong> * <strong>R<sub>i-1</sub></strong> + <strong>B<sub>r</sub></strong>) | |
| %<strong>C<sub>r</sub></strong> + <strong>D<sub>r</sub></strong> | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of different regions of towns. | |
| For each region, there is first a line containing the integer <strong>N</strong>. | |
| Then there is a line with five space-separated integers, <strong>O<sub>1</sub></strong>, <strong>A<sub>o</sub></strong>, <strong>B<sub>o</sub></strong>, | |
| <strong>C<sub>o</sub></strong>, and <strong>D<sub>o</sub></strong>. | |
| Then there is a line with five space-separated integers, <strong>R<sub>1</sub></strong>, <strong>A<sub>r</sub></strong>, <strong>B<sub>r</sub></strong>, | |
| <strong>C<sub>r</sub></strong>, and <strong>D<sub>r</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th graph, print a line containing "Case #<strong>i</strong>: " | |
| followed by the total amount of gas which will be used (in litres), modulo 1,000,000,007 | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 40 <br /> | |
| 3 ≤ <strong>N</strong> ≤ 1,000,000 <br /> | |
| 1 ≤ <strong>O<sub>1</sub></strong>, <strong>C<sub>o</sub></strong>, <strong>D<sub>o</sub></strong> ≤ 1,000,000 <br /> | |
| 0 ≤ <strong>A<sub>o</sub></strong>, <strong>B<sub>o</sub></strong> ≤ 1,000,000 <br /> | |
| 1 ≤ <strong>R<sub>1</sub></strong>, <strong>C<sub>r</sub></strong>, <strong>D<sub>r</sub></strong> ≤ 1,000,000 <br /> | |
| 0 ≤ <strong>A<sub>r</sub></strong>, <strong>B<sub>r</sub></strong> ≤ 1,000,000 <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, the roads running around the outside require 1, 3, and 5 litres of gas to drive along, respectively. Meanwhile, the roads connected to the central town require 1, 2, and 2 litres of gas to drive along, respectively. The amounts of gas required for the 6 deliveries (in litres) are [1, 1, 2, 2, 3, 3], for a total of 12. | |
| </p> | |