[蓝桥杯 2018 省 B] 螺旋折线
题目描述
如图所示的螺旋折线经过平面上所有整点恰好一次。
对于整点 ( X , Y ) (X, Y) (X,Y),我们定义它到原点的距离 dis ( X , Y ) \text{dis}(X, Y) dis(X,Y) 是从原点到 ( X , Y ) (X, Y) (X,Y) 的螺旋折线段的长度。
例如 dis ( 0 , 1 ) = 3 \text{dis}(0, 1)=3 dis(0,1)=3, dis ( − 2 , − 1 ) = 9 \text{dis}(-2, -1)=9 dis(−2,−1)=9。
给出整点坐标 ( X , Y ) (X, Y) (X,Y),你能计算出 dis ( X , Y ) \text{dis}(X, Y) dis(X,Y) 吗?
输入格式
X X X 和 Y Y Y。
输出格式
输出 dis ( X , Y ) \text{dis}(X, Y) dis(X,Y)
样例 #1
样例输入 #1
0 1
样例输出 #1
3
提示
对于 40 % 40\% 40%的数据, − 1000 ≤ X , Y ≤ 1000 -1000\le X,Y\le 1000 −1000≤X,Y≤1000。
对于 70 % 70\% 70% 的数据, − 1 0 5 ≤ X , Y ≤ 1 0 5 -10^5\le X,Y \le 10^5 −105≤X,Y≤105。
对于 100 % 100\% 100% 的数据, − 1 0 9 ≤ X , Y ≤ 1 0 9 -10^9\le X,Y \le 10^9 −109≤X,Y≤109。
解析
将每个正方形的左侧中间横线,顺时针90度补成一个正方形,然后分四个象限讨论即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define inf 1e18
const int mod=1e9+7;
const int N=2e5+5;
int x,y;
void solve(){
cin>>x>>y;
int t=max(abs(x),abs(y));
int ans=t*(t-1)/2*8;
if(x>=0){
if(y>=0) ans+=3*t+x+(t-y);
else ans+=5*t-y+(t-x);
}
else{
if(y>=0) ans+=t+y+(t+x);
else{
if(x<=y) ans+=(t+y);
else ans+=t*7+(2+x);
}
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt=1;
// cin>>tt;
while(tt--) solve();
return 0;
}