Problem Statement
You have a rectangular piece of paper. Its dimensions are W times H. You want to have a paper with area A instead. Therefore, you decided to fold the paper you have. In each step you can fold the paper according to a straight line. There are two restrictions: First, that line must always be parallel to one of the rectangle's sides. Second, after each fold both dimensions of the new rectangle must be integers again.
For example, suppose that your paper is 5 units wide and 3 units tall. If you fold it according to a vertical line that is 4 units to the right of its left side, you will obtain a rectangle that is 4 units wide and 3 units tall. If you fold it according to a horizontal line that is 1 unit below the top of the rectangle, you will get a rectangle that is 5 units wide and 2 units tall.
You are given the
Definition
- Class:
- FoldingPaper2
- Method:
- solve
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int solve(int W, int H, int A)
- (be sure your method is public)
Constraints
- H, W will be between 1 and 1,000,000,000, inclusive.
- A will be between 1 and 100,000, inclusive.
Examples
5
3
12
Returns: 1
The solution in this case is the first example mentioned above.
2
2
3
Returns: -1
A 2x2 square cannot be folded into a rectangle with area 3. Note that a rectangle that is 1.5 units wide and 2 units tall is not a solution: both dimensions of all rectangles you produce must be integers.
4
4
1
Returns: 4
127
129
72
Returns: 8
1
100000
100000
Returns: 0
The paper already has the desired area, so no folding is necessary.
1
1
2
Returns: -1
1000000000
1000000000
100000
Returns: 44
1000000000
1000000000
1
Returns: 60
10000
10000
9999
Returns: 14
101
103
10403
Returns: 0
103
101
10403
Returns: 0
105
100
10403
Returns: -1
103
201
10403
Returns: 1
207
202
10403
Returns: 3
207
206
10403
Returns: 3
206
207
10403
Returns: 3
6465
103
10403
Returns: 7
9999
9999
10000
Returns: 14
84
286
3003
Returns: 3
8
8
35
Returns: 2
7
7
35
Returns: 1
6
6
35
Returns: -1
16
16
128
Returns: 1
8
8
128
Returns: -1
1
100000
99999
Returns: 1
3
100000
99999
Returns: 2
100000
3
99999
Returns: 2
4
74
222
Returns: 1
4
75
222
Returns: 2
4
149
222
Returns: 2
4
110
222
Returns: 2
4
111
222
Returns: 1
3
148
222
Returns: 1
3
147
222
Returns: 1
3
149
222
Returns: 2
234624
35235
108
Returns: 27
657356
54542
108
Returns: 29
30
29
225
Returns: 2
730
12
729
Returns: 4
100000
20056
208
Returns: 24
75
279
225
Returns: 7
775
279
225
Returns: 10
1
20
5
Returns: 2
1
11
5
Returns: 2
1
21
5
Returns: 3
9
3
12
Returns: 2
1000000000
1000000000
12
Returns: 57
1000000000
3
100000
Returns: 16
1000000000
1000000000
3
Returns: 59
99
999
23
Returns: 13
10
10
6
Returns: 5
15
39
133
Returns: 4
1000000000
1000000000
99999
Returns: 44
1247
12
13299
Returns: 2
7
7
9
Returns: 4
864682
824
100000
Returns: 14
16
5
20
Returns: 2
999999999
999999999
55566
Returns: 45
245344672
45632342
56322
Returns: 38
50
20000
5678
Returns: 8
7
7
36
Returns: 2
2
10000000
2
Returns: 24
4
4
2
Returns: 3
1
1
8
Returns: -1
1000000000
99935080
4096
Returns: 45
99
195
35
Returns: 10
3
16
12
Returns: 2
100000
1
100000
Returns: 0
1000000
1000000
1
Returns: 40
100000
10000000
1
Returns: 41
3
8
16
Returns: 1
3
16
6
Returns: 3
1000000000
747172737
1
Returns: 60
8
8
49
Returns: 2
156
95
35
Returns: 9
1232143
99643
23
Returns: 33
536870912
536870912
2
Returns: 57