1224. Spiral
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
![Problem illustration](https://i-blog.csdnimg.cn/blog_migrate/5eb35126240c52439c830f3620d2720a.png)
A brand new sapper robot is able to neutralize mines in a rectangular region having integer height and width (
N and
Mrespectively). Before the robot begins its work it is placed near the top leftmost cell of the rectangle heading right. Then the robot starts moving and neutralizing mines making a clockwise spiral way (see picture). The spiral twists towards the inside of the region, covering all the cells. The region is considered safe when all the cells are visited and checked by the robot.
Your task is to determine the number of the turns the robot has to make during its work.
Input
The input contains two integers in the following order:
N,
M
(1 ≤
N,
M ≤ 2
31 − 1)
.
Output
The output consists of a single integer value — the number of the turns.
Sample
input | output |
---|---|
3 5 | 4 |
Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002
class InputReader():
def nextInt(self):
return int(input().strip())
def nextLine(self):
return input()
def nextString(self):
return input().strip()
def nextInts(self):
ints = []
str = input().strip().split()
for s in str:
if (s is None) or (len(s) == 0):
continue
ints.append(int(s))
return ints
def nextFloats(self):
floats = []
str = input().strip().split()
for s in str:
if (s is None) or (len(s) == 0):
continue
floats.append(float(s))
return floats
def gao(n , m):
if n == 0 or m == 0:
return -1
if n == 1 :
return 0
if m == 1 :
return 1
if n == 2 :
return 2
if m == 2:
return 3
if n == 3:
return 4
if m == 3:
return 5
k = int(min(n , m)/2)-1
return 4 * k + gao(n - 2*k , m - 2*k)
if __name__ == '__main__':
reader = InputReader()
nums = reader.nextInts()
n = nums[0]
m = nums[1]
print(gao(n , m))