time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ayrat is looking for the perfect code. He decided to start his search from an infinite field tiled by hexagons. For convenience the coordinate system is introduced, take a look at the picture to see how the coordinates of hexagon are defined:
Ayrat is searching through the field. He started at point (0, 0) and is moving along the spiral (see second picture). Sometimes he forgets where he is now. Help Ayrat determine his location after n moves.
Input
The only line of the input contains integer n (0 ≤ n ≤ 1018) — the number of Ayrat’s moves.
Output
Print two integers x and y — current coordinates of Ayrat coordinates.
Examples
Input
3
Output
-2 0
Input
7
Output
3 2
【题解】
规律题;
可以看到,每一圈的边数是等差数列;公差为6;
且边的长度和圈数有着对应的关系(除了>形的地方);
对于所给的n;
用二分搞出这是第几圈;
(an=6+(n-1)6,sn=(a1+an)n/2;->sn=3*n(n+1));
然后总数减去3*(key-1)*(key-1+1)key为n所在的圈数;
减掉之后的步数可以再模拟一下具体在哪个位置,找下规律就好;
坐标和圈数和剩余的步数mod圈数有关
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long
using namespace std;
//const int MAXN = x;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
LL n;
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
int main()
{
//freopen("F:\rush.txt","r",stdin);
input_LL(n);
if (n==0)
{
puts("0 0");
return 0;
}
LL l = 0,r = 1e9,m = 0;
while (l <= r)
{
LL mid = (l+r)>>1;
if (1LL*3*mid*(mid+1)<=n)
{
m = mid;
l = mid+1;
}
else
r = mid-1;
}
n-=3*m*(m+1);
if (n==0)
{
printf("%I64d 0
",m*2);
return 0;
}
m++;
LL zc = n/m,yu = n % m;
//cout << zc << " "<<yu<<endl;
switch (zc)
{
case 0:
printf("%I64d %I64d
",2*m-yu,2*yu);
break;
case 1:
printf("%I64d %I64d
",m-2*yu,2*m);
break;
case 2:
printf("%I64d %I64d
",-m-yu,2*m-yu*2);
break;
case 3:
printf("%I64d %I64d
",-2*m+yu,-2*yu);
break;
case 4:
printf("%I64d %I64d
",-m+2*yu,-2*m);
break;
case 5:
printf("%I64d %I64d
",m+yu,-2*m+yu*2);
break;
case 6:
printf("%I64d %I64d
",2*m,0);
break;
}
return 0;
}