poj 1730Perfect Pth Powers(分解质因数)
来源:互联网 发布:淘宝网沙发垫 编辑:程序博客网 时间:2024/06/05 08:52
Perfect Pth Powers
Time Limit: 1000MS Memory Limit: 10000KTotal 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
171073741824250
Sample Output
1302
题意:给出一个整数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); //如果n不为1,则此时的n必为是一个素数 if(flag) { while(ans % 2 == 0) ans /= 2; } printf("%d\n", ans); } return 0;}
0 0
- poj 1730Perfect Pth Powers(分解质因数)
- POJ 题目1730 Perfect Pth Powers(质因子分解)
- POJ 1730 Perfect Pth Powers(数论)
- POJ-1730 Perfect Pth Powers(思维:大数分解素因子)
- POJ 1730 Perfect Pth Powers
- poj 1730 -- Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- poj-1730 Perfect Pth Powers
- poj-1730 Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- POJ-1730-Perfect Pth Powers
- POJ 1730 Perfect Pth Powers
- poj 1730 Perfect Pth Powers
- POJ 1730 Perfect Pth Powers 解题报告(因式分解)
- POJ 1730 Perfect Pth Powers(素数筛选法)
- zoj 2124 || poj 1730 Perfect Pth Powers
- poj 1730 Perfect Pth Powers(枚举)
- 各位acmer 学累的时候不妨来看看
- 向量时钟Vector Clock in Riak
- linux 单引号,双引号,反引号
- 关于servlet的 url-pattern 配置
- C++基类与派生类的转换
- poj 1730Perfect Pth Powers(分解质因数)
- Spring源码由浅入深系列四 创建BeanFactory
- 读书笔记之《淘宝技术这十年》
- 黑马程序员_Object和内部类
- javascript中的计时器
- android中保存Bitmap图片到指定文件夹中的方法
- IO流概念
- android4.4系统LoadAverageService服务分析
- ios8 didRegisterForRemoteNotificationsWithDeviceToken 未被调用