A number spiral is an infinite grid whose upper-left square has number 1. Here are the first five layers of the spiral:
Your task is to find out the number in row y and column x.
Input
The first input line contains an integer t: the number of tests.
After this, there are t lines, each containing integers y and x.
Output
For each test, print the number in row y and column x.
Constraints
1≤t≤10^5
1≤y,x≤10^9
Sample
Input | Output |
3 2 3 1 1 4 2 | 8 1 15 |
e,让我翻译一下:
把所有整数排成一个如图的蛇形方阵
多测。输入x,y,输出表格中第x行第y列的数字。
方法:把他的螺旋转为同一方向
代码奉上
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
ll x,y;
cin>>x>>y;
ll l=max(x,y);
if(l%2==0)swap(x,y);
if(y==l)cout<<l*l-x+1<<endl;
else cout<<(l-1)*(l-1)+y<<endl;
}
return 0;
}