首页 技术 正文
技术 2022年11月9日
0 收藏 915 点赞 3,199 浏览 2069 个字

http://lightoj.com/volume_showproblem.php?problem=1220

Mysterious Bacteria

Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1220

Description

Dr. Mob has just discovered a Deathly Bacteria. He named it RC-01. RC-01 has a very strange reproduction system. RC-01 lives exactly x days. Now RC-01 produces exactly p new deadly Bacteria where x = bp (where b, p are integers). More generally, x is a perfect pth power. Given the lifetime x of a mother RC-01 you are to determine the maximum number of new RC-01 which can be produced by the mother RC-01.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a line containing an integer x. You can assume that x will have magnitude at least 2 and be within the range of a 32 bit signed integer.

Output

For each case, print the case number and the largest integer p such that x is a perfect pth power.

Sample Input

3

17

1073741824

25

Sample Output

Case 1: 1

Case 2: 30

Case 3: 2

题目大意:

给你一个数x = b^p,求p的最大值

x = p1^x1*p2^x2*p3^x3*…*ps^xs

开始我以为是找x1、x2、… 、xs中的最大值,后来发现想错了,x = b^p, x只有一个因子的p次幂构成

如果x = 12 = 2^2*3^1,要让x = b^p,及12应该是12 = 12^1

所以p = gcd(x1, x2, x3, … , xs);

比如:24 = 2^3*3^1,p应该是gcd(3, 1) = 1,即24 = 24^1

324 = 3^4*2^2,p应该是gcd(4, 2) = 2,即324 = 18^2

本题有一个坑,就是x可能为负数,如果x为负数的话,x = b^q, q必须使奇数,所以将x转化为正数求得的解如果是偶数的话必须将其一直除2转化为奇数

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>using namespace std;const int N = 1e5 +;
const int INF = 0x3f3f3f3f;
typedef long long ll;int prime[N], k;
bool Isprime[N];void Prime()
{
k = ;
memset(Isprime, true, sizeof(Isprime));
prime[] = false;
for(int i = ; i < N ; i++)
{
if(Isprime[i])
{
prime[k++] = i;
for(int j = i ; 1LL * i * j < N ; j++)
Isprime[i * j] = false;
}
}
}int gcd(int a, int b)
{
return a % b == ? b : gcd(b, a % b);
}int main()
{
int t, p = ;
ll n;//n要用long long 定义,如果n是负数的话会超时
Prime();
scanf("%d", &t);
while(t--)
{
p++;
scanf("%lld", &n);
int f = ; if(n < )
{
n = - n;//int定义n这儿会卡住半天出不来,就会超时,为什么这样我也不知道
f = ;
}
int x, ans = ;
for(int i = ; i < k && prime[i] * prime[i] <= n ; i++)
{
if(n % prime[i] == )
{
x = ;
while(n % prime[i] == )
{
x++;
n /= prime[i];
}
if(ans == )
ans = x;
else
ans = gcd(ans, x);
}
}
if(n > )
ans = gcd(ans, );
if(f == )
{
if(ans % == )
ans = ;
}
printf("Case %d: %d\n", p, ans);
}
return ;
}
/*
8
2147483647
-2147483648
32
-32
64
-64
4
-4Output:Case 1: 1
Case 2: 31
Case 3: 5
Case 4: 5
Case 5: 6
Case 6: 3
Case 7: 2
Case 8: 1
*/
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905