这个有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. NDK jni 加载静态库

    加载静态库到android,静态库的提供方式有2种, a. 通过源文件来编译静态库 b. 加载已经编译好的静态库 首先我们来看,通过源文件来编译静态库,工程目录如下 第一步:我们来看我们的jni目录, ...

  2. 村田噪声抑制基础教程-第一章 需要EMI静噪滤波器的原因

    1-1. 简介 EMI静噪滤波器 (EMIFIL®) 是为电子设备提供电磁噪声抑制的电子元件,配合屏蔽罩和其他保护装置一起使用.这种滤波器仅从通过连线传导的电流中提取并移除引起电磁噪声的元件.第1章说 ...

  3. 解决Win7下运行php Composer出现SSL报错的问题

    以前都在linux环境使用php composer.今天尝试在win7下运行composer却出现SSL报错: D:\data\www\mmoyu\symapp>php -f %phprc%\c ...

  4. 简单的优化处理 By LINQ TO SQL

    最近在做关于新浪微博授权的一些minisite,数据库并不复杂,所以在数据打交道这块采用了linqtosql,开发起来更快更简单...但是随着用户访问逐渐增多,用户上传的图片也越来越多,因为首页是一个 ...

  5. CPU原理

    cpu map 1.CPU的整体架构: 2.从CPU向内存 3.CPU和内存的关系图 4.CPU指令集 5.A+B 6.结果输入寄存器 7.寄存器中的临时存储,用来暂存B 8.将B传入寄存器 9.A会 ...

  6. python之域与属性

    python, javascript中域与属性是二个不同的概念, 域就是变量, 而属性则是符合某些约束, 例如getter, setter...等的特殊"变量". python中使 ...

  7. centos 格式化硬盘并挂载,添加重启后生效

    [root@cloud /]# passwd 更改用户 root 的密码 . 新的 密码: 重新输入新的 密码: passwd: 所有的身份验证令牌已经成功更新. [root@cloud /]# fd ...

  8. matlab实现插值法sin函数

    插值法实现sin函数: %calculate and print the sine function %input: x %output: sin(x) similar function y = si ...

  9. sky A800s手机恢复出厂设置操作

    关机的情况下,手指一起按这3个按键(音量下键+搜索键+开机键)看到SKY标志后即可松手,就可以进入恢复界面恢复界面操作方法:音量上下键为上下移动,关机键为选择.选择 wipe data/factory ...

  10. 20145120 《Java程序设计》第5周学习总结

    20145120 <Java程序设计>第5周学习总结 教材学习内容总结 try和catch语法,如果被try{}的语句出现了catch()的问题就执行catch{}的语句. 错误的对象都继 ...