|
<p>You are the King of Byteland, which now consists of <strong>K</strong> rival states. The national |
|
day of Byteland is approaching, and it is a day of great merriment. Each of the |
|
<strong>K</strong> states is throwing its own party. Fortunately your kingdom also has <strong>N</strong> rich and |
|
famous entertainers, numbered from <strong>0</strong> to <strong>N-1</strong>. Its your job to allocate to each state, |
|
some non empty set |
|
of entertainers. Note that the same entertainer cannot be allocated to two |
|
different states. Some entertainers may be unallocated. Also to each allocated |
|
entertainer, you must pay <strong>C</strong> coins to hire him.</p> |
|
|
|
<p>The trouble is, some of the entertainers are fond of some others, and refuse to |
|
spend the national day without their friends, meaning they insist on being allocated to the same state if they are allocated). Under this constraint, you find out that |
|
allocating entertainers to the states becomes impossible. So you appeal to them |
|
to relax their requirements, and they ask you to donate more money to the |
|
entertainment industry. More formally, you will be provided with a 2-dimensional array <strong>R</strong>. |
|
If the total amount of money you donate is less than <strong>R[u][v]</strong>, for <strong>u</strong> not equal |
|
to <strong>v</strong>, then entertainer <strong>#u</strong> will NOT agree to spend the national day without the |
|
company of entertainer <strong>#v</strong>. Note that if you do not allocate entertainer <strong>#u</strong>, then you can safely |
|
ignore his restrictions. <strong>R[u][v]</strong> need not be equal to <strong>R[v][u]</strong>.</p> |
|
<ul> |
|
<p>You are free to donate any non-negative amount of coins as you see fit. Find out |
|
the minimum expenditure you must make to satisfy all entertainers and all |
|
states. Note that your total expenditure is : |
|
(the amount of money you donate + <strong>C</strong> * the number of allocated entertainers). <p> |
|
|
|
<h2> Input:</h2> |
|
|
|
<p> The first line contains <strong>T</strong>, the number of test cases. |
|
Each test contains 3 lines.</p> |
|
<li>The first line contains 3 integers <strong>N</strong>, <strong>K</strong>, <strong>C</strong>.</li> |
|
<li>The second line contains 4 integers <strong>x1</strong>, <strong>a1</strong>, <strong>b1</strong>, <strong>m1</strong></li> |
|
<li>The third line contains 4 integers <strong>x2</strong>, <strong>a2</strong>, <strong>b2</strong>, <strong>m2</strong></li> |
|
<li>Using these values, the array <strong>R</strong> can be generated as follows:</li> |
|
<li>Let <strong>f1</strong>[0] = <strong>x1</strong>, <strong>f2</strong>[0] = <strong>x2</strong>;</li> |
|
<li><strong>f1</strong>[<strong>i</strong>] = (<strong>a1</strong> * <strong>f1</strong>[<strong>i</strong>-1] + <strong>b1</strong>) % <strong>m1</strong> for <strong>i</strong> ≥ 1</li> |
|
<li><strong>f2</strong>[<strong>i</strong>] = (<strong>a2</strong> * <strong>f2</strong>[<strong>i</strong>-1] + <strong>b2</strong>) % <strong>m2</strong> for <strong>i</strong> ≥ 1</li> |
|
<p> |
|
<li>If <strong>i</strong> > <strong>j</strong>, <strong>R</strong>[<strong>i</strong>][<strong>j</strong>] = <strong>f1</strong>[ <strong>i</strong> * (<strong>i</strong>-1) / 2 + <strong>j</strong> ]</li> |
|
<li>If <strong>i</strong> < <strong>j</strong>, <strong>R</strong>[<strong>i</strong>][<strong>j</strong>] = f2[ <strong>j</strong> * (<strong>j</strong>-1) / 2 + <strong>i</strong> ]</li> |
|
<li>Note that <strong>R</strong>[<strong>i</strong>][<strong>j</strong>] is not defined for <strong>i</strong> = <strong>j</strong>.</li> |
|
</p> |
|
<h2> Output:</h2> |
|
|
|
For test case numbered <strong>i</strong>, output "Case #i: " followed by the minimum number |
|
of coins you must spend to satisfy everybody. |
|
|
|
<p> |
|
<h2>Constraints:</h2> |
|
<li><strong>T</strong> ≤ 20</li> |
|
<li>1 ≤ <strong>N</strong> ≤ 1111</li> |
|
<li>1 ≤ <strong>K</strong> ≤ <strong>N</strong></li> |
|
<li>1 ≤ <strong>C</strong> ≤ 1,000,000,000</li> |
|
<li>0 ≤ <strong>x1</strong>, <strong>a1</strong>, <strong>b1</strong> ≤ 1,000,000,000</li> |
|
<li>0 ≤ <strong>x2</strong>, <strong>a2</strong>, <strong>b2</strong> ≤ 1,000,000,000</li> |
|
<li>1 ≤ <strong>m1</strong>, <strong>m2</strong> ≤ 1,000,000,000</li> |
|
</p> |
|
|
|
<p> |
|
<h2>Explanation of Sample Cases:</h2> |
|
<p>In case 1, we get R[1][0] = 20 and R[0][1] = 8. The optimal choice is to donate 8 coins, and allocate entertainer #0 to the only state.</p> |
|
<p>In case 2, R[1][0] = 20 and R[0][1] = 12. The optimal choice here is to donate 0 coins, and allocate both entertainers to the only state.</p> |
|
<p><li>In case 5, the matrix R look like this: </li> |
|
<li>--- 800 1600</li> |
|
<li>400 --- 400</li> |
|
<li>800 1200 ---</li> |
|
<li> The optimal choice is to donate 1,600 coins, and allocate one entertainer to each state.</li> |
|
</p> |
|
|