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 ...
随机推荐
- GENIL_BOL_BROWSER 中显示的Object Name 是root object的名字
EMPLOYEE 是root object 的名字. 2: dynamic query parameters 对应于:srch_attr. Each BOL object appears in a t ...
- node—基础命令
1.安装node,在任意文件夹下按shift键选中“在此处打开PowerShell窗口”或者直接在开始菜单输入cmd启动 2.常用命令: c: 如果我们想访问c盘,那么我们需要在命令行中输入c:就行了 ...
- MySQL 基础 简单操作
一.数据库基础 什么是数据库 数据库:保存有组织的数据的容器(通常是一个文件或一组文件). 表:是一种结构化的文件,可以用来存储数据(类似Excel表).数据库就是由成千上万个表组成. 什么事SQL ...
- [Guitar self-practising] 【吉他练习王-节奏练习】曲目1 基本扫弦节奏练习
这本书来自吉他练习王-节奏练习, 大家可以自行到网上搜搜电子版看看. 扫弦练习: 将左手轻轻靠着琴弦, mute琴弦.右手拿上拨片, 节拍器60,左脚踏着节拍练习. note: 注意 “轻”(幅度轻, ...
- GCC 用户态&内核态 Makefile
转了一圈,今天再次回到C 网上一篇博文,个人感觉良心作品,故而拿来重新实现一遍,原作者原文有问题,我这里把他打通了 一.GCC Makefile //hello.c #include <stdi ...
- iOS UI进阶-1.0 Quartz2D
概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...
- (已解决) eclipse提示报错"serializing cdt project settings"解决方案
最近在使用eclipse作为cocos2dx的交叉编译工具.但是某天突然相关工程打不开,打开eclipse直接提示 “serializing cdt project settings” has enc ...
- 26.如何获得select被选中option的value和text
如何获得select被选中option的value和text 一:JavaScript原生的方法 1:拿到select对象: var myselect=document.getElementById( ...
- OBV15 案例5,上M10拉高出货
- sqli-labs(九)_COOKIE处注入
第二十关: 这关是一个Cookie处的注入,输入正确的账号密码后,会跳到index.php页面,如下图 这个时候再访问登陆页面的时候http://localhost/sqli-labs-master/ ...