Fence Boards
Time limit20sMemory limit512 MB
Pick the fewest boards from N unlimited lengths to total exactly L for a fence up to 1e18 long, or report IMPOSSIBLE.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Math
- Solved
- No attempts yet
Problem
You are planning a very long fence. The site is already chosen, and the only work left is collecting the material.
Local hardware stores sell wooden boards in several lengths, and you can buy as many of each length as you want. To avoid waste, the total length of the boards you buy must be exactly the length of the fence.
Given the length of the fence and the board lengths on sale, find the minimum number of boards you have to buy to reach that total exactly.
Note that the fence is very long.
Input
The first line contains the number of test cases . test cases follow.
Each test case consists of two lines. The first line contains two space-separated integers and . is the total length of the fence and is the number of different board lengths on sale. The second line contains the board lengths , separated by spaces.
Limits
Output
For each test case, print one line in the form Case #x: M, where is the case number starting from 1 and is as follows.
- If one or more boards can be bought so that their total length is exactly , then is the minimum number of boards that does this.
- Otherwise is the string
IMPOSSIBLE.
Hint
In the first case of the example, the best choice is 2 boards of length 23, 5 boards of length 51, and 99999997 boards of length 100. Buying 100000001 boards of length 100 gives a total greater than , which is not allowed.
In the second case, only even lengths can be made.