Rectangle Tiling

시간 제한1초메모리 제한1024 MB

요약
주어진 2의 거듭제곱 정사각형들로 W 곱하기 H 직사각형을 덮을 때 필요한 최소 개수를 구하거나, 불가능하면 -1을 출력한다.
난이도

보통10점 중 7점

유형
그리디, 분할 정복, 수학, 비트 연산
정답자
아직 제출이 없습니다

문제

Consider a rectangle with integer side lengths. A square tiling of the rectangle is a covering of the entire region using non-overlapping squares whose sides are parallel with those of the rectangle. In a square tiling, no square may overhang (extend beyond the rectangle's boundary).

You have a collection of squares with side lengths being powers of 22. Find a square tiling of the rectangle using the fewest squares possible, or, indicate that it cannot be done.

Figure 1: Optimal square tilings for the first three sample inputs. The small unlabelled tiles are 1×11 \times 1 tiles.

입력

The first line of input contains three integers W,HW, H and NN (1≤W,H≤2501 \leq W,H \leq 2^{50} and 1≤N≤511 \leq N \leq 51). Here, WW and HH indicate the dimensions of the rectangle. The next line contains NN integers a_0,a_1,…,a_N−1a\_0, a\_1, \ldots, a\_{N-1} where a_ia\_i (0≤a_i≤2510 \leq a\_i \leq 2^{51}) is the number of 2i×2i2^i \times 2^i squares you own.

출력

If there is a square tiling of a W×HW \times H rectangle using the squares you own, output the minimum number of squares needed in such a square tiling. Otherwise, output −1-1 if there is no square tiling of the rectangle using the squares you own.

예제4

  1. 예제 1

    입력
    4 2 2
    5 1
    
    예상 출력
    5
    
  2. 예제 2

    입력
    6 7 3
    10 10 10
    
    예상 출력
    12
    
  3. 예제 3

    입력
    17 20 5
    20 0 4 0 1
    
    예상 출력
    25
    
  4. 예제 4

    입력
    4 2 3
    3 1 10
    
    예상 출력
    -1