LightOJ1220 Mysterious Bacteria prime factorization of

LightOJ1220 Mysterious Bacteria

label

  • Prime factorization of

Foreword

Concise meaning of the questions

  • Given n, find (a ^ k = \ n) \ , the maximum k

Thinking

  • Directly to the prime factor decomposition of n. Min_c defined as the smallest c. For all have c \ (min \ _c | c \ ) time, c is the answer. Otherwise, the answer is 1.
  • However, when such considerations are not comprehensive, because n may be negative! This time it needs special judge it. Still a step above, if it is negative, the answer is min_c largest odd factor. And the biggest odd factor, directly min_c constantly / 2, in addition to not divide up on it.

    Let min_c constantly / 2, in addition to not divide up on it. Talk about here's why. 2 prime number is odd except for the prime factorisation min_c, 2 without considering his other prime factors are multiplied, the product must be odd (odd number by odd numbers are still odd). Other so the prime factors after the prime factors 2 min_c the product is removed min_c largest odd factor.

Precautions

  • no

to sum up

  • Seeking the largest odd factor: remove the 2 prime factors, other qualitative factors are multiplied as usual is the answer.

AC Code

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;

bool no_prime[maxn];
int prime[maxn];
int shai(int n)
{
    int cnt = 0;
    no_prime[1] = 1;

    for (int i = 2; i <= n; i++)
    {
        if (!no_prime[i])
            prime[++cnt] = i;

        for (int j = 1; j <= cnt && prime[j] * i <= n; j++)
        {
            no_prime[prime[j] * i] = 1;
            if (i % prime[j] == 0) break;
        }
    }
    return cnt;
}

void solve()
{
    int cnt = shai(maxn - 10);

    int t;
    scanf("%d", &t);
    for (int i = 1; i <= t; i++)
    {
        long long n, r;
        scanf("%lld", &n);
        r = n;      
        n = abs(n);
        

        int flag = 1;

        int x_cnt = -1;
        for (int i = 1; 1ll * prime[i] * prime[i] <= abs(r) && n != 1; i++)
        {
            int cnt = 0;
            while (n % prime[i] == 0)
                n /= prime[i], cnt++;
            if (x_cnt == -1 && cnt != 0)
                x_cnt = cnt;

            

            if (cnt != 0 && !(x_cnt % cnt == 0 || cnt % x_cnt == 0) )
            {
                flag = 0;
                break;
            }

            if (x_cnt != -1 && cnt != 0)
            {
                x_cnt = min(x_cnt, cnt);
            }
        }
        if (n != 1 && n != x_cnt)
            flag = 0;
        if (r < 0 && x_cnt % 2 == 0)
        {
            while (x_cnt % 2 == 0)
                x_cnt /= 2;
        }
        if (x_cnt == -1) x_cnt = 1;

        if (flag)
            printf("Case %d: %d\n", i, x_cnt);
        else
            printf("Case %d: 1\n", i);
    }
}

int main()
{
    freopen("Testin.txt", "r", stdin);
    solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/danzh/p/11420963.html