UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意:
对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值。
分析:
将x分解质因数,然后求所有指数的gcd即可。
对于负数还要再处理一下,负数求得的p必须是奇数才行。
#include <cstdio>
#include <cmath> const int maxn = ;
bool vis[maxn + ];
int prime[], cnt = ; void Init()
{
int m = sqrt(maxn + 0.5);
for(int i = ; i <= m; ++i) if(!vis[i])
for(int j = i * i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; ++i) if(!vis[i]) prime[cnt++] = i;
cnt--;
} int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
} int solve(int n)
{
int ans = ;
for(int i = ; i <= cnt; ++i)
{
int k = ;
while(n % prime[i] == )
{
k++;
n /= prime[i];
}
if(k)
{
if(k == ) return ;
ans = gcd(k, ans);
}
}
if(n > ) return ;
return ans;
} int main()
{
Init(); int n;
while(scanf("%d", &n) == && n)
{
int ans = solve(n < ? -n : n);
if(n < ) while((ans & ) == ) ans >>= ;
printf("%d\n", ans);
} return ;
}
代码君
UVa 10622 (gcd 分解质因数) Perfect P-th Powers的更多相关文章
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- 【基础数学】质数,约数,分解质因数,GCD,LCM
1.质数: 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数. 2.约数: 如 ...
- UVA 10622 Perfect P-th Powers
https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...
- 【20181027T1】洛阳怀【推结论+线性筛+分解质因数+GCD性质】
原题:CF402D [错解] 唔,先打个表看看 咦,没有坏质数好像就是质因数个数啊 那有坏质数呢? 好像变负数了 推出错误结论:f(x)=x的质因数个数,如果有个坏质数,就乘上-1 然后乱搞,起码花了 ...
- poj 1730Perfect Pth Powers(分解质因数)
id=1730">Perfect Pth Powers Time Li ...
- light oj 1236 分解质因数
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/H 题意:求满足1<=i<=j<=n ...
- hdu 5428 The Factor 分解质因数
The Factor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...
- uva10791 uva10780(分解质因数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- java求最大公约数(分解质因数)
下面是四种用java语言编程实现的求最大公约数的方法: package gcd; import java.util.ArrayList; import java.util.List; public c ...
随机推荐
- 【python】开始python之旅
上午开始抽空学习python,具体内容如下: (1)安装了python 2.7 (2)安装了notepad ++,安装它之前,在notepad++和Sublime Text之间纠结了一下,baidu了 ...
- fedora -- java多版本切换
一般java开发时会下载多个版本的SDK,所以需要多个版本中进行切换 1. 设置JAVA_HOME环境变量需要打开.bashrc文件 2. 安装时使用alternatives将不同版本的java连接到 ...
- mac上xampp配置
sudo su /Applications/XAMPP/xamppfiles/xampp security
- EXTJS 4.2 资料 控件之textfield文本框加事件的用法
{ xtype: "textfield", width: 100, id: "txtGroupName", name: "txtGroupName&q ...
- willMoveToParentViewController 与 didMoveToParentViewController
在iOS 5.0以前,我们在一个UIViewController中这样组织相关的UIView 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候被盖 ...
- Mac - 更新 Ruby
因为准备在项目中使用bootstrap,在安装bootstrap过程中提示需要Ruby的版本在1.9.2以上,而目前使用的Ruby版本是Mac系统自带的1.8.7.所以需要对Ruby进行升级.这里使用 ...
- 360随身wifi怎样购买?360随身wifi怎样预约?
---恢复内容开始--- 360随身wifi怎样购买 想要购买360随身Wifi,可以登录360随身Wifi的官网wifi.360.cn,或者直接登陆京东商城进行购买,售价为19.9元,分黑.白两色. ...
- 【线段树/数学/扩展欧几里得】 Bzoj 3913:奇数国
Description 在一片美丽的大陆上有100000个国家,记为1到100000.这里经济发达,有数不尽的账房,并且每个国家有一个银行.某大公司的领袖在这100000个银行开户时都存了3大洋,他惜 ...
- asp 文件上传(无组件上传)
文件1.上传界面文件 upload.htm<html><head><meta http-equiv="Content-Language" conten ...
- hdu 1085
额 母函数 #include <cstdio> #include <cstring> int a[3],b[3]= {1,2,5}; int c1[10001],c2[1 ...