-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number Spiral.cpp
46 lines (43 loc) · 897 Bytes
/
Number Spiral.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long int t;
cin >> t;
long long int y, x;
while (t--)
{
cin >> y >> x;
if (x > y)
{
long long int X = 0;
if (x % 2 == 1)
{
X = x * x;
cout << X - (y - 1) << endl;
}
else
{
X = (x - 1) * (x - 1) + 1;
cout << X + (y - 1) << endl;
}
}
else
{
long long int Y = 0;
if (y % 2 == 0)
{
Y = y * y;
cout << Y - (x - 1) << endl;
}
else
{
Y = (y - 1) * (y - 1) + 1;
cout << Y + (x - 1) << endl;
}
}
}
return 0;
}