Pyramids
Time limit5sMemory limit512 MB
Given a count of stones, find the smallest set of distinct high or low pyramids (height at least 2) that uses every stone, breaking ties by maximizing sizes lexicographically, or report impossible.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Backtracking, Math
- Solved
- No attempts yet
Problem
If you have enough identical cube-shaped stones, you can build a pyramid. There are two kinds.
High pyramid. Place a square of stones on the bottom, then on top of it, then , shrinking each side by 1 per layer until a single stone sits on top. A high pyramid with base length has height and uses stones.
Low pyramid. Place on the bottom, then shrink each side by 2 per layer: , , …. If the base length is even the top layer is ; if it is odd the top layer is . A low pyramid with base length has height .
Long ago a pharaoh inherited an enormous number of cube stones and ordered that all of them be used to build pyramids. His architect explained that what can be built depends on the number of stones: with 10 stones you can build a low pyramid of base 3, with 5 stones a high pyramid of base 2, but with 7 stones no pyramid can be built using every stone.
The pharaoh was furious, and after several days set the following conditions.
- Every stone must be used.
- At least one pyramid must be built, and the number of pyramids must be as small as possible.
- Each shape (same kind and same base length) may be built at most once.
- Every pyramid must have height at least 2. That is, a high pyramid needs base and a low pyramid needs base .
- Subject to the above, make the pyramid that uses the most stones as large as possible.
- Then make the second-largest pyramid as large as possible.
- Continue likewise, maximizing the size of the third, fourth, … pyramid in turn.
Given the number of stones, determine how the pyramids should be built to satisfy these conditions.
Input
The input consists of several test cases. Each test case is a single line containing the number of stones (). The last line contains a single , which is not processed.
Output
For each test case, first print Case t: using the test-case number (starting from 1). Then print the pyramids to build, in order from the one using the most stones to the fewest, separated by spaces. Each pyramid is written as its base length followed by L for a low pyramid or H for a high pyramid (for example, a high pyramid of base 3 is 3H). If two pyramids use exactly the same number of stones, print the high pyramid (H) first. If no set of pyramids can satisfy the pharaoh's conditions, print impossible.