I've Got Your Back(gammon)

Time limit1sMemory limit128 MB

Summary
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 (0,0,0,15,0,0)(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)(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)(0, 0, 0, 0, 0, 15), then (0,0,0,0,1,14)(0, 0, 0, 0, 1, 14), (0,0,0,0,2,13)(0, 0, 0, 0, 2, 13), ..., (0,0,0,0,14,1)(0, 0, 0, 0, 14, 1), (0,0,0,0,15,0)(0, 0, 0, 0, 15, 0), (0,0,0,1,0,14)(0, 0, 0, 1, 0, 14), (0,0,0,1,1,13)(0, 0, 0, 1, 1, 13), and so on, ending with (15,0,0,0,0,0)(15, 0, 0, 0, 0, 0).

Assign array indices in this order: the first configuration, (0,0,0,0,0,15)(0, 0, 0, 0, 0, 15) (all 15 pieces on point 1), gets index 00, and the last configuration, (15,0,0,0,0,0)(15, 0, 0, 0, 0, 0) (all 15 pieces on point 6), gets index 1550315503. 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 ii with 0≤i<155040 \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 XX is the query number (starting at 1 and increasing by one per query) and AA 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.

Examples3

  1. Example 1

    Input
    m 0 0 0 0 0 15
    u 15503
    e
    
    Expected output
    Case 1: 0
    Case 2: 15 0 0 0 0 0
    
  2. Example 2

    Input
    u 0
    e
    
    Expected output
    Case 1: 0 0 0 0 0 15
    
  3. Example 3

    Input
    m 0 0 0 15 0 0
    m 5 2 3 0 5 0
    e
    
    Expected output
    Case 1: 135
    Case 2: 13121