hackercup / 2016 /round3 /matt_laundro.md
wjomlex's picture
2016 Problems
07f695f verified
|
raw
history blame
No virus
3.78 kB

Matt Laundro is about to engage in his favourite activity — doing laundry! He's brought L loads of laundry to his local laundromat, which has recently been cracking down on excessive washer and dryer usage. It turns out the other customers weren't very thrilled when they saw Matt using a billion washers and dryers simultaneously, so he's now been restricted to just one washer and one dryer. Matt's ith load of laundry takes Wi minutes to wash, and Di minutes to dry. As is usually the case with laundry, each load takes at least as long to dry as it does to wash. At any point in time, each machine may only be processing at most one load of laundry.

As one might expect, Matt wants to wash and then dry each of his L loads of laundry. Unfortunately, the laundromat closes in K minutes, so he might not be able to get through every load. But he'll try his best! If he chooses to wash and dry the ith load of laundry, it will go through the following steps in order:

  1. A non-negative amount of time after Matt arrives at the laundromat, Matt places the load in the washing machine
  2. Wi minutes later, he removes the load from the washing machine, placing it in a temporary holding basket (which has unlimited space)
  3. A non-negative amount of time later, he places the load in the dryer
  4. Di minutes later, he removes the load from the dryer

Matt can instantaneously add laundry to or remove laundry from a machine. He can choose to wash and dry any of his loads and they can be washed and dried in any order as long as they each follow the steps above. Help Matt maximize the number of loads he can finish washing and drying in K minutes, and amongst all the ways he could finish that many loads, find the minimum amount of time it will take for all of those loads to be washed and dried.

Input

In this problem, the sequences W and D are generated using a pseudo- random number generator. The generator first produces two length-L sequences X and Y. X1 and Y1 are given, and the remaining terms X2...XL and Y2...YL are computed as follows:

Xi = ( (Ax * Xi-1 + Bx) mod Cx) + 1
Yi = ( (Ay * Yi-1 + By) mod Cy) + 1

After X and Y are generated, W and D are computed as follows:

Wi = min {Xi, Yi}
Di = max {Xi, Yi}

Input begins with an integer T, the number of times Matt goes to the laundromat. For each trip to the laundromat, there is first a line containing the space-separated integers L and K, then a line containing the space-separated integers Ax, Bx, Cx, and X1, then a line containing the space-separated integers Ay, By, Cy, and Y1.

Output

For the ith trip, print a line containing "Case #i: " followed by the maximum number of loads Matt can finish before the laundromat closes, and the minimum amount of time it will take to finish that many.

Constraints

1 ≤ T ≤ 50
1 ≤ L ≤ 500,000
1 ≤ K ≤ 1,000,000,000
1 ≤ Ax, Bx, Cx ≤ 1,000,000,000
1 ≤ Ay, By, Cy ≤ 1,000,000,000
1 ≤ X1Cx
1 ≤ Y1Cy

Explanation of Sample

In the first case, W and D are the same: {1, 3, 5, 7}. Matt can finish the first three loads in 14 minutes. He starts by washing the third load, which takes 5 minutes. Then, while that load is drying he can wash the first and second loads. The second case is the same, except Matt now has just enough time to wash all of the loads. He can start by washing the fourth load, which takes 7 minutes. In the third case, W = {2, 1, 6, 2, 1, 3} and D = {3, 4, 7, 5, 10, 6}.