#P1432. 倒水问题

倒水问题

Description

Assume two jugs AA and BB, with unlimited water supply. You can use three kinds of operations:

  • Fill a jug.
  • Empty a jug.
  • Pour water from one jug into the other.

When pouring from one jug into the other, you must stop when either the first jug becomes empty or the second jug becomes full. For example, if jug AA has capacity 55 gallons and jug BB has capacity 66 gallons, and the amount of water is 88 gallons, then when pouring from jug AA into jug BB, jug BB becomes full and jug AA has 33 gallons left.

The problem has 33 parameters: CaC_a, CbC_b, and NN, which are the capacities of jugs AA and BB and the target amount NN, respectively. The goal is to output a sequence of pouring steps so that the amount of water in jug BB is exactly NN.

If there are multiple valid sequences, output any one with the minimum number of operations.

Input Format

The first line contains the number of test cases TT.

The next TT lines each contain three numbers CaC_a, CbC_b, and NN, as described above.

Constraints: T30T \le 30, 0<CaCb0 < C_a \le C_b, NCb1000N \le C_b \le 1000, and CaC_a and CbC_b are coprime.

Output Format

Print TT lines. For each test case, first output the number of operations aia_i (a solution is guaranteed to exist). Then output aia_i integers, each representing an operation:

  • Operation 1: fill A means fill AA to full.
  • Operation 2: fill B.
  • Operation 3: empty A means empty AA.
  • Operation 4: empty B.
  • Operation 5: pour B A means pour from BB into AA (until AA is full or BB is empty).
  • Operation 6: pour A B.
2
3 5 4 
5 7 3 

6 2 5 3 5 2 5 
6 1 6 1 6 4 6 

1
26 29 11

22 1 6 1 6 4 6 1 6 4 6 1 6 4 6 1 6 4 6 1 6 4 6 

Hint

SPJ is enabled.

If your sequence is better than the official answer, it will show UKE. In this case, please contact an administrator to update the testdata.

If your sequence is worse than the official answer, your score will be reduced accordingly.

Translated by ChatGPT 5