Mirrorball


Submit solution

Points: 1
Time limit: 1.5s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++, Java, Python

You are walking down Hindley Street one day when you find three mysterious cloth bags, numbered 1, 2 and 3. Bags 2 and 3 are empty, but bag 1 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 50\%.

  • 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 1200 times:

  • Color move: Pick a color c, a source bag b_1, and a destination bag b_2. Take all balls out of b_1, move all balls that are observed as color c (i.e., whose current sampled appearance is c) into b_2, and return all remaining balls to b_1.

  • Full move: Pick a source bag b_1 and a destination bag b_2. Move every ball from b_1 into b_2, 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 n (1 \le n \le 10), the number of possible colors a mirrorball can have.

The interactor then sends n lines, each containing an integer k and a string c (1 \le k \le 10,\ |c| = k,\ c \ne \texttt{all}), indicating that c is a valid color.

Your program may then perform at most 1200 operations:

  • Print b1 b2 c to perform a color move: remove all balls from bag b_1, move all balls currently appearing as color c, and return all remaining balls to b_1. The interactor responds with a single integer, the number of balls that appeared as color c.

  • Print b1 b2 all to perform a full move: move every ball from b_1 into b_2 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 0 \le x \le 10^{9}.

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 1200 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 10^9 balls initially in bag 1.
  • 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 50\% of the time and blue 50\% of the time.

Let's say we know that, in this case, bag 1 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 1 \rightarrow 2.
    • 5 balls appeared as red, and were moved.
      • 2 of them were red stable balls.
      • 3 of them were red-blue meta balls that appeared as red.

  • Move all red balls from bag 2 \rightarrow 3.
    • 3 balls appeared as red, and were moved.
      • 2 of them were red stable balls.
      • 1 of them was a red-blue meta ball that appeared as red.

  • Move all blue balls from bag 1 \rightarrow 2.
    • 4 balls appeared as blue, and were moved.
      • 3 of them were blue stable balls.
      • 1 of them was a red-blue meta ball that appeared as blue.

  • Move all blue balls from bag 2 \rightarrow 1.
    • 5 balls appeared as blue, and were moved.
      • 3 of them were blue stable balls.
      • 2 of them were red-blue meta balls that appeared as blue.

  • Move all balls from bag 1 \rightarrow 3.
    • 6 balls were moved; we didn't record their color.
      • 3 of them were blue stable balls.
      • 3 of them were red-blue meta balls.

  • We guessed that there were 5 stable balls, which was correct.

Comments

There are no comments at the moment.