Read problem statements in [Russian], [Mandarin Chinese], [Bengali], and [Vietnamese] as well.

An integer x is said to be a Perfect Power if there exists positive integers a and b (i.e a, and b should be ≥ 1) such that x = a^{b+1}.

Given an integer n, find the closest Perfect Power to n. If there are multiple *Perfect Powers* equidistant from n and as close as possible to n, find the smallest one.

More formally, find the smallest integer x such that x is a Perfect Power and |n - x| ≤ |n - y| over all Perfect Powers y.

------ Input Format ------ 

- The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains a single integer n.

------ Output Format ------ 

For each test case, print in a separate line, the smallest x such that x is a perfect power and |n - x| is minimum.

------ Constraints ------ 

$1 ≤ T ≤ 10^{5}$
$1 ≤ n ≤ 10^{18}$

----- Sample Input 1 ------ 
7
7
10
26
242
129
394857629456789876
353872815358409997
----- Sample Output 1 ------ 
8
9
25
243
128
394857628993920400
353872815358410000
----- explanation 1 ------ 
Test case $1$: $n = 7$, $8 = 2^{3}$ is a perfect power, but $6$ is not, so the minimum difference obtainable is $1$ when $x = 8$.

Test case $2$: $n = 10$, $9 = 3^{2}$ is a perfect power, but $11$ is not, so the minimum difference obtainable is $1$ when $x = 9$.

Test case $3$: $n = 26$, $25 = 5^{2}$ is a perfect power, $27 = 3^{3}$ is also a perfect power. Both $x = 25$ and $x = 27$ give the minimum difference obtainable which is $1$. The smallest of them is $25$.

Test case $4$: $n = 242$, $243 = 3^{5}$ is a perfect power but $241$ is not, so the answer is $243$.

Test case $5$: $128 = 2^{7}$ is closest to $129$.

Test case $6$: $394857628993920400 = 628376980^{2}$ is the closest to $394857629456789876$.

Test case $7$: $353872815358410000 = 24390^{4}$ is the closest to $353872815358409997$.

Difficulty: VERY_HARD
Tags: Preprocessing

Mathematics

Starter code:

Solutions:
from math import floor, log, exp, log1p, expm1
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59]
for __ in range(int(input())):
	(n, arr) = (int(input()), [])
	for bp1 in primes:
		a = floor(expm1(log1p(n - 1) / bp1)) + 1
		arr.append(pow(a, bp1))
		arr.append(pow(a + 1, bp1))
	arr.sort()
	i = len(arr) - 1
	while i >= 0 and arr[i] > n:
		i -= 1
	(x1, x2) = (arr[i], arr[i + 1])
	print(x1 if abs(n - x1) <= abs(n - x2) else x2)


import sys
import itertools

def erat2():
	D = {}
	yield 2
	for q in itertools.islice(itertools.count(3), 0, None, 2):
		p = D.pop(q, None)
		if p is None:
			D[q * q] = q
			yield q
		else:
			x = p + q
			while x in D or not x & 1:
				x += p
			D[x] = p

def get_primes_erat(n):
	return list(itertools.takewhile(lambda p: p < n, erat2()))
primes = get_primes_erat(100000)
tests = int(input())
for test in range(tests):
	num = int(input())
	best_perfect = 1
	best_distance = num - 1
	for power in primes:
		base = int(num ** (1 / power))
		lower_perfect = base ** power
		distance = num - lower_perfect
		if distance == best_distance:
			best_perfect = min(best_perfect, lower_perfect)
		elif distance < best_distance:
			best_perfect = lower_perfect
			best_distance = distance
		higher_perfect = (base + 1) ** power
		distance = higher_perfect - num
		if distance == best_distance:
			best_perfect = min(best_perfect, higher_perfect)
		elif distance < best_distance:
			best_perfect = higher_perfect
			best_distance = distance
		if base == 1:
			break
	print(best_perfect)


import math
t = int(input())
for i in range(0, t):
	n = int(input())
	if n <= 2:
		print(1)
	else:
		L = []
		M = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 39, 41, 43, 47, 53, 59, 61]
		for u in M:
			x = math.floor(n ** (1 / u))
			if pow(x, u) == n:
				x1 = x
			else:
				x1 = x + 1
			y = pow(x, u)
			y1 = pow(x1, u)
			L.append((abs(n - y), y))
			L.append((abs(n - y1), y1))
		L.sort()
		print(L[0][1])