HDU6623 Minimal Power of Prime

題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=6623
Minimal Power of Prime

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1935 Accepted Submission(s): 437

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

這道題題意是 ,幾個素數的幾次冪相乘,求最小的冪。
比如108=4*27=22*33,min=2;
那先打一個素數表求出1-4000的素數個數,由於數有1018,
要是沒除盡的話,因子最多也就4個了,所以冪數大於1的情況有p14,p13, p12 , p12*p22,
Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps=1e-5;
int P4 ( ll x )
{
    ll l=floor(pow( x, 1.0/4 )+eps);
    ll sum = l*l*l*l;
    return sum==x;
}
int P3 ( ll x )
{
    ll l=floor(pow( x, 1.0/3 )+eps);
    ll sum = l*l*l;
    return sum==x;
}
int P2 ( ll x )
{
    ll l=floor(pow( x, 1.0/2 )+eps);
    ll sum = l*l;
    return sum==x;
}
int isprime[4010];
int primes[4010],len;
void get_prime()
{
    len = 0;
    memset(isprime,true,sizeof(isprime));
    isprime[0] = false;
    isprime[1] = false;
    for( int i=2 ; i<4010 ; i++ )
    {
        if( isprime[i] )
            primes[len++] = i;
        for( int j=0 ; j<len ; j++ )
        {
            if( i*primes[j]>=4010 )
                break;
            isprime[i*primes[j]]=false;
            if( i%primes[j]==0 )
                break;
        }
    }
}

int main()
{
    get_prime();
    int repeat;
    cin>>repeat;
    while(repeat--)
    {
        ll n;
        cin>>n;
        int ans = 100;
        for( int i=0; i<len; i++ )
        {
            if(n<primes[i])
                break;
            if(n%primes[i]==0)
            {
                int tmp=0;
                while(n%primes[i]==0)
                {
                    n/= primes[i];
                    tmp++;
                }
                ans=min(ans,tmp);
            }
        }
        if( n==1 )
        {
            printf("%d\n",ans);
        }
        else
        {
            if(ans>4&&P4(n))
            {
                printf("4\n");
            }
            else if(ans>3&&P3(n))
            {
                printf("3\n");
            }
            else if(ans>2&&P2(n))
            {
                printf("2\n");
            }
            else
            {
                printf("1\n");
            }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章