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 ...
随机推荐
- spark.yarn.jar和spark.yarn.archive的使用
启动Spark任务时,在没有配置spark.yarn.archive或者spark.yarn.jars时, 会看到不停地上传jar非常耗时:使用spark.yarn.archive可以大大地减少任务的 ...
- solr整合spring
<!-- 单机版solr服务配置 --> <!-- <bean id="httpSolrServer" class="org.apache.sol ...
- 调用另一个文件的python代码【转载】
转自:https://blog.csdn.net/u010412719/article/details/47089883 例如我们有a.py和b.py两个文件,当我们需要在b.py文件中应用a.py中 ...
- 【LeetCode每天一题】Two Sum(两数之和)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 2019.03.21 admin
1.新建文件创建一个应用类 stu Python manage.py startapp stu 2,创建应用类 记得在setting中的installed_apps中添加应用 student/mode ...
- [LeetCode] 414. Third Maximum Number_Easy
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- trie字典树
---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 #include <bits/stdc++.h> usin ...
- JAVA代码MD5加密方法
PwdEncoder.java 接口类 package com.common.security.encoder; /** * 密码加密接口 */ public interface PwdEncoder ...
- cocos2dx C++为Sprite添加触摸事件监听器
1.首先头文件定义事件处理的函数原型 private: bool onTouchBegan(Touch* tTouch,Event* eEvent);//手指按下事件 void onTouchMove ...
- Windows(华硕/联想)笔记本上安装黑苹果与win双系统教程
声明:电脑小白者请谨慎安装,如有需要可私聊或留言提供安装工具 首先说明:Windows PC的文件操作系统也就是磁盘格式是FAT32或 NTFS ,而 Mac 的文件操作系统格式是 HFS ,所以这时 ...