Minimal Power of Prime(HDU-6623)
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
65536Sample Output
1
1
2
2
16
题意:t 组数据,每组给出一个数 n,要求将这个数进行质因数分解,然后求所有质因数的最小指数
思路:
首先 t 最大可达到 50000,而且 n 最大能到 1E18,大数质因子分解肯定超时
因此可以先将 1E4 内的质数筛出来,由 1E4 内的质数进行判断,如果 prime[i] 是 n 个质因子,就让 n 除以 prime[i],最后如果不是 1,那么说明 n 除剩下的是一个大于 1E4 的数
此时,质因子的幂最大就是 4,因此对此时的 n 进行开方、开三次方、开四次方,判断能不能开出整数,然后记录最小幂即可
至于为什么是选择 1E4 内的质数,这个是试出来的,1E5 会超时。。
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 10000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int primes[N],cnt;
bool bPrime[N];
void getPrimes(int n){
memset(bPrime,false,sizeof(bPrime));
for(int i=2;i<=n;i++){
if(!bPrime[i])
primes[cnt++]=i;
for(int j=0;j<cnt&&i*primes[j]<n;j++){
bPrime[i*primes[j]]=true;
if(i%primes[j]==0)
break;
}
}
}
int check(LL x, int k, LL n) {
LL sum = 1;
for (int i = 1; i <= k; i++)
sum *= x;
if (sum > n)
return 1;
else if (sum < n)
return -1;
else
return 0;
}
bool judge(LL left, LL right, LL n, int k) {
while (left <= right) {
LL mid = (left + right) >> 1;
int temp = check(mid, k, n);
if (temp == 1)
right = mid - 1;
else if (temp == -1)
left = mid + 1;
else
return true;
}
return false;
}
int cal(LL x) {
if (x == 1)
return 1000;
if (judge(10000, 1000000, x, 3))//二分开三次方
return 3;
//开方
LL temp = sqrt(x);
if ((temp - 1) * (temp - 1) == x)
temp--;
if ((temp + 1) * (temp + 1) == x)
temp++;
if (temp * temp != x)
return 1;
//开四次方
x = temp;
temp = sqrt(temp);
if ((temp - 1) * (temp - 1) == x)
temp--;
if ((temp + 1) * (temp + 1) == x)
temp++;
if (temp * temp == x)
return 4;
else
return 2;
}
int main() {
getPrimes(10000);
int t;
scanf("%d", &t);
while (t--) {
LL n;
scanf("%lld", &n);
int res = 10000;
for (int i = 0; i < cnt; i++) {
if (n % primes[i] == 0) {//如果n能整除质因子primes[i]
int num = 0;
while (n % primes[i] == 0) {//除到除不动为止
num++;
n /= primes[i];
}
res = min(res, num);//记录最小
}
if (n == 1)
break;
}
if (n > 10000)
res = min(res, cal(n));
if (res == 10000)
res = 1;
printf("%d\n", res);
}
return 0;
}