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, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth power.
Input
Output
| Sample Input 1 | Sample Output 1 |
|---|---|
17 |
1 |
题目的大概意思是——给出一个n,n=b^p,求出最大p值。(b未知)
思路:
首先利用唯一分解定理,把n写成若干个素数相乘的形式。接下来对于每个素数的指数求最大公约数,该公约数就是所能到达的最大p值。
有一点要注意的是如果n为负数的话,如果当前p值为偶数,就一直除2直到p为奇数。(被坑了。)
//Asimple
#include <bits/stdc++.h>
#define INF (1<<20)
#define mod 10007
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
using namespace std;
typedef long long ll;
const int maxn = ;
const double PI=atan(1.0)*;int n, m, num, res, ans, len, T, k;int a[maxn];
int pr[maxn]; int gcd(int a, int b) {
return b==?a:gcd(b, a%b);
} void solve() {
CLS(a, );
len = ;
for(int i=; i*i<=maxn; i++) {
if( !a[i] ) {
pr[len++] = i;
for(int j=i; j<maxn; j+=i)
a[j] = ;
}
}
} void input() {
solve();
while( cin >> n && n ) {
ans = ;
int f = ;
if( n < ) {
n = -n;
f = ;
}
for(int i=; i<len && n>; i++) {
if( n%pr[i]== ) {
res = ;
while( n%pr[i]== ) {
res ++;
n /=pr[i];
}
ans = gcd(ans, res);
}
}
if( n> ) ans = ;
if( f ) while( ans%== ) ans/=;
cout << ans << endl;
}
} int main(){
input();
return ;
}
Kattis之旅——Perfect Pth Powers的更多相关文章
- 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 ...
- POJ 1730 Perfect Pth Powers(暴力枚举)
题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...
- [暑假集训--数论]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 ...
- poj 1730 Perfect Pth Powers
这个有2种方法. 一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理…… #include<iostream> #incl ...
- 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& ...
- Perfect Pth Powers pku-1730(筛+合数分解)
题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26, 然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...
随机推荐
- log4j。日志输出
log4j.rootLogger = debug , stdout , D , E log4j.appender.stdout = org.apache.log4j.ConsoleAppender l ...
- http://lib.csdn.net/article/reactnative/40118
http://lib.csdn.net/article/reactnative/40118 ES6学习笔记(四)--数值与数组 作者:SirM2z 数值的扩展 Number.isFinite(), N ...
- sap QG3搜索
先创建一个QG3系统,创建一个用户. 1: 进入搜索模板 2: 选择软件组件,点击执行 3: 设置过滤条件. 4: 选择在哪一列 设置过滤条件. 5: 定义搜索值 6: 设置值 可以将搜索的结果删除. ...
- background-size的兼容性
做响应式布局的时候,如果有背景图,我们当然希望他能够全屏100%显示,这样显得页面非常的爆满,不过这又出现了一个问题,图片的尺寸多大合适呢,现在的浏览器分辨率参差不齐,对于Firefox等高级的浏览器 ...
- Python pickle 模块
转自:https://www.cnblogs.com/lincappu/p/8296078.html pickle可以存储的数据类型 所有python支持的原生类型:布尔值,整数,浮点数,复数,字符串 ...
- 【RPC】综述
RPC定义 RPC(Remote Procedure Call)全称远程过程调用,它指的是通过网络,我们可以实现客户端调用远程服务端的函数并得到返回结果.这个过程就像在本地电脑上运行该函数一样,只不过 ...
- SQL Server 2008 R2下载地址
MS 发布了最新的SQL Server 2008 R2,似乎早在4.19就提供了下载,不过才到昨天放出下载地址: 这个是试用版的下载,于正式版的区别就只在一个序列号! 而MSDN的版本则集成了安装序列 ...
- RF基础(一) RF内建函数库BuiltIn
Robot framework做为一个测试框架,并不是只能做selenium测试,是支持扩展的, 比如说,你引用requests库就可以做接口测试, 那么无论你用什么库 首先要了解, RF本身提供的内 ...
- JavaScript学习笔记之call和apply
前端的知识面太广了,想要记住所有知识点是不可能的,所以将这些学过的记录下来,随时都可以翻开来参考 1.call方法 调用一个对象的一个方法,call(this, arg1, arg2,argN);用来 ...
- MYSQLi数据访问分页查询
<?php //分页工具 直接加载使用 /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录 ...