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: 

[Image] [Image] 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 ≤ 10^18) — 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

Source: codeforces | Difficulty: HARD
Tags: Mathematics

Sorting

Implementation


Solutions:
import math

def main(m):
	if m == 0:
		print('0 0')
	else:
		x = math.floor(1 / 6 * ((12 * m - 3) ** 0.5 + 3))
		while True:
			d = m - (x ** 3 - (x - 1) ** 3)
			if d < 0:
				x -= 1
			elif d > x * 6 + 6:
				x += 1
			else:
				break
		(s, r) = divmod(d, x)
		if s == 0:
			print('{} {}'.format(2 * x - r - 1, 2 * r + 2))
		elif s == 1:
			print('{} {}'.format(x - 2 * r - 2, 2 * x))
		elif s == 2:
			print('{} {}'.format(-x - r - 1, 2 * (x - r - 1)))
		elif s == 3:
			print('{} {}'.format(-2 * x + r + 1, -2 * r - 2))
		elif s == 4:
			print('{} {}'.format(-x + 2 * r + 2, -2 * x))
		elif s == 5:
			print('{} {}'.format(x + r + 1, -2 * x + 2 * r + 2))

def __starting_point():
	main(int(input()))
__starting_point()


3
import math

def solve(n):
	if n == 0:
		return (0, 0)
	k = int(0.5 * (-1 + math.sqrt(1 + 4 * n / 3.0))) + 10
	while 3 * k * (k + 1) >= n:
		k -= 1
	n -= 3 * k * (k + 1) + 1
	x = 1 + 2 * k
	y = 2
	lim = [k] + [k + 1] * 5
	dx = [-1, -2, -1, 1, 2, 1]
	dy = [2, 0, -2, -2, 0, 2]
	i = 0
	while n > 0:
		t = min(n, lim[i])
		x += t * dx[i]
		y += t * dy[i]
		n -= t
		i += 1
	return (x, y)
(x, y) = solve(int(input()))
print(x, y)


n = int(input())
(l, r) = (1, 10 ** 9)
(x, mid) = (0, 0)
while l <= r:
	mid = (l + r) // 2
	if 3 * mid * (mid - 1) <= n:
		l = mid + 1
		x = mid
	else:
		r = mid - 1
