Problem Description
Given a number x, ask positive integer
y≥2
, that satisfy the following conditions:
1. The absolute value of y - x is minimal
2. To prime factors decomposition of Y, every element factor appears two times exactly.
1. The absolute value of y - x is minimal
2. To prime factors decomposition of Y, every element factor appears two times exactly.
Input
The first line of input is an integer T (
1≤T≤50
)
For each test case,the single line contains, an integer x ( 1≤x≤1018 )
For each test case,the single line contains, an integer x ( 1≤x≤1018 )
Output
For each testcase print the absolute value of y - x
Sample Input
5 1112 4290 8716 9957 9095
Sample Output
23 65 67 244 70
把x开个根号,相对而言y就是每个素数因子出现一次的数,可以预处理一下10w的素数,然后直接暴力即可
#include<set> #include<map> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) using namespace std; typedef __int64 LL; const int low(int x) { return x&-x; } const double eps = 1e-8; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 1e5 + 10; int T, tp, p[N], f[N], t; LL n, ans; void init() { t = 0; rep(i, 2, N - 1) { if (!f[i]) p[t++] = i; for (int j = 0; j < t&&p[j] * i < N; j++) { f[i*p[j]] = 1; if (i%p[j]) break; } } } bool check(int x) { for (int i = 0, j, k = x; i < t; i++) { for (j = 0; k%p[i] == 0; j++) k /= p[i]; if (j > 1) return false; if (p[i] * p[i] > k) break; } ans = min(ans, abs(n - 1LL * x*x)); return true; } int main() { init(); scanf("%d", &T); while (T--) { scanf("%I64d", &n); LL x = (LL)sqrt(1.0*n); LL y = x + 1; ans = INF; while (x >= 2 && !check(x)) --x; while (!check(y)) ++y; printf("%I64d\n", ans); } return 0; }