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

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

时间: 2024-10-19 13:10:57

UVA 10622 - Perfect P-th Powers(数论)的相关文章

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

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int find(int n) { for (int i = 2 ; i < 50000 ; ++ i) { if (pow(0.0+i, (int)(log10

UVA 10622 Perfect P-th Powers

https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include<cmath> #include<cstdio> #include<algorithm> #define N 65550 using namespace std; int gcd(int a,int b) { return !b ? a : gcd(b,a%b); } int

UVA 10548 - Find the Right Changes(数论)

UVA 10548 - Find the Right Changes 题目链接 题意:给定a,b,c,表示货物的价值,求由A货物和B货物组成C货物有几种方法,判断有无解和是否有无限多种 思路:扩展欧几里得求通解,去计算上限和下限就能判断 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const long l

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n

UVA - 1218 Perfect Service(树形dp)

题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连接且仅仅能连接一台server(不包含作为server的电脑).求最少须要多少台电脑作为server. 思路 典型的树形dp问题,那么我们来建立模型. d(u,0):u是server,孩子是不是server均可 d(u,1):u不是server,u的父亲是server,u的孩子不能是server d(u,2)

UVA 11246 - K-Multiple Free set(数论推理)

UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合,求一个子集合,使得元素个数最多,并且不存在有两个元素x1 * k = x2,求出最多的元素个数是多少 思路:推理一下, 一开始n个 先要删除k倍的,删除为{k, 2k, 3k, 4k, 5k, 6k...},会删掉多余的k^2,因此在加回k^2倍的数 然后现在集合中会出现情况的只有k^2的倍数,因此对k^2倍的数字看成一个新集合反复做这个操作即可,因此最后答案为n - n / k + n /

uva 10773 - Back to Intermediate Math(数论)

题目链接:uva 10773 - Back to Intermediate Math 题目大意:有一天河,宽d,水流速度v,船速u,问说垂直过河和最快过河的时间差,如果不能过河输出"can't determine". 解题思路:将u的速度分解成水平方向和竖直方向的两个速度,使水平方向速度恰好为v,船即可垂直过河,速度为竖直方向速度. #include <cstdio> #include <cstring> #include <cmath> const

uva 12119 - The Bells are Ringing(数论+枚举)

题目链接:uva 12119 - The Bells are Ringing 题目大意:有三个钟,分别间隔t1,t2,t3秒响一次,0时刻同时响,给定M,问有没又满足的三个数,最小公倍数为M.并且t3-t1<=25 解题思路:因为M为t1,t2,t3的最小公倍数,所以ti一定为M的因子,所以只要枚举因子判断即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace st