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 integer b, x = b 3. More generally, x is a perfect pth power if, for some integer b, x = b p. Given an integer x you are to determine the largest p such that x is a perfect p th 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 p th power.

Sample Input

17
1073741824
25
0

Sample Output

1
30
2 一点都不难但是很讨厌的一道题,很烦的地方就是输入可以为负,没有处理搞了半天runtime error。
然后输入用longlong。
对于输入为负的数字。例64=2^6,然而-64=(-4)^3。
综上,负数的output只能为奇数
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define N 100001
using namespace std;
typedef long long ll;
bool vis[N];
int prime[N];
int pn=0;
int gcd(int a,int b)
{
if(!b) return a;
return gcd(b,a%b);
}
int main()
{
for (int i = 2; i < N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (int j = i; j < N; j += i)
vis[j]=1;
}
ll n;
while(~scanf("%lld",&n),n)
{
ll r=n>0?n:-n;
int ans=0;
for(int i=0;(long long)prime[i]*prime[i]<=r;i++)
{
int tem=0;
while(r%prime[i]==0)
{
r/=prime[i];
tem++;
}
if(tem)
ans=gcd(ans,tem);
}
if(r!=1&&ans)
ans=gcd(ans,r);
if(!ans)
ans++;
if(n<0)
{
while(ans%2==0)
ans/=2;
}
cout<<ans<<endl;
}
}

  

poj_1730_Perfect Pth Powers的更多相关文章

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

    UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...

  2. Perfect Pth Powers poj1730

    Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16383   Accepted: 37 ...

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

                                                             id=1730">Perfect Pth Powers Time Li ...

  4. Kattis之旅——Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, ...

  5. POJ 1730 Perfect Pth Powers(暴力枚举)

    题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...

  6. [暑假集训--数论]poj1730 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 ...

  7. poj 1730 Perfect Pth Powers

    这个有2种方法. 一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理…… #include<iostream> #incl ...

  8. UVa 10622 (gcd 分解质因数) Perfect P-th Powers

    题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...

  9. uva10622 Perfect P-th Powers

    留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...

随机推荐

  1. ubuntu15.04 xampp 安装memcache

    要ThinkPHP3.2里由于要用到 memcache 缓存, 如果没有配置memcache,会报错,说系统支持 memcache. 所在配置的时候有点问题,现在解决了,现把它记录下来,以便以后查阅之 ...

  2. 案例50-crm练习dao层的抽取BaseDao

    1 抽取BaseDao 2 BaseDao设计思路 3 BaseDao接口书写 package www.test.dao; import java.io.Serializable; import ja ...

  3. Mongodb~连接串的整理

    mongodb连接串可以分为普通开放的,带全局用户名和密码的,为指定数据库指定用户名密码的等. 普通开放连接 mongodb://localhost:27017 带全局用户密码的 mongodb:// ...

  4. gcc标准,c++中的inline

    1. GCC的inlinegcc对C语言的inline做了自己的扩展,其行为与C99标准中的inline有较大的不同. 1.1. static inlineGCC的static inline定义很容易 ...

  5. vue学习笔记 vue安装

    一.安装步骤:(用cmd命令用管理身份安装比较顺利) 1.安装node,安装后可以输入npm -v 查看版本,升级npm可用 cnpm install npm -g 2.安装vue 输入cnpm in ...

  6. js 去掉字符串前后空格5种方法

    第一种:循环检查替换 //供使用者调用 function trim(s){ return trimRight(trimLeft(s)); } //去掉左边的空白 function trimLeft(s ...

  7. Spring课程 Spring入门篇 1-2Spring简介

    课程链接: 1 Spring是什么? 2 为什么是Spring 3 Spring的作用: 4 适用范围 1 Spring是什么? a 开源框架 b 轻量级的控制反转(Ioc)和面向切面编程(AOP)的 ...

  8. python复习目录

    目录 一.计算机基础 二.python 2.1初始python 2.1 python基础1 2.2 python基础2 三.函数 3.1 函数进阶 3.2 函数之装饰器 3.3 递归函数之二分查找 3 ...

  9. (C# 基础) 类访问修饰符

    C# 中有5个权限修饰符,用于控制对对象的访问权限. 1. public:   访问不受限制. namespace, enum成员,interface成员 隐式的具有public 修饰符,不能在显式添 ...

  10. Python开发环境Wing IDE如何使用GTK和PyGObject

    Wing IDE是一个集成开发环境,可用于编辑.测试和调试使用PyGObject为GTK编写的Python代码.Wing IDE提供自动完成.调用提示.一个强大的调试器,以及许多其他功能,可帮助用户编 ...