Rectangle Division
InterviewTime limit1sMemory limit1024 MB
Given a board of size a by b, k cuts, and m rectangles, find the horizontal and vertical cut counts h and v, preferring fewer horizontal cuts, or print -1.
- Level
Medium5 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
Anya is playing a new board game, "Checkered Kingdom".
Consider a rectangular grid board of size .
The board must be divided into rectangles using vertical or horizontal cuts. The rectangles do not have to be equal in size. Exactly cuts must be made in total.
Each cut is a straight line from one edge of the board to the opposite edge. Cuts can only be made along grid lines, the borders between cells.
Print how many horizontal cuts () and how many vertical cuts () are made. If the board can be cut in several ways, print the one with fewer horizontal cuts. If the board cannot be cut as required, print -1.
Input
The first line contains a single integer , the number of tests (). Each of the next lines contains four integers separated by spaces: , , , , the height and width of the board, the number of cuts, and the number of rectangles (, , , ).
Output
For each test, print two integers and separated by a space: the number of horizontal cuts and the number of vertical cuts, if the board can be cut as required. Otherwise, print -1.
Hint
The example has three tests.
- In the first test, the board can be cut as shown in the figure:

Illustration for the first test: , , , .
-
In the second test, the board cannot be cut as required.
-
In the third test, the board can be cut as shown in the figure:

Illustration for the third test: , , , .