• UVA


    Description

    Download as PDF

    Problem E: Perfect Pth Powers

    We say that x is a perfect square if, for some integer b,x = b^2. Similarly, x is a perfect cube if, for some integerb, x = b^3. More generally, x is a perfect pthpower if, for some integer b, x = b^p. Given aninteger x you are to determine the largest p such that xis a perfect pth power.

    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 therange of a (32-bit) int inC, C++, and Java. A line containing 0 follows the last test case.

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

    Sample Input

    17
    1073741824
    25
    0
    

    Output for Sample Input

    1
    30
    2
    

    题意:求x=b^p,的 最大的p

    思路:首先将n分解质因数,然后找因数个数的gcd就是了,负数的话,一直除以2。直到答案是奇数的时候

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    typedef long long ll;
    using namespace std;
    const int maxn = 333333;
    
    ll n;
    int prime[maxn], vis[maxn], cnt = 0;
    
    int gcd(int a, int b) {
    	return b==0?a:gcd(b, a%b);
    }
    
    int solve() {
    	ll tmp = n;
    	if (n < 0)
    		n = -n;
    	int ans = 0;
    	for (int i = 0; i < cnt && prime[i] <= n; i++) {
    		int count = 0;
    		while (n % prime[i] == 0) {
    			count++;
    			n /= prime[i];
    		}
    		ans = gcd(ans, count);
    	}
    	if (ans == 0)
    		ans = 1;
    	if (tmp < 0) {
    		while (ans % 2 == 0)
    			ans /= 2;
    	}
    	return ans;
    }
    
    int main() {
    	for (int i = 2; i < maxn; i++) {
    		if (vis[i])
    			continue;
    		prime[cnt++] = i;
    		for (int j = i; j < maxn; j += i)
    			vis[j] = 1;
    	}
    	while (scanf("%lld", &n) != EOF && n) {
    		printf("%d
    ", solve());
    	}
    	return 0;
    }



  • 相关阅读:
    SAP系统报错
    基金投资
    Reading: 重构相关
    C/C++: static variables
    C/C++: 如何删除本地文件/读取某个目录下符合某种pattern的所有文件路径
    如何在Linux下建立包含lua vm的unit test framwork
    C++实现字符串分割(类似于Python的split方法)
    postMan 汉化
    搞懂MySQL InnoDB事务ACID实现原理
    mysql 索引优化
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6871718.html
Copyright © 2020-2023  润新知