poj 1730 Perfect Pth Powers
这个有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的更多相关文章
- POJ 1730 Perfect Pth Powers(暴力枚举)
题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...
- POJ 1730 Perfect Pth Powers(唯一分解定理)
http://poj.org/problem?id=1730 题意:给出一个n,a=b^p,求出最大p值. 思路: 首先利用唯一分解定理,把n写成若干个素数相乘的形势.接下来对于每个指数求最大公约数, ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
- [暑假集训--数论]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 ...
- 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, ...
- UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...
- uva10622 Perfect P-th Powers
留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...
- UVA 10622 Perfect P-th Powers
https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...
随机推荐
- “~/Views/Home/Text.aspx”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。
在MVC架构中使用aspx页面,需要在Text.aspx中开头加入如下代码: <%@ Page Language="C#" Inherits="System.Web ...
- Spark菜鸟学习营Day5 分布式程序开发
Spark菜鸟学习营Day5 分布式程序开发 这一章会和我们前面进行的需求分析进行呼应,完成程序的开发. 开发步骤 分布式系统开发是一个复杂的过程,对于复杂过程,我们需要分解为简单步骤的组合. 针对每 ...
- 在Centos7上安装漏洞扫描软件Nessus
本文摘要:简单叙述了在Centos7上安装Nessus扫描器的过程 Nessus 是目前全世界最多人使用的系统漏洞扫描与分析软件,Nessus的用户界面是基于Web界面来访问Nessus漏洞扫描器 ...
- 【转】Eazfuscator.NET 3.3中混淆化需要注意的一些问题
对于DLL,Eazfuscator.NET默认不会混淆化任何公共成员,因为类库的公共成员很有可能被外界调用,而对于EXE的程序集,所有类型都可能被混淆化.注意上面这句话有一个“可能”,因为Eazfus ...
- JS对Json对象Distinct
Json对象去重 今日有一个需求如下: 从数据库中取出数据源转化成json字符串绑定到隐藏域中,取出的json字符串如下: string data="[{"CompanyName& ...
- SQL Server 2014 Always on ON Microsoft Azure New Portal(1)
以前假如需要在Azure IaaS 创建的SQL Server AlwaysOn 需要参考以下的步骤 From the MVPs: SQL Server High Availability in Wi ...
- Zendframework 模块加载事件触发顺序。
模块加载时事件触发的时间顺序: 0.loadModules(ModuleEvent::EVENT_LOAD_MODULES) 1. loadModule.resolve(ModuleEvent::E ...
- FastLoad错误 — RDBMS error 2634
我们来看一下下面这条语句: BEGIN LOADING stu_flERRORFILES error_1, error_2; 如果此时已经存在error_1或error_2表,那么将会报错,信息如 ...
- hifi/ headphone test
https://www.youtube.com/watch?v=-r0gRjqN0N8 https://www.youtube.com/watch?v=sMh_zvCw6us
- inputstream与其他格式的转换
1.InputStream 转换成InputSource . InputStream inputStream = request.getInputStream(); InputSource input ...