UVA - 10622 Perfect P-th Powers
来源:互联网 发布:java随机生成4位验证码 编辑:程序博客网 时间:2024/05/16 18:27
Description
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 anintegerx 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 thatx isa perfect pth power.
Sample Input
171073741824250
Output for Sample Input
1302
题意:求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\n", solve());}return 0;}
0 0
- UVa 10622 - Perfect P-th Powers
- UVA 10622 - Perfect P-th Powers(数论)
- uva 10622 - Perfect P-th Powers(数论)
- UVA - 10622 Perfect P-th Powers
- UVA 10622 - Perfect P-th Powers
- UVa 10622 - Perfect P-th Powers
- UVa 10622 - Perfect P-th Powers
- Perfect P-th Powers UVA
- UVA 10622 Perfect P-th Powers(数论)
- UVa 10622 - Perfect P-th Powers(数论)
- uva 10622——Perfect P-th Powers
- UVa10622 Perfect P-th Powers
- UVa 10622 - Perfect P-th Powers(对数+快速幂)
- UVA 10622 Perfect P-th Powers (唯一分解定理 + GCD)
- 10622 - Perfect P-th Powers(唯一分解定律)
- UVA 10622(p343)----Perfect Pth Powers
- POJ 1730 Perfect Pth Powers (求最大p,x=b^p)
- poj1730 - Perfect Pth Powers
- oracle存储过程自定义split函数
- 免费sap学习资料
- 启发式A*算法的整理
- win8.1不支持交行满金宝的解决方法
- 5G候选技术都有哪些
- UVA - 10622 Perfect P-th Powers
- centos7.0的几个新特性
- mongo mongoexport 导出数据为csv格式
- 运算符重载
- vs2012设置tab为4个space的方式
- POJ 3301 Texas Trip
- 【Sqlit数据库官方API学习—part02】触发器(Trigger)
- 如何解决Firefox26第三方插件无法工作,提示需要激活?
- Android实现推送方式解决方案