螺旋折线(2018年第九届蓝桥杯第七题)
如图p1.png所示的螺旋折线经过平面上所有整点恰好一次。
对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。
例如dis(0, 1)=3, dis(-2, -1)=9
给出整点坐标(X, Y),你能计算出dis(X, Y)吗?
【输入格式】
X和Y
对于40%的数据,-1000 <= X, Y <= 1000
对于70%的数据,-100000 <= X, Y <= 100000
对于100%的数据, -1000000000 <= X, Y <= 1000000000
【输出格式】
输出dis(X, Y)
【样例输入】
0 1
【样例输出】
3
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[])
{
long long int x,y,sum;
cin>>x>>y;
if(x>0)
sum=4*x*x+x-y;
if(x<0)
sum=4*x*x+x*3+y;
if(x==0&&y>0)
sum=4*y*y-y;
if(x==0&&y<0)
sum=4*y*y-3*y;
if(x==0&&y==0)
sum=0;
cout<<sum;
return 0;
}