Problems > Number Spiral

Number Spiral

Difficulty: 1600 2000 ms 128 MB

math

An infinite grid is numbered along L-shaped rings: ring k = max(row, col) holds the values (k−1)²+1 through k². The first rows begin: row 1 = 1 2 9 10 25 ...; row 2 = 4 3 8 11 24 ...; row 3 = 5 6 7 12 23 ... Print the value at row R, column C.

Input: one line with two integers R and C.

Output: the value at (R, C).

Constraints:

  • 1 ≤ R, C ≤ 109

Sample Tests

Input 1
2 3
Output 1
8
Input 2
4 2
Output 2
15

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.