2019HDU school Minimal Power of Prime-- multi-segmented discussion thinking &&

topic

The $ n $ ($ 1 <n \ leq 10 ^ {18} $) prime factorisation, and quality factor of the minimum power.

analysis

Direct prime factorisation, not OK.

Can think, intercell prime factorisation, n-smaller, then the answer enumeration.

Prime number table between the printing and 1-10000 prime factorisation, the complete decomposition of the remaining number,

  • Two kinds of prime number (certainly larger than $ 10 $ ^ 4) multiplied by a maximum of two times, but also a number of squares together;
  • Prime number is multiplied by three or more, it is possible only once, regardless of.
  • One of several quality, up to the fourth power, enumerated four, three, quadratic, if not, is the single prime number

To note: look at is look at the 4 th power of 2 (since the fourth power must meet if they meet the square, but also to meet the meet the 4 th power of 2, it is not a power of 2, then the prime factors), 3 power does not matter, because the loss of accuracy will be open to the third power, so half of it.

#include <bits / STDC ++ H.>
 the using  namespace STD; 

typedef Long  Long LL; 
LL n; 

// number of primes less than n, returns
 // Eppendorf sieve O (nloglogn) 
const  int MAXN = 100000 + 10 ;
 int Prime [MAXN];             // prime [i] denotes the i-th prime number 
BOOL is_prime [MAXN + . 1 ];     // is_prime [i] is true if i is a prime number 

int Sieve ( int n-) 
{ 
    int CNT = 0 ;
     for ( int = I 0; i <= n; i++)  is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; i++)
    {
        if (is_prime[i])
        {
            prime[cnt++] = i;
            for (int j = i * i; j <= n; j += i)  is_prime[j] = false;  //i * i可能爆int
        }
    }
    return cnt;
}

bool is_three(ll n)    //是否能开立方
{
    ll l = 10000, r = 1e6;
    while(l <= r)
    {
        ll mid = (l+r) >> 1;
        ll tmp = mid*mid*mid;
        if(tmp == n)  return true;
        else if(tmp > n) r = mid-1;
        else l = mid+1;
    }
    return false;
}

int main()
{
    int cnt = sieve(10000);  //筛出10000内的质数
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld", &n);
        int ans = 100;
        for(int i = 0;i < cnt;i++)
        {
            if(n % prime[i] == 0)
            {
                int tmp = 0;
                while(n % prime[i] == 0)
                {
                    n /= prime[i];
                    tmp++;
                }
                ans = min(tmp, ans);
            }
            if(n == 1) break;
        }
        if(ans == 1){ printf("1\n"); continue;}
        if(n == 1){ printf("%d\n", ans); continue;}

        ll t1 = (ll)sqrt(n);
        ll t2 = (ll)sqrt(t1);
        if (t2 * t2 * t2 * t2 == n) ans = min (years, 4 );
        else  if (t1 t1 * == n) ans = min (year 2 );
        else  if (is_three (n)) ans = min (year 3 );
        else years = min (year 1 ); 
        printf ( " % d \ n " , year); 
    } 
    Return  0 ; 
}

 

 

Reference Links: https://blog.csdn.net/lgz0921/article/details/97948432

Guess you like

Origin www.cnblogs.com/lfri/p/11290356.html