http://poj.org/problem?id=1730

题意:
给出一个n,a=b^p,求出最大p值。

思路:

首先利用唯一分解定理,把n写成若干个素数相乘的形势。接下来对于每个指数求最大公约数,该公约数就是所能到达的最大p值。

有一点要注意的是如果n为负数的话,如果当前p值为偶数,就一直除2直到p为奇数。

 #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
using namespace std; const int maxn = ; long long n;
int vis[maxn];
int prime[];
int cnt; void get_prime()
{
cnt = ;
int m = sqrt(maxn + 0.5);
for (int i = ; i < maxn; i++)
{
if (!vis[i])
{
prime[cnt++] = i;
for (int j = i; j < maxn; j += i)
vis[j] = ;
}
}
} int gcd(int a, int b)
{
if (a < b) return gcd(b, a);
if (b == ) return a;
return gcd(b, a % b);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
get_prime();
while (~scanf("%lld", &n) && n)
{
int flag = ;
int ans = ;
if (n < )
{
n = -n;
flag = ;
}
int flag2 = ;
for (int i = ; i < cnt && n>; i++)
{
if (n%prime[i] == )
{
int ret = ;
while (n%prime[i] == )
{
n /= prime[i];
ret++;
}
ans = gcd(ans, ret);
}
}
if (n > ) ans = ;
if (flag)
{
while (ans % == ) ans /= ;
}
printf("%d\n", ans);
}
}

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

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

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

  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 1845 Sumdiv (整数唯一分解定理)

    题目链接 Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25841   Accepted: 6382 Desc ...

  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. nodejs 环境搭建

    一 下载nodejs 官网:http://nodejs.cn/ 有时官网有点慢,可以去其他地方下载 统一下载站:http://www.3987.com/xiazai/2/43/57188.html 二 ...

  2. CentOS oracle Client客户端安装

    CentOS客户端安装方法如下: 1.安装客户端 rpm -ivh /当前目录/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm rpm - ...

  3. 获得当前正在显示的activity的类名

    需要加一个权限: <uses-permission android:name="android.permission.GET_TASKS"/> ActivityMana ...

  4. JSP 通过Session和Cookie实现网站自动登录

    点记住密码 login.jsp String host = request.getServerName(); Cookie cookie = new Cookie("SESSION_LOGI ...

  5. R的grep和grepl

    grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, ...

  6. 最小生成树的变形(次小生成树hdu4081)

    hdu4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  7. java8新增的日期时间包

    Clock clock=Clock.systemUTC(); System.out.println("当前时刻为:"+clock.instant()); System.out.pr ...

  8. 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】

    链接:https://www.nowcoder.com/acm/contest/145/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  9. js 中 0 和 null 、"" Boolean 值关系

    在做字符串非空判断时,无意发现一个问题,记录下以便以后回顾. 问题描述:非空判断,只是校验传值的内容是否为"".null .undefined.当变量 赋值的字符串内容为 0,此时 ...

  10. php安装xmlwriter遇到报错及解决方法

    Q1:make的时候报' error: 'zend_class_entry' has no member named 'default_properties''错误, A:把 错误行C文件中defau ...