AcWing 1237. 螺旋折线
原题链接
中等
我是推四个角推出的公式
(n > 0)
(n,n)4*n*n;
(-n,n) 4*n*n - 2*n;
(-n,-n + 1) (2*n - 1)*(2 * n - 1);
(n,-n)(4*n*n + 2*n);
推出(根据四个角以及距离四个角的水平竖直距离)
右边直线:4*x*x + x - y;
上面直线:4*y*y + x - y;
左边直线:4*n*n - 2*n -(2*n - 1-(y -1- x)) => 4x*x + 2*x +2*x + 1 + x - y;
=> (2*x + 1)*(2*x + 1) + x - y- 1;
下面直线:(2*y - 1)*(2*y - 1)+ y - 1 -x;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define x first
#define y second
using namespace std;
int main()
{
LL x,y;
scanf("%lld%lld",&x,&y);
LL res = 0;
if(abs(y) <= x)
res = 4*x*x + x - y;
else if(abs(x) <= y)
res = 4*y*y + x - y;
else if(x <= 0 && y >= x)
res = (2*x + 1)*(2*x + 1) + y - 1 - x;
else
res = (2*y - 1)*(2*y - 1) + y - 1 - x;
cout<<res<<endl;
return 0;
}
\\唉惭愧的是公式对了,边界找错了,借鉴了下Y总的代码;