解题思路:先把x开根号,往两边暴力搜索符合条件的数,取最小值即可。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
inline void read(LL &n){n=0;bool _minus=false;while(true){unsigned char c=getchar();if(c==' '||c=='\n')break;if(c=='-'){_minus=true;continue;}n=n*10+c-'0';}if(_minus)n*=-1;}
inline void read(int &n){n=0;bool _minus=false;while(true){unsigned char c=getchar();if(c==' '||c=='\n')break;if(c=='-'){_minus = true;continue;}n=n*10+c-'0';}if(_minus)n*=-1;}
//#pragma comment(linker, "/STACK:102400000,102400000")
bool isPrime[1000005];
int PrimeList[100000];
int cnt;
int Eular(int x)
{
for(int i = 0; i< 100005; i++)
{
isPrime[i] = true;
}
int Primecount = 0;
for(int i = 2; i<= x; i++)
{
if(isPrime[i])
{
Primecount++;
PrimeList[Primecount] = i;
}
for(int j = 1; j<= Primecount; j++)
{
if(i*PrimeList[j] > x) break;
isPrime[i*PrimeList[j]] = false;
if(i%PrimeList[j] == 0 ) break;
}
}
//for(int i = 1; i<= Primecount; i++) cout << PrimeList[i] << endl;
return Primecount;
}
int check(LL x)
{
for(int i=1;i<=cnt;i++)
{
LL k = PrimeList[i];
if(x%k==0)
{
x /= k;
if(x%k==0)return 0;
}
}
return 1;
}
LL x,y;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cnt = Eular(1000000);
int t;
read(t);
while(t--)
{
read(x);
y = floor(sqrt(x));
LL p = y, q = y + 1;
while(check(p)==0)p--;
while(check(q)==0)q++;
LL ans = abs(q*q-x);
if(p>=2)ans = min(ans,abs(p*p-x));
printf("%I64d\n",ans);
}
return 0;
}