I've Got Your Back(gammon)
Time limit1sMemory limit128 MB
Map between 6-tuples of 15 pieces on 6 points, ordered lexicographically, and their index among the 15504 configurations.
- Level
Medium5 of 10
- Topics
- Combinatorics, Math, Implementation, Simulation
- Solved
- No attempts yet
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 , and "5 on point 6, 2 on point 5, 3 on point 4, 5 on point 2" is written .
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 , then , , ..., , , , , and so on, ending with .
Assign array indices in this order: the first configuration, (all 15 pieces on point 1), gets index , and the last configuration, (all 15 pieces on point 6), gets index . 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 with . 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 is the query number (starting at 1 and increasing by one per query) and 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.