O(n^{1/3}) 确定性分解质因数
Dirichlet 逼近定理: 任意
n>1(n\in \mathbb R) 和比例\alpha ,存在\frac{p}{q}(1\le q\le n) 使|\alpha-\frac{p}{q}|<\frac{1}{nq} 。
整个做法由上述定理启发。同时我印象中有类似复杂度的做法但未在网上找到。
处理掉
用
进一步地,由这两个范围可以得出
若能解出
其满足
这样
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline bool isqr(ll n, ll &rt) { return n < 0 ? 0 : (rt = sqrtl(n)) * rt == n; }
ll p3, _p6, _, m, res;
ll d(ll n) {
p3 = pow(n, 1.0 / 3), _p6 = pow(n, 1.0 / 6) / 4;
__int128 t = 4 * n;
for (ll k = 1; k <= p3; ++k, t += 4 * n)
for (ll x = sqrtl(t) + 0.5, y, xr = x + _p6 / sqrtl(k) + 1; x <= xr; ++x) if (isqr((__int128)x * x - t, y)) return __gcd(x - y, n);
return 1;
}
signed main() {
cin >> _;
while (_--) {
ll n; cin >> n, m = n, res = 0, p3 = pow(n, 1.0 / 3);
for (int i = 2; i <= p3; ++i) while (n % i == 0) n /= i, res = i;
ll t = d(n); ((res = max(res, max(t, n / t))) == m) ? cout << "Prime\n" : cout << res << '\n';
}
return 0;
}