HDU 6623 Minimal Power of Prime

Posted by 486974 on Fri, 11 Oct 2019 17:41:17 +0200

Time limit 1000 ms
Memory limit 65536 kB
OS Windows

Chinese title

Given a number n, let's decompose the prime factor of N and get it.

\[n=\prod_{i=1}^{\omega(n)} a_i^{p_i}\]

Among them, ( omega(n) means the number of different prime factors of n, is the prime factor of n.

Minimum output is required for (p_i).

Title Solution

It feels great to finish reading the problem.——

Let's first factorize \(N\) using prime numbers not larger than \(N^{\frac{1}{5}}\). And let's denote \(M\) as the left part, all the prime factors of \(M\) are larger than \(N^{\frac{1}{5}}\). If \(M=1\) then we are done, otherwise M can only be \(P^2\), \(P^3\), \(P^4\) or \(P^2 \times Q^2\), here \(P\) and \(Q\) are prime numbers.

  1. If \(M^{\frac{1}{4}}\) is an integer, we can know that \(M=P^4\). Update answer using 4 and return.
  2. If \(M^{\frac{1}{3}}\) is an integer, we can know that \(M=P^3\). Update answer using 3 and return.
  3. If \(M^{\frac{1}{2}}\) is an integer, we can know that \(M=P^2\) or \(M=P^2 \times Q^2\). No matter which situation, we can always update answer using 2 and return.
  4. If (1)(2)(3) are false, we can know that answer=1.

Since there are just \(O(\frac{N^{\frac{1}{5}}}{log(N)})\) prime numbers, so the expected running time is \(O(\frac{TN^{\frac{1}{5}}}{log(N)})\).

The reason I didn't respond to the official questions at first was that

M can only be \(P^2\), \(P^3\), \(P^4\) or \(P^2 \times Q^2\),

Not only, (M) but also \\\\\\\\\\\\\\\\\\\\

Another question is how to judge whether ( sqrt[4]M), ( sqrt[3]M), and ( sqrt{M}) are integers. We can find the results of (\ sqrt[4]M), ( sqrt[3]M\), sqrt (\ sqrt{M}\) downwards and multiply them back, for example, to see if (\ lfloor\ sqrt{M}\ exists or not.

Moreover, when ( lfloor sqrt [3] {M} rfloor), the accuracy of the pow function is not enough, so we use pow(n,1.0/3.0) to solve WA, which needs dichotomy. The code for the following dichotomy comes from This blog The two function in the code, if there is an integer solution, returns an integer solution, otherwise returns a negative one, which is quite good.

And... Complexity Where did that log come from? Is it related to the distribution of prime numbers?

source code

#include<stdio.h>
#include<math.h>
#include<algorithm>
int T;
long long n;
long long cnt=0;
long long prime[10000];
long long ans;
bool vis[10000];

void shai()//Take the variable name back to the time before liberation. Not to mention, it's convenient and clear at a glance.
{
    for(int i=2;i<=10000;i++)
    {
        if(!vis[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<=10000;j++)
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)
            break;
        }
    }
}

long long sancigeng(long long a)
{
    long long l=1,r=(long long)pow(n*1.0, 1.0 / 3) + 1,mid;
    while(l<=r){
        mid=(l+r)>>1;
        if(mid*mid*mid==n) return mid;
        else if(mid*mid*mid>n) r=mid-1;
        else l=mid+1;
    }
    return -1;
}

int main()
{
    shai();
    //freopen("test.in","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        ans=0x7fffffff;
        int pw,pr;
        for(int i=1;i<=cnt;i++)
        {
            if(n%prime[i]==0)
            {
                pr=prime[i],pw=0;
                while(n%pr==0)
                {
                    n/=pr;
                    pw++;
                }
                if(pw<ans) ans=pw;
            }
            if(ans==1)
            {
                n=1;
                break;
            }
        }
        if(n==1)
        {
            printf("%lld\n",ans);
            continue;
        }
        long long temp1=sqrt(n),temp2=sqrt(temp1),temp3=sancigeng(n);
        if(temp2*temp2==temp1&&temp1*temp1==n)
        {//The max and min in algorithm do not support long and int mixed parameters, and they do not implicitly type-convert.
            ans=std::min(ans,4LL);
        }
        else if(temp3>=0)
        {
            ans=std::min(ans,3LL);
        }
        else if(temp1*temp1==n)
        {
            ans=std::min(ans,2LL);
        }
        else ans=1;
        printf("%lld\n",ans);
    }
    return 0;
}

Topics: PHP Windows