Under construction

Still being built. Things may change

Cut Tiles (Large)

Time limit5sMemory limit512 MB

Summary
Pack square tiles with power-of-two sides into the fewest MxM tiles using only side-parallel cuts.
Level

Medium6 of 10

Topics
Greedy, Sorting, Bit manipulation
Solved
No attempts yet

Problem

Enzo is renovating the house he just bought. The hardest part is buying exactly the right number of tiles. He needs NN square tiles whose side lengths are 2S1,2S2,…,2SN2^{S_1}, 2^{S_2}, \dots, 2^{S_N} in that order. The same size may appear more than once.

The store sells one product only, a square tile of side MM. Every tile Enzo needs has to be cut out of a tile he bought, and to keep the work simple he only makes cuts parallel to the sides of the tile he is cutting. Each required tile comes out of a single purchased tile in one piece, and cut pieces cannot be joined together.

Find the smallest number of M×MM \times M tiles Enzo has to buy.

Input

The first line contains the number of test cases TT. Each of the next TT lines holds one test case. A line starts with the number of required tiles NN and the side length MM of the tiles on sale. Then come the exponents S1,S2,…,SNS_1, S_2, \dots, S_N that give the sizes of the required tiles.

Limits

  • 1≤T≤10001 \le T \le 1000
  • 1≤N≤5001 \le N \le 500
  • 1≤2Sk≤M≤231−11 \le 2^{S_k} \le M \le 2^{31} - 1

Output

For each test case, print one line of the form Case #x: y, where xx is the test case number starting from 1 and yy is the smallest number of M×MM \times M tiles Enzo has to buy.

Examples1

  1. Example 1

    Input
    4
    1 6 2
    2 6 2 2
    3 6 2 1 1
    7 277 3 8 2 6 1 3 6
    
    Expected output
    Case #1: 1
    Case #2: 2
    Case #3: 1
    Case #4: 2