(a, b) = ((n - 3 * x * (x - 1)) // x, (n - 3 * x * (x - 1)) % x)
(q, w) = (0, 0)
if a == 0:
	(q, w) = (2 * x, 0)
	if b == 0:
		q = 2 * x - 2
	else:
		q += -b
		w += 2 * b
elif a == 1:
	(q, w) = (x - 2 * b, 2 * x)
elif a == 2:
	(q, w) = (-x - b, 2 * x - 2 * b)
elif a == 3:
	(q, w) = (-2 * x + b, -2 * b)
elif a == 4:
	(q, w) = (-x + 2 * b, -2 * x)
elif a == 5:
	(q, w) = (x + b, -2 * x + 2 * b)
print(q, w)


def layer(n):
	a = (-3 + (9 + 12 * n) ** 0.5) // 6
	a = int(a)
	a += 1
	while 3 * a * (a + 1) >= n:
		a -= 1
	return a + 1
n = int(input())
if n == 0:
	print(0, 0)
	quit()
l = layer(n)
base = 3 * (l - 1) * l + 1
a = [2 * l, 0]
b = [l, 2 * l]
bx = base + (l - 1)
c = [-l, 2 * l]
cx = bx + l
d = [-2 * l, 0]
dx = cx + l
e = [-l, -2 * l]
ex = dx + l
f = [l, -2 * l]
fx = ex + l
ax = fx + l
daa = abs(n - base + 1)
da = abs(n - ax)
db = abs(n - bx)
dc = abs(n - cx)
dd = abs(n - dx)
de = abs(n - ex)
df = abs(n - fx)
if n <= bx:
	print(int((db * a[0] + daa * b[0]) / (db + daa)), int((db * a[1] + daa * b[1]) / (db + daa)))
	quit()
if bx <= n <= cx:
	print(int((dc * b[0] + db * c[0]) / (db + dc)), b[1])
	quit()
if cx <= n <= dx:
	print(int((dd * c[0] + dc * d[0]) / (dc + dd)), int((dd * c[1] + dc * d[1]) / (dc + dd)))
	quit()
if dx <= n <= ex:
	print(int((de * d[0] + dd * e[0]) / (dd + de)), int((de * d[1] + dd * e[1]) / (dd + de)))
	quit()
if ex <= n <= fx:
	print(int((df * e[0] + de * f[0]) / (de + df)), e[1])
	quit()
if fx <= n <= ax:
	print(int((da * f[0] + df * a[0]) / (df + da)), int((da * f[1] + df * a[1]) / (df + da)))
	quit()


n = int(input())

def j(i):
	return 3 * i * (i + 1) <= n
high = 10 ** 18
low = 0
while high - low > 5:
	mid = high + low >> 1
	if j(mid):
		low = mid
	else:
		high = mid
while j(low + 1):
	low += 1
r = low
x = r << 1
y = 0
n -= 3 * r * (r + 1)
r += 1
if n:
	n -= 1
	x += 1
	y += 2
if n:
	sub = min(n, r - 1)
	n -= sub
	x -= sub
	y += sub << 1
if n:
	sub = min(n, r)
	n -= sub
	x -= sub << 1
if n:
	sub = min(n, r)
	n -= sub
	x -= sub
	y -= sub << 1
if n:
	sub = min(n, r)
	n -= sub
	x += sub
	y -= sub << 1
if n:
	sub = min(n, r)
	n -= sub
	x += sub << 1
if n:
	sub = min(n, r)
	n -= sub
	x += sub
	y += sub << 1
print(x, y)


def binsearch(p, l, r):
	while l + 1 != r:
		m = (l + r) // 2
		if p(m):
			r = m
		else:
			l = m
	return r
n = int(input())
if n == 0:
	print(0, 0)
else:
	i = binsearch(lambda i: n <= 3 * i * (i + 1), 0, 10 ** 18)
	acc = 3 * (i - 1) * i
	j = binsearch(lambda j: n <= acc + i * (j + 1), -1, 6)
	k = n - acc - i * j - 1
	dy = [0, 2, 2, 0, -2, -2]
	dx = [2, 1, -1, -2, -1, 1]
	y = dy[(j + 1) % 6] + dy[j] * (i - 1) + dy[(j + 2) % 6] * k
	x = dx[(j + 1) % 6] + dx[j] * (i - 1) + dx[(j + 2) % 6] * k
	print(x, y)


from math import sqrt, ceil
from collections import namedtuple

def add(a, b):
	return (a[0] + b[0], a[1] + b[1])

def count(p):
	return p * (3 * p + 2)

def bin_search(n):
	l = 0
	r = ceil(sqrt(n))
	while r - l > 1:
		m = (l + r) // 2
		if count(m) > n:
			r = m - 1
		else:
			l = m
	if count(r) > n:
		return l
	else:
		return r

def get_pos(n, p):
	if n < p:
		return add((p - 1, -2 * p + 2), (n, 2 * n))
	n -= p
	if n < p - 1:
		return add((1 + 2 * (p - 1), 2), (-n, 2 * n))
	n -= p - 1
	if n < p:
		return add((p, 2 * p), (-2 * n, 0))
	n -= p
	if n < p:
		return add((-p, 2 * p), (-n, -2 * n))
	n -= p
	if n < p:
		return add((-2 * p, 0), (n, -2 * n))
	n -= p
	if n < p:
		return add((-p, -2 * p), (2 * n, 0))
	raise RuntimeError("You're a big guy")
n = int(input())
if n == 0:
	print(0, 0)
else:
	p = bin_search(n)
	start = count(p)
	n -= start
	ans = get_pos(n, p + 1)
	print(ans[0], ans[1])


def sLayer(n):
	return 3 * n * (n + 1)

def getLayer(N):
	a = 0
	b = 600000000
	while b - a > 1:
		n = (a + b) // 2
		tN = sLayer(n)
		if tN > N:
			b = n
		else:
			a = n
	return a
N = int(input())
if N == 0:
	print('0 0')
	raise SystemExit
N -= 1
layer = getLayer(N)
N -= sLayer(layer)
seg = N // (layer + 1)
idx = N % (layer + 1)
segDiff = [(-1, 2), (-2, 0), (-1, -2), (1, -2), (2, 0), (1, 2)]
if seg == 0:
	x = 2 * layer + 1
	y = 2
elif seg == 1:
	x = -1 + layer
	y = 2 * (layer + 1)
elif seg == 2:
	x = -2 - layer
	y = 2 * layer
elif seg == 3:
	x = -1 - 2 * layer
	y = -2
elif seg == 4:
	x = 1 - layer
	y = -2 - 2 * layer
elif seg == 5:
	x = 2 + layer
	y = -2 * layer
x += segDiff[seg][0] * idx
y += segDiff[seg][1] * idx
print('%d %d' % (x, y))


def main():
	n = int(input())
	(x, y) = solver(n)
	print(x, y)

def solver(n):
	rounds = int(quadraticEqPlus(3, 3, -n))
	n -= 3 * rounds * (rounds + 1)
	curPoint = (2 * rounds, 0)
	curRound = rounds + 1
	circle = [(goUpRight, 1), (goUpLeft, curRound - 1), (goLeft, curRound), (goDownLeft, curRound), (goDownRight, curRound), (goRight, curRound), (goUpRight, curRound)]
	for (func, steps) in circle:
		if n >= steps:
			curPoint = func(curPoint, steps)
			n -= steps
		else:
			curPoint = func(curPoint, n)
			n = 0
			return curPoint
	assert False

def quadraticEqPlus(a, b, c):
	return (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)

def goUpLeft(point, steps):
	return (point[0] - steps, point[1] + 2 * steps)

def goLeft(point, steps):
	return (point[0] - 2 * steps, point[1])

def goDownLeft(point, steps):
	return (point[0] - steps, point[1] - 2 * steps)

def goDownRight(point, steps):
	return (point[0] + steps, point[1] - 2 * steps)

def goRight(point, steps):
	return (point[0] + 2 * steps, point[1])

def goUpRight(point, steps):
	return (point[0] + steps, point[1] + 2 * steps)
main()


import math

def printa(x, y):
	print(2 * x + y, ' ', 2 * y)
n = int(input())
a = math.floor((-3 + math.sqrt(9 + 12 * n)) / 6)
while 3 * a * (a + 1) > n:
	a -= 1
while 3 * (a + 1) * (a + 2) <= n:
	a += 1
b = n - 3 * a * (a + 1)
if b == 0 or b == 1:
	printa(a, b)
	quit()
b -= 1
if b >= 0 and b <= a:
	printa(a - b, b + 1)
	quit()
b -= a
if b >= 0 and b <= a + 1:
	printa(-b, a + 1)
	quit()
b -= a + 1
if b >= 0 and b <= a + 1:
	printa(-a - 1, a + 1 - b)
	quit()
b -= a + 1
if b >= 0 and b <= a + 1:
	printa(-a - 1 + b, -b)
	quit()
b -= a + 1
if b >= 0 and b <= a + 1:
	printa(b, -a - 1)
	quit()
b -= a + 1
printa(a + 1, -a - 1 + b)


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
SIDES = [lambda c, v: (1 + 2 * c - v, 2 + 2 * v), lambda c, v: (-1 + c - 2 * v, 2 + 2 * c), lambda c, v: (-c - 2 - v, 2 * c - 2 * v), lambda c, v: (-1 - 2 * c + v, -2 - 2 * v), lambda c, v: (1 - c + 2 * v, -2 - 2 * c), lambda c, v: (2 + c + v, -2 * c + 2 * v)]
n = rint()
if n == 0:
	print(0, 0)
	return
n -= 1
(l, h) = (0, 10 ** 9)
while h - l > 1:
	m = (h + l) // 2
	if 3 * m * (m + 1) > n:
		h = m - 1
	else:
		l = m
c = h if 3 * h * (h + 1) <= n else l
n -= 3 * c * (c + 1)
print(*SIDES[n // (c + 1)](c, n % (c + 1)))


from functools import reduce
from decimal import *
import math
import itertools

class pair(list):

	def __init__(self, a, b):
		list.__init__(self, (a, b))

	def __add__(self, q):
		return pair(self[0] + q[0], self[1] + q[1])

	def __sub__(self, q):
		return pair(self[0] - q[0], self[1] - q[1])

	def __mul__(self, k):
		return pair(k * self[0], k * self[1])

	def __neg__(self):
		return pair(-self[0], -self[1])
setcontext(ExtendedContext)
getcontext().prec = 100
(a, b, c) = (pair(1, 2), pair(-2, 0), pair(-1, 2))
n = int(input())
k = int(((Decimal(3 + 4 * n) / Decimal(3)).sqrt() - Decimal(1)) // 2)
n1 = n - 3 * k * (k + 1)
(x, y) = (n1 // (k + 1), n1 % (k + 1))
L = [c, b, -a, -c, -b, a]
M = [pair(0, 0)] + list(itertools.accumulate(L))
if n1 == 0:
	ans = pair(2, 0) * k
else:
	ans = pair(2, 0) * (k + 1) + M[x] * (k + 1) + L[x] * y
print(ans[0], ans[1])


def f(n):
	(left, right) = (-1, n + 1)
	while right - left > 1:
		mid = (left + right) // 2
		x = 6 * mid * (mid + 1) // 2 + 5 * (mid + 1)
		if x > n:
			right = mid
		else:
			left = mid
	if left >= 0:
		mid = left
		x = 6 * mid * (mid + 1) // 2 + 5 * (mid + 1)
		n -= x
	return (n, left + 1)

def main():
	n = int(input())
	(n, k) = f(n)
	x = k
	y = -2 * k
	d = [k + 1] * 6
	d[1] -= 1
	dx = [1, -1, -2, -1, 1, 2]
	dy = [2, 2, 0, -2, -2, 0]
	for i in range(6):
		x += min(d[i], n) * dx[i]
		y += min(d[i], n) * dy[i]
		n = max(0, n - d[i])
	print(x, y)
main()


ru = (1, 2)
r = (2, 0)
rd = (1, -2)
ld = (-1, -2)
l = (-2, 0)
lu = (-1, 2)
(x, y) = (0, 0)
n = int(input())
l = -1
r = int(1e+18)
while r - l > 1:
	m = (r + l) // 2
	if 5 * m + 3 * m * (m - 1) > n:
		r = m
	else:
		l = m
x += l * 1
y += l * -2
n -= 5 * l + 3 * l * (l - 1)
if n < r:
	x += n * 1
	y += n * 2
	n = 0
else:
	n -= r
	x += r * 1
	y += r * 2
if n < r - 1:
	x += n * -1
	y += n * 2
	n = 0
else:
	n -= l
	x += l * -1
	y += l * 2
if n < r:
	x += -2 * n
	n = 0
else:
	n -= r
	x += -2 * r
if n < r:
	x += -1 * n
	y += -2 * n
	n = 0
else:
	n -= r
	x += -1 * r
	y += -2 * r
if n < r:
	x += 1 * n
	y += -2 * n
	n = 0
else:
	n -= r
	x += 1 * r
	y += -2 * r
if n < r:
	x += 2 * n
print(x, y)


from math import sqrt

def hex(l):
	return 1 + 3 * l * (l + 1)

def level(n):
	if n == 0:
		return (0, 0)
	l = int((-3.0 + sqrt(9.0 + 12.0 * (n - 1))) / 6.0)
	while hex(l) > n:
		l -= 1
	while hex(l + 1) <= n:
		l += 1
	return (l + 1, n - hex(l))

def coordinates(l, k):
	if l == 0:
		return (0, 0)
	(s, i) = divmod(k, l)
	if s == 0:
		return (2 * l - (i + 1), 2 * (i + 1))
	elif s == 1:
		return (l - 2 * (i + 1), 2 * l)
	elif s == 2:
		return (-l - (i + 1), 2 * l - 2 * (i + 1))
	elif s == 3:
		return (-2 * l + (i + 1), -2 * (i + 1))
	elif s == 4:
		return (-l + 2 * (i + 1), -2 * l)
	elif s == 5:
		return (l + (i + 1), -2 * l + 2 * (i + 1))

def ayrat(n):
	(l, k) = level(n)
	return coordinates(l, k)
n = int(input())
print('{} {}'.format(*ayrat(n)))