这个有2种方法。

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

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int fun(double n)
{
int p=,f=;
if(n<)
{
p=;
f=;
n=-n;
}
for(;p>=;p-=f)
{
double a=pow(n,1.0/p);
long t1=a;
long t2=a+;
if(a-t1<=1e-||t2-a<=1e-)
break;
}
return p;
}
int main()
{
int x,p;
while(cin>>x&&x)
{
p=fun(x);
cout<<p<<endl;
}
return ;
}

第二种方法就是分解素数因子,求出因子的指数的最大公约数就是答案了,但是我这测试没错,提交超时……

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int prime[],a[];
void init()
{
int i,j,k=;
for(i=;i<=;i++)
{
if(!a[i])
{
prime[k++]=i;
for(j=i+i;j<=;j+=i)
a[j]=;
}
}
}
int gcd(int a,int b)
{
int c;
if(a<b) swap(a,b);
while(b)
{
c=a;
a=b;
b=c%b;
}
return a;
}
int main()
{
int n,i,j;
bool flag;
init();
while(scanf("%d",&n),n)
{
flag=;
if(n<)
{
flag=;
n=-n;
}
vector<int> key;
for(i=;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
int c=;
for(;n%prime[i]==;n/=prime[i],c++);
key.push_back(c);
}
}
if(n>) key.push_back();
int sum=key[];
for(i=;i<key.size();i++)
sum=gcd(sum,key[i]);
if(flag)
{
while(sum%==)
sum/=;
}
printf("%d\n",sum);
}
return ;
}

poj 1730 Perfect Pth Powers的更多相关文章

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

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

  2. POJ 1730 Perfect Pth Powers(唯一分解定理)

    http://poj.org/problem?id=1730 题意:给出一个n,a=b^p,求出最大p值. 思路: 首先利用唯一分解定理,把n写成若干个素数相乘的形势.接下来对于每个指数求最大公约数, ...

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

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

  4. Perfect Pth Powers poj1730

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

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

  6. 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, ...

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

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

  8. uva10622 Perfect P-th Powers

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

  9. UVA 10622 Perfect P-th Powers

    https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...

随机推荐

  1. <postfix邮件服务下mysql的升级>

    本片服务的环境的红帽的企业版6.5 的,6.3的测试可能会略有不一样,不过方法大致是一样的. 当前系统的postfix的版本为 postfix-2.6.6-2.2.el6_1.x86_64 我们要向使 ...

  2. linux远程客户端putty,xshell搭建注意事项——《视频》

    本视频放在云端,点击此链接即可播放:              http://www.tudou.com/programs/view/75QMh0-DQfA/               崇尚开源,热 ...

  3. JVM学习---JAVA内存

    一.JAVA运行时数据区域:JAVA中的运行时内存区域有的随着虚拟机进程的启动而存在,有的区域则是依赖用户线程的启动和结束而建立和销毁的.包括以下的几个区域. 图. JAVA虚拟机运行时数据区 1.程 ...

  4. C string 函数大全

    PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...

  5. oracle窗口函数中range interval的使用

    oracle窗口函数中range interval配合一般用来针对指定时间范围进行统计.其中range表示范围,between...and 表示之前的范围和之后的范围 , CURRENT ROW表示当 ...

  6. Android Studio快速开发之道(各种语法糖)

    现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. Post ...

  7. CentOS 6.3 配置 yum

    ContOS 配置yum:1.cd /etc/yum.repos.d2.创建个任意目录,将所有文件移动到创建的目录中,除了CentOS-Media.repo3.编辑CentOS-Media.repov ...

  8. mysql 查询

    查询数据:select s_name from student limit 1;//限制数量 select * from student where s_id in (select s_id from ...

  9. 【python】 开始第一个项目

    根据这篇文章开始上手 http://www.oschina.net/translate/the-flask-mega-tutorial-part-i-hello-world 再加点东西 如果你的环境是 ...

  10. 语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient)

    语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient) 原理 梅尔频率倒谱系数:一定程度上模拟了人耳对语音的处理特点 预加重:在语音信号中,高频部分的能 ...