number_spiral.cpp (439B)
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 void solve() 6 { 7 long y, x, res = 1; 8 cin >> y >> x; 9 if (y == x) { 10 res = x * (x - 1) + 1; 11 } else if (y > x) { 12 res = 2*(y/2) * 2*(y/2) + y%2; 13 int dir = y%2 ? 1 : -1; 14 res += (x-1) * dir; 15 } else { 16 res = (2*((x-1)/2)+1) * (2*((x-1)/2)+1) + (x+1)%2; 17 int dir = x%2 ? -1 : 1; 18 res += (y-1) * dir; 19 } 20 cout << res << "\n"; 21 } 22 23 int main() 24 { 25 int t; 26 cin >> t; 27 while (t--) 28 solve(); 29 }