HDU-6623 Minimal Power of Prime (思维 +素数筛)

探讨了在给定正整数n的情况下,将其分解为素数的幂次乘积形式,并找出所有素数幂次中最小的一个。通过预处理素数至n^1/5,有效地降低了算法复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Minimal Power of Prime

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

Source

2019 Multi-University Training Contest 4

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6623

题目大意:给你一个数n,把它分解为素数的幂次的乘积的形式:n=p1^e1 * p2^e2 * .......pk^ek  求最小的幂次是多少

例如:16=2^4  ans=4,    108=2^2 * 3^3   ans=2

思路:因为n到10^18 所以我们不能直接处理出到n^1/2的素数,但是我们可以处理出到n^1/5的素数,把存在幂次可能为5的素因子都先分解出来, 那么现在的n的素因子的幂次最为4,那么就判这个数是不是一个数的4次方如果是的话ans=min(ans,4),

如果不是的话再判n是不是一个数的3次方,如果是的话ans=min(ans,3),如果不是的话就判n是不是一个数的平方,如果是ans=min(ans,2),如果不是那么ans=1

这样就可以把复杂度降到O(T*N^1/5 / log(N)).

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<math.h>
using namespace std;
#define ll long long
const int N=500005;
int a[10005],tot;
ll b[10005];
void init()
{
    tot=0;
    a[0]=a[1]=1;
    for(int i=2;i<=10000;i++)
        if(a[i]==0)
    {
        b[tot++]=(ll)i;
        for(int j=i*2;j<=10000;j+=i)
            a[j]=1;
    }
}
int fun(ll n)
{
    ll l=1,r=pow(n*1.0,1.0/3)+1,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        ll y=mid*mid*mid;
        if(y==n) return 1;
        else if(y>n) r=mid-1;
        else l=mid+1;
    }
    return 0;
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        int x,ans=100;
        for(int i=0;i<tot;i++)
        {
            if(n<b[i]) break;
            x=0;
            if(n%b[i]==0)
            {
                while(n%b[i]==0)
                {
                    n/=b[i];
                    x++;
                }
                ans=min(ans,x);
            }
        }
        if(n==1) printf("%d\n",ans);
        else if(ans==1) printf("%d\n",ans);
        else if(n>1)
        {
            ll m1=(ll)sqrt(sqrt(n*1.0));
            ll m2=(ll)sqrt(n*1.0);
            if((m1*m1*m1*m1)==n) ans=min(ans,4);
            else if(fun(n)==1) ans=min(ans,3);
            else if(m2*m2==n) ans=min(ans,2);
            else ans=1;
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值