Mirrorball
You are walking down Hindley Street one day when you find three mysterious cloth bags, numbered ,
and
. Bags
and
are empty, but bag
is filled with several "mirrorballs". Mirrorballs come in two flavours:
Meta: A meta ball has two fixed internal colors (which are always different). Whenever you take the ball out of a bag and observe it, it independently appears as either of its two internal colors, each with probability
.
Stable: A stable ball has a single fixed internal color, and always appears as that color whenever observed.
For example, a meta ball might have internal colors red and blue, appearing red half the time and blue half the time. A stable ball with color green always appears green.
Since you don't like randomness, you'd like to figure out how many of your balls are stable. You may perform the following operations up to times:
Color move: Pick a color
, a source bag
, and a destination bag
. Take all balls out of
, move all balls that are observed as color
(i.e., whose current sampled appearance is
) into
, and return all remaining balls to
.
Full move: Pick a source bag
and a destination bag
. Move every ball from
into
, regardless of what color they appear as.
Using these operations, can you determine how many of your balls are stable?
Interaction
This is an interactive problem. Your submission will be run against an interactor, which reads from the standard output of your program and writes to its standard input.
The interactor first sends one integer
, the number of possible colors a mirrorball can have.
The interactor then sends lines, each containing an integer
and a string
, indicating that
is a valid color.
Your program may then perform at most operations:
Print
b1 b2 cto perform a color move: remove all balls from bag, move all balls currently appearing as color
, and return all remaining balls to
. The interactor responds with a single integer, the number of balls that appeared as color
.
Print
b1 b2 allto perform a full move: move every ball frominto
unconditionally. The interactor responds with a single integer — the total number of balls moved.
Once you have determined the number of stable balls, print x ! (with a space before !) to submit your answer, where .
After printing each line, you must flush stdout. Failure to do so may result in an Idleness Limit Exceeded verdict.
For C++, you may use cout << endl to output, or call cout.flush() after each output.
For Python, use flush=True in print(), or call sys.stdout.flush() after each output.
You will receive a Wrong Answer verdict if:
- You perform more than
operations.
- Your guess for the number of stable balls is incorrect.
- Your output does not follow the required format.
Notes
- The internal colors of each meta ball are fixed in advance; they do not change between observations.
- Every time a ball is checked for its color, it is independently sampled.
- Each observation of a meta ball is independent of previous observations.
- There are at most
balls initially in bag
.
- It is guaranteed that the number of test cases in this problem does not exceed $100$.
Example
Input 1
Interaction 1
> 2
> 3 red
> 4 blue
< 1 2 red
> 5
< 2 3 red
> 3
< 1 2 blue
> 4
< 2 1 blue
> 5
< 1 3 all
> 6
< 5 !
Explanation 1
There are only two colors, so there are only three types of balls:
- A red stable ball.
- A blue stable ball.
- A red-blue meta ball, that shows up as red
of the time and blue
of the time.
Let's say we know that, in this case, bag started with:
- 2 red stable balls.
- 3 blue stable balls.
- 5 red-blue meta balls.

Now let's run through the interaction:
- Move all red balls from bag
.
balls appeared as red, and were moved.
of them were red stable balls.
of them were red-blue meta balls that appeared as red.

- Move all red balls from bag
.
balls appeared as red, and were moved.
of them were red stable balls.
of them was a red-blue meta ball that appeared as red.

- Move all blue balls from bag
.
balls appeared as blue, and were moved.
of them were blue stable balls.
of them was a red-blue meta ball that appeared as blue.

- Move all blue balls from bag
.
balls appeared as blue, and were moved.
of them were blue stable balls.
of them were red-blue meta balls that appeared as blue.

- Move all balls from bag
.
balls were moved; we didn't record their color.
of them were blue stable balls.
of them were red-blue meta balls.

- We guessed that there were
stable balls, which was correct.
Comments