POJ-1730 perfect PTH powers factorization factor

Source: Internet
Author: User

This question is to find the maximum number of power to which a number can be opened. This includes a negative number and must be read in long data. First, we break down the prime factor of this number and calculate their respective numbers. Then, we find a GCD for their numbers and finally output them. If it is a positive number, it is output directly. If it is a negative number, you need to find the largest Odd Factor.

The Code is as follows:

#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int p[66000], cnt;long long rec[6600], N; int son[6600];int gcd(int a, int b){    return b == 0 ? a : gcd(b, a % b);    }void pre(){    int k;    for (int i = 4; i <= 66000; i += 2) {        p[i] = 1;    }    for (int i = 3; i <= 257; i += 2) {        if (!p[i]) {            k = 2 * i;            for (int j = i * i; j <= 66000; j += k) {                p[j] = 1;            }        }    }    cnt = 1;    rec[1] = 2;    for (int i = 3; i <= 66000; i += 2) {        if (!p[i]) {            rec[++cnt] = i;        }    }//    printf("cnt = %d\n", cnt);}int deal(long long x){    bool flag = true;    int ans = 0;    for (int i = 1; i <= cnt; ++i) {        while (x % rec[i] == 0) {                 son[i]++;            x /= rec[i];        }    }    if (x != 1) {        return 1;    }    for (int i = 1; i <= cnt; ++i) {        if (son[i]) {            ans = gcd(ans, son[i]);        }    }    return ans;}// 1728 = 12 ^ 3  11664 = 108^2int main(){    int ans;    pre();    while (scanf("%I64d", &N), N) {        memset(son, 0, sizeof (son));         ans = deal(N < 0 ? -N : N);        if (N < 0) {            while (!(ans & 1)) {                ans >>= 1;            }            printf("%d\n", ans);        }        else {            printf("%d\n", ans);        }    }    return 0;    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.