I've Got Your Back(gammon)

Time limit1sMemory limit128 MB

Problem

A friend is writing a program to play backgammon and needs a numbering scheme for the final board positions. At the end of the game, all 15 of a player's pieces sit on 6 board positions called points, numbered 1 through 6. The pieces can be distributed in any way across the points: all 15 on point 3; or 5 on point 6, 2 on point 5, 3 on point 4 and 5 on point 2; and so on. There are exactly 15504 such configurations, and each must be stored in a linear array, so a mapping between each configuration and an array index is needed.

Represent a configuration by listing the number of pieces on each point, starting with point 6 and ending with point 1. For instance, "all 15 pieces on point 3" is written $(0, 0, 0, 15, 0, 0)$, and "5 on point 6, 2 on point 5, 3 on point 4, 5 on point 2" is written $(5, 2, 3, 0, 5, 0)$.

Order all configurations by lexicographic ordering of these 6-tuples, comparing the point-6 count first, then the point-5 count, and so on. The ordering therefore begins with $(0, 0, 0, 0, 0, 15)$, then $(0, 0, 0, 0, 1, 14)$, $(0, 0, 0, 0, 2, 13)$, ..., $(0, 0, 0, 0, 14, 1)$, $(0, 0, 0, 0, 15, 0)$, $(0, 0, 0, 1, 0, 14)$, $(0, 0, 0, 1, 1, 13)$, and so on, ending with $(15, 0, 0, 0, 0, 0)$.

Assign array indices in this order: the first configuration, $(0, 0, 0, 0, 0, 15)$ (all 15 pieces on point 1), gets index $0$, and the last configuration, $(15, 0, 0, 0, 0, 0)$ (all 15 pieces on point 6), gets index $15503$. For each query you must map a configuration to its index, or an index back to its configuration.

Input

Each query is on its own line and starts with a single character, either m or u.

  • If it is m, it is followed by a configuration: six integers giving the number of pieces on points 6, 5, 4, 3, 2, 1 (in that order), whose sum is 15. You must determine the array index it maps to.
  • If it is u, it is followed by a single integer array index $i$ with $0 \le i < 15504$. You must determine the configuration that maps to it.

A line containing the single character e ends the input.

Output

For each query, print one line of the form Case X: A, where $X$ is the query number (starting at 1 and increasing by one per query) and $A$ is the answer: for an m query, the array index; for a u query, the configuration written as the six piece counts for points 6, 5, 4, 3, 2, 1 separated by single spaces.