Folding a Rectangle
Time limit2sMemory limit256 MB
Each fold reduces one side to max(x, L-x). Find the fewest folds to turn sides W, H into w, h, in any orientation, or report -1.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
You have a rectangular sheet of paper whose sides are and . You want to fold it several times until it becomes a rectangle whose sides are and .
Every fold line is parallel to a side of the rectangle, and the shape after a fold is again a rectangle. Folding a side of length at distance from one of its ends () leaves that side with the longer of the two pieces, , and the other side keeps its length.
The finished rectangle may be turned around. Any orientation whose sides are and counts.
Write a program that computes the minimum number of folds.
Input
The first line contains the two side lengths and of the starting rectangle. The second line contains the two side lengths and of the target rectangle. ()
Output
Print the minimum number of folds that turns the starting rectangle into the target rectangle. Print -1 when it is impossible.