LightOJ 1220 Mysterious Bacteria

文章地址:http://henuly.top/?p=696

题目:

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

题目链接

对每个数 X X = a Y ,求 Y 的最大值。

对于每一个 X 进行质因数分解

X = p 1 e 1 × p 2 e 2 × . . . × p k e k

Y = g c d ( e 1 , e 2 , . . . , e k ) 时可以将 p 合并为一个数 M

X = M g c d ( e 1 , e 2 , . . . , e k )

此时 Y 具有最大值。

这道题目 X 并不是正整数,会有负数,所以当 X 为负数时需要将其转换为正数求解

X 为负数且 Y 最大值为偶数(显然不能为偶数)时,需要将 M 逐次乘方直到 Y 为奇数。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define XDebug(x) cout << #x << "=" << x << endl;
#define ArrayDebug(x,i) cout << #x << "[" << i << "]=" << x[i] << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e7 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) {
        return 0;
    }
    while (c != '-' && (c < '0' || c > '9')) {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return 1;
}
template <class T>
inline void out(T x) {
    if (x > 9) {
        out(x / 10);
    }
    putchar(x % 10 + '0');
}

bool IsPrime[maxn];
ll Prime[maxn / 10];
int tot;
vector<ll> ResolveAns;

inline ll gcd(ll x, ll y) {
    return y ? gcd(y, x % y) : x;
}

void PrimeInit() {
    mem(IsPrime, 1);
    IsPrime[1] = 0;
    tot = 0;
    for (ll i = 2; i < maxn; ++i) {
        if (IsPrime[i]) {
            Prime[tot++] = i;
            for (ll j = i * i; j < maxn; j += i) {
                IsPrime[j] = 0;
            }
        }
    }
}

void Resolve(ll x) {
    ResolveAns.clear();
    int num = 0;
    while (x > 1 && Prime[num] * Prime[num] <= x && num < tot) {
        if (!(x % Prime[num])) {
            int cnt = 0;
            while (!(x % Prime[num])) {
                x /= Prime[num];
                cnt++;
            }
            ResolveAns.pb(cnt);
        }
        num++;
    }
    if (x > 1) {
        ResolveAns.pb(1);
    }
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    PrimeInit();
    ll T;
    read(T);
    for (ll Case = 1, N; Case <= T; ++Case) {
        bool symbol = 0;
        read(N);
        if (N < 0) {
            N = -N;
            symbol = 1;
        }
        Resolve(N);
        ll ans = ResolveAns[0];
        for (int i = 1; i < int(ResolveAns.size()); ++i) {
            ans = gcd(ans, ResolveAns[i]);
        }
        if (symbol) {
            while (!(ans % 2)) {
                ans >>= 1;
            }
        }
        printf("Case %lld: %lld\n", Case, ans);
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Tony5t4rk/article/details/81507893