#CCBOTPBA0000055. Xoắn ốc số (Number Spiral)
Xoắn ốc số (Number Spiral)
Number Spiral
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
A number spiral is an infinite square grid filled with consecutive positive integers. Row 1, column 1 contains 1. The first five rows and columns are:
1 2 9 10 25
4 3 8 11 24
5 6 7 12 23
16 15 14 13 22
17 18 19 20 21
Given a 1-based row and column , determine the number in that cell. The spiral continues following the same pattern for all valid coordinates.
Input
One line contains two integers and , the requested row and column.
Output
Print exactly one integer: the value in row , column .
Subtasks
- Subtask 1 (20%): .
- Subtask 2 (30%): .
- Subtask 3 (50%): .
Examples
Example 1
Input:
2 3
Output:
8
Explanation: The value in row 2, column 3 of the displayed grid is 8.
Example 2
Input:
1 1
Output:
1
Explanation: The upper-left cell is the start of the spiral and contains 1.
Example 3
Input:
4 2
Output:
15
Explanation: The fourth row begins 16, 15, 14, 13, so column 2 has value 15.