Stealing Chocolate
Time limit1sMemory limit512 MB
Starting from an N by M rectangle, repeatedly remove one cell and rearrange the rest into a rectangle whose width-height difference stays within K of the original difference; count the maximum removals, keeping at least one cell.
- Level
Medium7 of 10
- Topics
- Number theory, Math, Brute force, Greedy
- Solved
- No attempts yet
Problem
Ingyu loves chocolate, and he plans to steal Yeonjin's chocolate one piece at a time. Yeonjin has a chocolate of size . Ingyu will steal one piece from it, leaving pieces of chocolate. Stealing a piece changes the shape, so it would reveal that he stole it. Therefore Ingyu will rearrange the remaining chocolate into a rectangle.
The thing to watch is that the difference between the width and height of the new chocolate, , must not differ much from the difference between the width and height of the original chocolate, , or it will be obvious that he stole a piece. So Ingyu will keep at most and eat as many pieces as possible.
Consider the figure below as an example.

When , there are pieces of chocolate. The original chocolate has equal width and height, so is .
If Ingyu eats one piece and rearranges the rest into a shape, then , so he can eat one piece.
If he eats one more piece here, he can rearrange the rest into a shape. Then , so he can eat one more piece.
If he eats one more piece here, the only possible rearrangement is a shape. Then , which does not satisfy the rule, so he cannot eat a third piece.
Here is another example.

When , there are pieces of chocolate. From the original chocolate, is .
If Ingyu eats one piece, he must arrange pieces into a rectangle, and the smallest is , which is greater than , so Ingyu cannot eat any piece. If Ingyu eats pieces and then arranges the rest into a shape, is , which is less than , but since he can eat only one piece at a time, he cannot reach that state.
Given , find the number of Yeonjin's chocolates that Ingyu can secretly eat.
However, if Ingyu eats all the chocolate, Yeonjin will notice, so at least piece of chocolate must remain.
Input
The first line contains three integers separated by spaces. (, )
Output
Print the number of chocolates that Ingyu can secretly eat.