poj 1730Perfect Pth Powers(分解质因数)

Perfect Pth Powers

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16746   Accepted: 3799

Description

We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power
if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth power.

Input

Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.

Output

For each test case, output a line giving the largest integer p such that x is a perfect pth power.

Sample Input

17
1073741824
25
0

Sample Output

1
30
2

题意:给出一个整数x,把x写成x=a^p,求p最大是多少?

分析:把x分解质因数,x = a1^b1 * a2^b2 … ak^bk,则最终结果为b1,b2,…bk的最大公约数。注意x有可能是负数。

如果x是负数,则要把求得的答案一直除以2,知道结果一个奇数,因为一个数的偶数次方不可能是负数。

#include <cstdio>
#include <cmath>
#include <cstring>

const int N = 66700;
int is[N];
int prime[7000], prime_cnt;

void get_prime() {
    for(int i = 0; i < N; i++) is[i] = 1;
    is[0] = is[1] = 0;
    prime_cnt = 0;
    int m = (int)sqrt(N + 0.5);
    for(int i = 2; i < N; i++) {
        if(is[i]) {
            prime[prime_cnt++] = i;
            if(i <= m) {
                for(int j = i * i; j < N; j += i)
                    is[j] = 0;
            }
        }
    }
}

int gcd(int a, int b) {
    if(a < b) return gcd(b, a);
    if(b == 0) return a;
    return gcd(b, a % b);
}

int main() {
    get_prime();
    long long n;  //不知道当n为int时为什么会TLE
    while(~scanf("%lld", &n) && n) {
        int flag = 0;
        if(n < 0) {
            flag = 1;
            n = -n;
        }
        int ans = 0;
        for(int i = 0; i < prime_cnt && n > 1; 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 = gcd(ans, 1);
        if(flag) {
            while(ans % 2 == 0) ans /= 2;
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-11-02 10:31:17

poj 1730Perfect Pth Powers(分解质因数)的相关文章

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a

POJ 1091 跳蚤(分解质因数 + 容斥 + 大数)

跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8910   Accepted: 2676 Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字.跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向

POJ 1730 Perfect Pth Powers (枚举||分解质因子)

Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16638   Accepted: 3771 Description We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More g

poj 2992 Divisors 整数分解

设m=C(n,k)=n!/((n-k)!*k!) 问题:求m的因数的个数 将m分解质因数得到 p1有a1个 p2有a2个 .... 由于每个质因数可以取0~ai个(全部取0就是1,全部取ai就是m)最后的答案就是(a1+1)*(a2+1)*....* 注意不能直接将m分解,因为太大,所以要先分解n,n-k,k,根据他们再来加减. #include <iostream> #include <cstdio> #include <cmath> #include<cstr

poj 2429 Pollard_rho大数分解

先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里可以直接搜索,注意一个问题,由于相同因子不能分配给两边(会改变gcd)所以可以将相同因子合并,这样的话,搜索的层数也变的很少了. #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<time.h&g

UVA 10622 - Perfect P-th Powers(数论)

UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取所有质因子个数的gcd就是答案,但是这题有个坑啊,就是输入的可以是负数,负数的情况比较特殊,p只能为奇数,这时候是要把答案不断除2除到为奇数即可. 代码: #include <stdio.h> #include <string.h> #include <math.h> long long n; int prime[333333], vi

大素数测试和分解质因数

Prime Test http://poj.org/problem?id=1811 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 typedef __int64 LL; 5 LL mulmod(LL a,LL b,LL c) { //ret=(a*b)%c 6 LL ret=0; 7 for(; b; a=(a<<1)%c,b>>=1) { 8 if(b&1) {

分解质因数模板

/*==================================================*| 分解质因数,可能有些地方需要改为long long \*==================================================*/ const int MAXN=100010; int prm[MAXN+1]; bool is[MAXN+1]; int getprm(int n){ int i, j, k = 0; int s, e = (int)(sqrt

java编程题 --分解质因数

package Solve; import java.util.Scanner; public class Solve { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入一个正整数:"); int num = scan.nextInt(); System.out.print(num + " = "