Cutting Chocolate
InterviewTime limit2sMemory limit128 MB
Compute the minimum number of straight cuts needed to divide an N by M chocolate bar into all 1x1 pieces.
- Level
Easy1 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Junghwa has one chocolate bar. The bar has grooves running horizontally and vertically, so if every groove is cut, it can be split into pieces of size .
She wants to split the chocolate into pieces to share it with her friends. In one cut, she chooses one current chocolate piece and cuts it along one of its grooves. That piece then becomes two pieces.
Because the chocolate may melt while being cut, Junghwa wants to minimize the number of cuts. Given and , find the minimum number of cuts needed to make every piece have size .
Input
The first line contains two integers and .
Output
Print the minimum number of cuts needed to split the whole chocolate bar into pieces.