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. linux漏洞扫描工具【lynis】

    Lynis是一款Unix系统的安全审计以及加固工具,能够进行深层次的安全扫描,其目的是检测潜在的时间并对未来的系统加固提供建议.这款软件会扫描一般系统信息,脆弱软件包以及潜在的错误配置. 特征: 漏洞 ...

  2. 170711、Linux下搭建MySQL集群

    一.MySQL集群简介 1.什么是MySQL集群 MySQL集群是一个无共享的(shared-nothing).分布式节点架构的存储方案,其目的是提供容错性和高性能. 数据更新使用读已提交隔离级别(r ...

  3. Webpack基础入门学习笔记

    # Webpack Project Build 1.创建一个项目目录文件夹 如:D:/demo 2.打开demo文件夹,按住Shift + 鼠标右键,选择[在此处打开命令窗口] 3.初始化npm,生成 ...

  4. CentOS中为新用户添加sudo权限

    1.切换成root权限 su root 2.查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 ls -l /etc/sudoers 3.如果是只读进行如下操作 chmod /etc/ ...

  5. cookie.setPath()的用法

    正常的cookie只能在一个应用中共享,即:一个cookie只能由创建它的应用获得. 可在同一应用服务器内共享cookie的方法:设置cookie.setPath("/");  ( ...

  6. Java中参数传递时值传递的机制分析

    参数传递是什么?      在C的函数或是JAVA的方法中,向一个函数或方法内部传递一个参数,比如:   void fun( int num ){     num+=2 ; }   int a = 3 ...

  7. 【Python】读取各种文档(txt、csv、excel、pdf)方法

    1.读取txt文件 注意事项: 1..txt文件同下方脚本所在的.py文件需要在同一个文件夹下 # coding=utf-8 txt读取 with open("1233.txt") ...

  8. 【Javascript】Windows下Node.js与npm的安装与配置

      1:先下载Node.js,网站https://nodejs.org/en/,左侧为稳定版,右侧为最新版,推荐稳定版 2:Node.js安装,运行下载后的.msi文件,一路下一步就可以了,我选择的安 ...

  9. 代码处理 iOS 的横竖屏旋转

    一.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation.UIInterfaceOrientation和UIInterfaceOrientationMask这三个枚举 ...

  10. xutil3 post 和 get请求

    https://i.cnblogs.com/EditPosts.aspx?postid=7001253 compile 'org.xutils:xutils:3.3.36' 注册xutil3 < ...