题意:
已知任意大于\(1\)的整数\(a = p_1^{q_1}p_2^{q_2} \cdots p_k^{q_k}\),现给出\(a \in [2,1e18]\),求\(min\{q_i\},q \in [1, k]\)。即求质因数分解后,最小指数是多少。
思路:
因为\(a \in [2,1e18]\),所以我们现打一个\(1e4\)以内的质数表,然后直接求出\(1e4\)以内的情况。
上面弄完了,那么现在最多只有\(4\)个质因子,情况如下:
\(n = p^4\),这种情况就是\(4\)次
\(n = p^3\),这种情况是3次,\(n = p_1^3p_2\)就直接和最后一种答案一样
\(n = p^2\),\(p = p_1 * p_1\)就是第一种情况
\(n = p\)
代码:
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<stack>
#include<ctime>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 10000 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
using namespace std;
int prime[maxn], p[maxn], cnt;
void init(){
memset(p, 0, sizeof(p));
cnt = 0;
for(int i = 2; i < maxn; i++){
if(!p[i]){
prime[cnt++] = i;
}
for(int j = 0; j < cnt && i * prime[j] < maxn; j++){
p[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
}
bool triple(ll n){
ll l = 1e4, r = 1e6;
while(l <= r){
ll m = (l + r) >> 1;
ll ret = m * m * m;
if(ret == n) return true;
if(ret > n) r = m - 1;
else l = m + 1;
}
return false;
}
int main(){
int T;
init();
scanf("%d", &T);
while(T--){
ll n;
scanf("%lld", &n);
int ans = 1000;
for(int i = 0; i < cnt && prime[i] <= n; i++){
if(n % prime[i] == 0){
int num = 0;
while(n % prime[i] == 0){
num++;
n /= prime[i];
}
ans = min(ans, num);
}
}
if(n > 1 && ans > 1){
ll t1 = ll(sqrt(n));
ll t2 = ll(sqrt(t1));
if(t2 * t2 * t2 * t2 == n) ans = min(ans, 4);
else if(t1 * t1 == n) ans = min(ans, 2);
else if(triple(n)) ans = min(ans, 3);
else ans = 1;
}
printf("%d\n", ans);
}
return 0;
}
原文地址:https://www.cnblogs.com/KirinSB/p/11290778.html
时间: 2024-10-08 21:06:12