Paper Cutting
Time limit1sMemory limit128 MB
For each test case, decide whether an A by B grid of C by D cards fits on an E by F sheet in some rotation, then report the minimum number of straight cuts needed to separate the cards.
- Level
Hard8 of 10
- Topics
- Math, Greedy, Geometry, Implementation
- Solved
- No attempts yet
Problem
A print shop needs business cards. After the cards are printed on one large sheet of paper, they are separated with a special cutting machine. Because operating the machine is expensive, the number of cuts must be as small as possible. Your task is to find the optimal way to produce the cards.
Several rules apply. The cards are always printed as a grid of exactly cards. The grid size (the number of cards in one row and in one column) is fixed and cannot be changed. The sheet is rectangular and its size is fixed as well. The grid must be aligned with the edges of the sheet; it may be rotated only by 90 degrees. You may swap the roles of rows and columns and place the grid anywhere on the sheet, and the cards may even touch the edges of the paper.
For example, suppose a card is cm and the grid is cards. The four possible orientations of the grid are shown below, each labelled with the smallest sheet that can hold it.

The cutting machine makes one straight cut of arbitrary length at a time. Each cut must run all the way through the piece of paper; it cannot stop in the middle. Only one loose piece of paper may be cut at a time — you may not stack pieces on top of one another, nor place them side by side, to save cuts.
Input
The input consists of several test cases. Each test case is given on one line as six positive integers , , , , , separated by spaces:
- and are the dimensions of the card grid, with ;
- and are the dimensions of a single card in centimeters, with ;
- and are the dimensions of the paper sheet in centimeters, with .
The input ends with a line containing six zeros, which is not processed.
Output
For each test case, print a single line. If the grid of cards fits on the sheet, print
The minimum number of cuts is X.
where X is the minimum number of cuts required. Otherwise print
The paper is too small.