Stamps
Time limit1sMemory limit128 MB
For each h and k with h+k<=9, find k stamp values whose at-most-h sums cover 1..n consecutively for the longest range, then print the lexicographically smallest such set and n.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Brute force, Combinatorics, Greedy
- Solved
- No attempts yet
Problem
The government of Nova Mareterrania attaches revenue stamps to legal documents so that it can collect income from them. By recent law, each class of document may carry only a limited number of stamps. The government wants to decide how many stamp denominations to print, and of what values, so that the widest possible range of totals can be assembled within that limit. Every stamp is worth a whole number of dollars.
When a document may carry at most stamps and different denominations are available, let be the largest value such that every amount from to can be formed using at most stamps. A denomination may be used more than once, and there is no limit on how many stamps of each denomination are available (only the total count is limited). One denomination is always , since otherwise the amount could not be formed.
For example, with and : using denominations and you can form every amount from to , but using denominations and you can form every amount from to . The second choice is better, so .
Given and , choose the denominations that maximize , and report both the chosen denominations and the value .
Input
The input consists of several lines. Each line contains two integers and separated by a space, with , , and .
The input ends with a line containing 0 0, which is not a query and must not be processed.
Output
For each query, print one line: the chosen denominations in ascending order, each right-justified in a field 3 characters wide, followed by a space, an arrow ->, and the value right-justified in a field 3 characters wide.
Several different sets of denominations can reach the same maximum . When that happens, output the lexicographically smallest such set: compare the two ascending denomination sequences position by position and take the one whose value is smaller at the first position where they differ.