Ultraviolet A 10622-perfect p-th powers (number theory)

Source: Internet
Author: User

Connection: Ultraviolet A 10622-perfect p-th powers

For X, if the maximum P exists so that the integer must be X = bp, X is called Perfect PTH power. Returns X and returns p.

Solution: use X to break down the prime factor. The maximum number of all input factors is P. Note that when X is a negative number, P must be an odd number. Pay special attention to X =-1.

#include <cstdio>#include <cstring>#include <cmath>const int maxp = 333333;int cp, v[maxp], prime[maxp];int gcd (int a, int b) {    return b == 0 ? a : gcd(b, a%b);}void primeTable (int n) {    memset(v, 0, sizeof(v));    cp = 0;    for (int i = 2; i < n; i++) {        if (!v[i]) {            prime[cp++] = i;            for (int j = 2 * i; j < n; j += i)                v[j] = 1;        }    }}int divFactor (long long n) {    int ans = 0;    for (int i = 0; i < cp && prime[i] <= n; i++) {        if (n % prime[i] == 0) {            int cnt = 0;            while (n % prime[i] == 0) {                n /= prime[i];                cnt++;            }            ans = gcd(ans, cnt);        }    }    if (n > 1 || ans == 0)        ans = 1;    return ans;}int main () {    primeTable(maxp);    long long n;    while (scanf("%lld", &n) == 1 && n) {        int ans = divFactor(n < 0 ? -n : n);            if (n < 0) {            while (ans%2 == 0)                ans /= 2;        }        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.