Mysterious Bacteria LightOJ - 1220 (Integer Unique Factorization Theorem + Sieve Primes)

Mysterious Bacteria

LightOJ - 1220

Dr. Mob has just discovered a Deathly Bacteria. He named it RC-01. RC-01 has a very strange reproduction system. RC-01 lives exactly x days. Now RC-01 produces exactly p new deadly Bacteria where x = bp (where b, p are integers). More generally, x is a perfect pth power. Given the lifetime x of a mother RC-01 you are to determine the maximum number of new RC-01 which can be produced by the mother RC-01.


Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a line containing an integer x. You can assume that x will have magnitude at least 2 and be within the range of a 32 bit signed integer.

Output

For each case, print the case number and the largest integer p such that x is a perfect pth power.

Sample Input

3

17

1073741824

25

Sample Output

Case 1: 1

Case 2: 30

Case 3: 2

Topic meaning:

Given a number x = b^p, find the maximum value of p

 

x = p1^x1*p2^x2*p3^x3*...*ps^xs

x = b^p, x has only one factor raised to the p-th power

If x = 12 = 2^2*3^1, let x = b^p, and 12 should be 12 = 12^1

So p = gcd(x1, x2, x3, ... , xs);

For example: 24 = 2^3*3^1, p should be gcd(3, 1) = 1, ie 24 = 24^1

         324 = 3^4*2^2, p should be gcd(4, 2) = 2, ie 324 = 18^2

There are many pits in this problem, that is, x may be negative. If x is negative, x = b^q, q must be an odd number, so if the solution obtained by converting x to a positive number is an even number, it must always be divided by 2 Converted to an odd number, followed by the fact that although it is given n<2^32, it cannot use int and needs to use long long to pass. I guess it is still a problem with negative numbers.

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+10;
bool isprime[maxn];
int prime[maxn];
int top;
void init(){
    memset(isprime,true,sizeof(isprime));
    print [0] = print [1] = false;
    top = 0;
    for(int i = 2; i < maxn; i++){
        if(isprime[i]){
            prime[top++] = i;
            for(int j = i+i; j < maxn; j += i){
                isprime[j] = false;
            }
        }
    }
}
int gcd(int a,int b){
    if(b == 0)
        return a;
    else return gcd(b,a%b);
}
int main(){
    init();
    int t,case = 0;
    scanf("%d",&t);
    while(t--){
        long long n;
        int flag = 0;
        int years = 0;
        scanf("%lld",&n);
        if(n < 0){
            flag = 1;
            n = -n;
        }
        for(int i = 0; prime[i] * prime[i] <= n && i < top; i++){
            if(n % prime[i] == 0){
                int cnt = 0;
                while(n % prime[i] == 0){
                    cnt++;
                    n /= prime[i];
                }
                ans = gcd(ans,cnt);
            }
            if(n == 1) break;
        }
        if(n != 1) years = gcd(years,1);
        if(flag){
            while(ans % 2 == 0)
                years /= 2;
        }
        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326912323&siteId=291194637