如下图所示的螺旋折线经过平面上所有整点恰好一次。
对于整点 (X,Y),我们定义它到原点的距离 dis(X,Y) 是从原点到 (X,Y) 的螺旋折线段的长度。
例如 dis(0,1)=3,dis(−2,−1)=9
给出整点坐标 (X,Y),你能计算出 dis(X,Y) 吗?
输入格式
包含两个整数 X,Y。
输出格式
输出一个整数,表示 dis(X,Y)。
数据范围
−109≤X,Y≤109
输入样例:
0 1
输出样例:
3
题解分析:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
int x,y;
cin>>x>>y;
int n;
LL res;
if(abs(x)<=y){
n = y;
res = (LL)(2*n-1)*(2*n) + x + n;
}
else if(abs(y)<=x){
n = x;
res = (LL)(2*n)*(2*n) + n - y;
}
else if(abs(x)<=abs(y)+1 && y < 0){
n = abs(y);
res = (LL)(2*n)*(2*n+1) + n - x;
}
else if(abs(y)<=abs(x)){
n = abs(x);
res = (LL)(2*n-1)*(2*n-1) + n-1 + y;
}
cout<<res<<endl;
return 0;
}