luogu#P5116. [USACO18DEC] Mixing Milk B

[USACO18DEC] Mixing Milk B

Problem 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 11 into bucket 22, then pours the milk from bucket 22 into bucket 33, then pours the milk from bucket 33 into bucket 11, then pours the milk from bucket 11 into bucket 22, and so on in a cycle, for a total of 100100 pours (so the 100100-th operation will be bucket 11 poured into bucket 22). When Farmer John pours milk from bucket aa into bucket bb, he pours as much as possible until bucket aa is empty or bucket bb is full.

Please tell Farmer John how much milk will be in each bucket after he has poured 100100 times.

Input Format

The first line of the input file contains two space-separated integers: the capacity of the first bucket c1c_1, and the amount of milk in the first bucket m1m_1. Both c1c_1 and m1m_1 are positive and do not exceed 10910^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 100100 pours.

10 3
11 4
12 5
0
10
2

Hint

In this example, after each pour, the amounts of milk in the buckets are as follows.

  1. Initial state: 3 4 5
  2. Bucket 1->2: 0 7 5
  3. Bucket 2->3: 0 0 12
  4. Bucket 3->1: 10 0 2
  5. Bucket 1->2: 0 10 2
  6. Bucket 2->3: 0 0 12

(The last three states then repeat in a cycle.)

Translated by ChatGPT 5