Minimal Power of Prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1753 Accepted Submission(s): 393
Problem Description
You are given a positive integer n > 1. Consider all the different prime divisors of n. Each of them is included in the expansion n into prime factors in some degree. Required to find among the indicators of these powers is minimal.
Input
The first line of the input file is given a positive integer T ≤ 50000, number of positive integers n in the file. In the next T line sets these numbers themselves. It is guaranteed that each of them does not exceed 10^18.
Output
For each positive integer n from an input file output in a separate line a minimum degree of occurrence of a prime in the decomposition of n into simple factors.
Sample Input
5 2 12 108 36 65536
Sample Output
1 1 2 2 16
思路:考虑分类讨论,当答案大于5时,我们可以知道,一定是有小于4000的质数相乘得到的,所以可以暴力跑一边4000以内的所有质数。并记录答案。若4000以内没有答案,则说明剩下的一定是大于4000的质数相乘得到的,所以我们可以分别二分四次方,三次方和二次方,如果与之前的ans取较小的值。否则说明答案一定是1。
AC代码:
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
typedef long long ll;
const int MAXN = 2e5+50;
const int INF = 0x3f3f3f3f;
ll n, t;
ll prime[MAXN];
ll lst[MAXN];
int primelist(){
int cnt = 0;
lst[2] = 0;
for(ll i = 2; i < 4000; i++){
if(!lst[i]){
prime[++cnt] = i;
for(ll j = i; j < 4000; j+=i){
lst[j] = 1;
}
}
}
return cnt;
}
ll find2(ll n){
ll l = 4000, r = 1e9+10;
ll ans = -1;
while(l <= r){
ll mid = (l+r)/2;
if(mid*mid < n){
l = mid+1;
}else if(mid*mid > n){
r = mid-1;
}else if(mid*mid == n){
return mid;
}
}
return ans;
}
ll find3(ll n){
ll l = 4000, r = 1e6+10;
ll ans = -1;
while(l <= r){
ll mid = (l+r)/2;
if(mid*mid*mid < n){
l = mid+1;
}else if(mid*mid*mid > n){
r = mid-1;
}else if(mid*mid*mid == n){
return mid;
}
}
return ans;
}
ll find4(ll n){
ll l = 4000, r = 1e5+10;
ll ans = -1;
while(l <= r){
ll mid = (l+r)/2;
if(mid*mid*mid*mid < n){
l = mid+1;
}else if(mid*mid*mid*mid > n){
r = mid-1;
}else if(mid*mid*mid*mid == n){
return mid;
}
}
return ans;
}
int main() {
//ios::sync_with_stdio(false);
// freopen("out.txt","w",stdout);
// while(~scanf("%lld", &t))
// printf("%lld\n", t*t);
// return 0;
scanf("%lld", &t);
int cnt = primelist();
while(t--){
scanf("%lld", &n);
int flag = 0;
int ans = INF;
int ct;
rep(i, 1, cnt){
ct = 0;
while(!(n%prime[i])){
n /= prime[i];
ct++;
}
if(ct){
ans = min(ans, ct);
if(n == 1 || ans == 1){
flag = 1;
break;
}
}
}
if(flag){
printf("%d\n", ans);
}else{
int k;
if(find4(n)!=-1){
printf("%d\n", min(ans, 4));
}else if(find3(n)!=-1){
printf("%d\n", min(ans, 3));
}else if(find2(n)!=-1){
printf("%d\n", min(ans, 2));
}else{
printf("%d\n", min(ans, 1));
}
}
}
return 0;
}
/*
4
100010740288369
100015260582169
100019220923521
100003380028561
*/