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. zookeeper开源客户端curator

    zookeeper的原生api相对来说比较繁琐,比如:对节点添加监听事件,当监听触发后,我们需要再次手动添加监听,否则监听只生效一次:再比如,断线重连也需要我们手动代码来判断处理等等.对于curato ...

  2. ubuntu使用ppa源安装最新版本的git

    国内也有ubuntu镜像源.但是里面的git都是1.9版本.最新的已经是2.3了 1 首先使用将ppa源加入Ubuntu,交大家 sudo add-apt-repository ppa:pdoes/p ...

  3. php函数内不能访问函数外的变量原因

    执行如下代码: $i = 10; function my(){ echo $i; } my(); xdebug显示是:Undefined variable 以前也知道有这个问题存在,即函数内部不可以访 ...

  4. CSS零碎知识点

    一.zoom:1 IE专有属性 可以设置或检索对象的缩放比例 触发IE的layout属性 清除浮动:overflow:hidden;zoom:1; 但要尽量比较使用这种方法 zoom:1;可以拯救IE ...

  5. Java学习笔记--继承和多态(中)

    1.通过继承来开发超类(superclass) 2.使用super 关键词唤起超类的构造方法 3.在超类中覆盖方法 4.区分override和overload 5.在Object类中探索toStrin ...

  6. ArrayList、Vector、HashMap、HashSet

    1. list  和 set 的有序无序是否可重复 List是有序可重复的: Set是无序不可重复的: 2.ArrayList.Vector.HashMap.HashSet的默认初始容量.加载因子.扩 ...

  7. 基于表单布局:分析过时的table结构与当下的div结构

    一些话在前面 最近做了百度前端学院一个小任务,其中涉及到表单布局的问题, 它要处理的布局问题:左边的标签要右对齐,右边的输入框.单选按钮等要实现左对齐. 从开始入门就被告知table布局已经过时了,当 ...

  8. 转:Oracle优化总结

    本文主要从大型数据库ORACLE环境四个不同级别的调整分析入手,分析ORACLE的系统结构和工作机理,从九个不同方面较全面地总结了 ORACLE数据库的优化调整方案. 关键词 ORACLE数据库 环境 ...

  9. 工作流常使用API

    记录实际开发中常使用到的API CreateProcess 在工作流开始之前,创建一个新的工作流 Wf_engine.CreateProcess (itemtype in varchar2,    - ...

  10. Python扩展包

    Python扩展包 1.NumPy NumPy提供了多种python本身不支持的多种集合,有list.ndarray和ufunc. list 更加灵活的数组,支持多维,数据可不同型,存储数量远大于ar ...