给定整数N,已知N可以写成ppq的形式,其中p和q为不同质数,求p和q。
1<=N<=9E18
分析:p与q的最小值不超过3E6,可以枚举。
#include <bits/stdc++.h>
using i64 = long long;
std::vector<int> minp, prime;
void sieve(int n) {
minp.assign(n + 1, 0);
prime.clear();
for (int i = 2; i <= n; i++) {
if (minp[i] == 0) {
minp[i] = i;
prime.push_back(i);
for (int j = 2 * i; j <= n; j += i) {
if (minp[j] == 0) {
minp[j] = i;
}
}
}
}
}
void solve() {
i64 N;
std::cin >> N;
for (auto x : prime) {
if (N % x == 0) {
N /= x;
if (N % x == 0) {
i64 y = N / x;
std::cout << x << " " << y << "\n";
} else {
i64 y = std::sqrt(N);
std::cout << y << " " << x << "\n";
}
return;
}
}
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
sieve(3E6);
int t = 1;
std::cin >> t;
while (t--) solve();
return 0;
}
标签:std,prime,abc284D,int,long,i64,Year,New
From: https://www.cnblogs.com/chenfy27/p/18475160