码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10622 - Perfect P-th Powers(数论)

时间:2014-06-24 23:30:45      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

UVA 10622 - Perfect P-th Powers

题目链接

题意:求n转化为b^p最大的p值

思路:对n分解质因子,然后取所有质因子个数的gcd就是答案,但是这题有个坑啊,就是输入的可以是负数,负数的情况比较特殊,p只能为奇数,这时候是要把答案不断除2除到为奇数即可。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>

long long n;
int prime[333333], vis[333333], m = 0;
int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

int solve() {
	long long nn = n;
	if (n < 0) n = -n;
	int ans = 0;
	for (int i = 0; i < m && prime[i] <= n; i++) {
		int count = 0;
		while (n % prime[i] == 0) {
			count++;
			n /= prime[i];
  		}
  		ans = gcd(ans, count);
 	}
  	if (ans == 0) ans = 1;
 	if (nn < 0) {
 		while (ans % 2 == 0) {
  			ans /= 2;		
		}
  	}
 	return ans;
}

int main() {
	for (int i = 2; i < 333333; i++) {
		if (vis[i]) continue;
		prime[m++] = i;
		for (int j = i; j < 333333; j += i) {
			vis[j] = 1;
		}
 	}
	while (~scanf("%lld", &n) && n) {
		printf("%d\n",  solve());
 	}
	return 0;
}


UVA 10622 - Perfect P-th Powers(数论),布布扣,bubuko.com

UVA 10622 - Perfect P-th Powers(数论)

标签:style   class   blog   code   http   tar   

原文地址:http://blog.csdn.net/accelerator_/article/details/33361983

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!