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
65536

Sample 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值