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 ...
随机推荐
- tp5Auth权限实现
原文地址:https://blog.csdn.net/qq_33257081/article/details/79137190 下面本人为大家讲解一下如何实现auth权限, 第一步,新建Auth.ph ...
- [django]django缓存
发现搞了全局缓存后,刷新得不到最新数据了. 还好有过期时间 redis常用: https://www.cnblogs.com/fansik/p/5483060.html django-redis缓存: ...
- 多态使用时,父类多态时需要使用子类特有对象。需要判断 就使用instanceof
instanceof:通常在向下转型前用于健壮性的判断,判断是符合哪一个子类对象 package Polymorphic; public class TestPolymorphic { public ...
- Django-分页、中间件和请求的声明周期
一.分页 相关连接:https://www.cnblogs.com/kongzhagen/p/6640975.html 一.Django的分页器(paginator) 1.view.py 视图 fro ...
- 对接口(interface)的思考
接口,java中用关键字interface定义.今天学习java基础API时,发现一个特点:每个包中都定义了一堆的接口,感觉有马克思主义中提到的“上层建筑”的味道.接口就是为需要实现的功能定一个基调, ...
- apache mod_python 安装
环境:Linux 2.6.32-431.23.3.el6.i686 1.安装python .tgz ./configure --prefix=/usr/local/services/Python- ...
- Nodejs基础(5-6)HTTP概念进阶
1.什么是回调? 是异步编程最基本的方法,对于nodejs来说需要按顺序执行异步逻辑的时候一般采用后续传递的方式,也就是将后续逻辑封装在回调函数中作为起始函数的参数逐层去嵌套.通过这种方式来让程序按照 ...
- iOS 新浪微博-2.0 搜索框/标题带箭头/下拉菜单
不管是搜索框还是下拉菜单,我们都需要对背景的图片进行拉伸.定义一个Category分类对图片进行操作. UIImage+Effect.h #import <UIKit/UIKit.h> @ ...
- Cocos2d-JS studio基础控件的使用
在studio里把几个基础控件往场景文件一拖,然后导出json格式的资源文件 逻辑代码如下: 1 var HelloWorldLayer = cc.Layer.extend({ 2 sprite:nu ...
- Apache Storm Installation
安装的过程参照此处的过程介绍(https://www.tutorialspoint.com/apache_storm/apache_storm_installation.htm) 安装的过程要安装3个 ...