P5116 [USACO18DEC] Mixing Milk B

Description

Farming, especially milk production, is a highly competitive industry. Farmer John realizes that if he does not innovate in the milk production process, his dairy business could suffer a major blow. Fortunately, Farmer John has come up with a great idea. His three award-winning cows, Bessie, Elsie, and Mildred, produce milk with slightly different flavors, and he plans to mix these three kinds of milk to create the perfect taste. To mix these three different kinds of milk, he uses three buckets, each containing the milk produced by one of the cows. These buckets may have different capacities, and they may not be completely full. Then he pours the milk from bucket $1$ into bucket $2$, then pours the milk from bucket $2$ into bucket $3$, then pours the milk from bucket $3$ into bucket $1$, then pours the milk from bucket $1$ into bucket $2$, and so on in a cycle, for a total of $100$ pours (so the $100$-th operation will be bucket $1$ poured into bucket $2$). When Farmer John pours milk from bucket $a$ into bucket $b$, he pours as much as possible until bucket $a$ is empty or bucket $b$ is full. Please tell Farmer John how much milk will be in each bucket after he has poured $100$ times.

Input Format

The first line of the input file contains two space-separated integers: the capacity of the first bucket $c_1$, and the amount of milk in the first bucket $m_1$. Both $c_1$ and $m_1$ are positive and do not exceed $10^9$. The second and third lines similarly contain the capacities and milk amounts for the second and third buckets.

Output Format

Output three lines, giving the amount of milk in each bucket after $100$ pours.

Explanation/Hint

In this example, after each pour, the amounts of milk in the buckets are as follows. 0. Initial state: 3 4 5 1. Bucket 1->2: 0 7 5 2. Bucket 2->3: 0 0 12 3. Bucket 3->1: 10 0 2 4. Bucket 1->2: 0 10 2 5. Bucket 2->3: 0 0 12 (The last three states then repeat in a cycle.) Translated by ChatGPT 5