UVA 10622 Perfect P-th Powers (唯一分解定理 + GCD)
来源:互联网 发布:华为总是停用移动数据 编辑:程序博客网 时间:2024/06/05 20:31
题意:找所给定的一个数变成x的p次方的形式, 寻找P最大是多少。
解题思路:先使用唯一分解定理分解素因子, 然后再求所有素因子在一起的GCD,注意负数的P不能为偶数只能为奇数。边分解边求GCD, 否则可能超时。
We say that x is a perfect square if, for some integer b, x = b
2
.
Similarly, x is a perfect cube if, for some integer b, x = b
3
.
More generally, x is a perfect pth power if, for some integer b,
x = b
p
. Given an integer x you are to determine the largest
p such that x is a perfect pth power.
Input
Each test case is given by a line of input containing x. The
value of x will have magnitude at least 2 and be within the
range of a (32-bit) int in C, C++, and Java. A line containing
`0' follows the last test case.
Output
For each test case, output a line giving the largest integer p
such that x is a perfect pth power.
Sample Input
17
1073741824
25
0
Sample Output
1
30
2
#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<cctype>#include<list>#include<iostream>#include<map>#include<queue>#include<set>#include<stack>#include<vector>using namespace std;#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)int buf[10];inline long long read(){ long long x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f;}inline void writenum(int i){ int p = 0; if(i == 0) p++; else while(i) { buf[p++] = i % 10; i /= 10; } for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);}/**************************************************************/#define MAX_N 1000000const int INF = 0x3f3f3f3f;int isprime[MAX_N];int prime[MAX_N];int cnt = 0;int flag = 0;void make_prime(){ cnt = 0; memset(isprime, 1, sizeof(isprime)); isprime[1] = 0; for(long long i = 2 ; i < MAX_N ; i++) { if(isprime[i]) { prime[cnt++] = i; for(long long j = i * i ; j < MAX_N; j += i) { isprime[j] = 0; } } }}long long gcd(long long a, long long b){ return b ? gcd(b, a % b) : a;}long long f(long long n){ long long ret = 0; for(int i = 0 ; i < cnt && prime[i] <= n ; i++) { long long cnt = 0; while(n % prime[i] == 0) { cnt++; n /= prime[i]; } ret = gcd(cnt, ret); } if(ret == 0) return 1; if(flag) { while(ret % 2 == 0) { ret /= 2; } } return ret;}int main(){// freopen("in.txt", "r", stdin);// freopen("out1.txt", "w", stdout); make_prime(); long long n; while(~scanf("%lld", &n) && n) { flag = 0; if(n < 0) { flag = 1; n = -n; } long long ans = f(n); printf("%lld\n", ans); } return 0;}
- UVA 10622 Perfect P-th Powers (唯一分解定理 + GCD)
- 10622 - Perfect P-th Powers(唯一分解定律)
- UVa 10622 - Perfect P-th Powers
- UVA 10622 - Perfect P-th Powers(数论)
- uva 10622 - Perfect P-th Powers(数论)
- UVA - 10622 Perfect P-th Powers
- UVA 10622 - Perfect P-th Powers
- UVa 10622 - Perfect P-th Powers
- UVa 10622 - Perfect P-th Powers
- UVA 10622 Perfect P-th Powers(数论)
- UVa 10622 - Perfect P-th Powers(数论)
- Perfect P-th Powers UVA
- uva 10622——Perfect P-th Powers
- UVa 10622 - Perfect P-th Powers(对数+快速幂)
- Uva 10622 Perfect Pth Power 唯一分解定理
- UVa10622 Perfect P-th Powers
- GCD Again 唯一分解定理
- uva 10375 唯一分解定理
- SSH框架+EL表达式—jsp跨界面传值显示
- SQLServer修改密码
- spring boot实战(第十四篇)整合RabbitMQ源码分析前言
- java中对数组转置
- CentOS 6.5系统中iptables防火墙开放端口80 3306 22端口
- UVA 10622 Perfect P-th Powers (唯一分解定理 + GCD)
- java_笔记-10
- 添加到UIView上的UIButton点击没反应 UITest时源文件找不到解决方案
- 排序之Java的Sort函数
- 【数据结构与算法分析】1.3 用printOut函数输出任意实数
- 什么是版本控制系统(VCS)?
- MySQL5.6 Using a password on the command line interface can be insecure解决方法;
- JS高级
- 软件测试面试题