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

Mysterious Bacteria

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

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
-4 Output: Case 1: 1
Case 2: 31
Case 3: 5
Case 4: 5
Case 5: 6
Case 6: 3
Case 7: 2
Case 8: 1
*/
 

LightOJ 1220 Mysterious Bacteria(唯一分解定理 + 素数筛选)的更多相关文章

  1. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  2. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  3. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  4. LightOJ-1220 Mysterious Bacteria 唯一分解定理 带条件的最大公因数

    题目链接:https://cn.vjudge.net/problem/LightOJ-1220 题意 给x=y^p,问p最大多少 注意x可能负数 思路 唯一分解定理,求各素因数指数的GCD 注意负数的 ...

  5. LightOj 1220 - Mysterious Bacteria (分解质因子x=b^p 中的 x 求最大的 p)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1220 题意:已知 x=bp 中的 x 求最大的 p,其中 x b p 都为整数 x = ...

  6. LightOj 1220 Mysterious Bacteria

    题目大意: 给出一个x,求满足x = b^p,p最大是多少? 解题思路: x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en. p = gcd (e1,e ...

  7. LightOJ 1220 Mysterious Bacteria 水题

    暴力就行了,找出素因子,正的最多是30,然后负的最多是31(这一点wa了一次) #include <cstdio> #include <iostream> #include & ...

  8. LightOj 1197 Help Hanzo (区间素数筛选)

    题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围 ...

  9. LightOJ 1197 Help Hanzo(区间素数筛选)

    E - Help Hanzo Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. 转载spring restemplate

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

  2. 新的方法(Set<T>)实现mvc的crud

    model层的属性为: public partial class UserInfo { public int Uid { get; set; } public string UName { get; ...

  3. 关于eclipse open call hierarchy功能的一个细节

    这个功能对应的快捷键是ctrl alt H,大家应该都很熟悉了.默认是查找这个方法的被调用堆栈.90%的人应该也是习惯这个默认的功能的,也基本无视它的另一个功能. 昨天重启eclipe之后,我的ecl ...

  4. 30.概述strust2中的拦截器

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 拦截器是Struts2框架的核心,它主要完成解析请求参数.将请求参数赋值给A ...

  5. NotePad++替换行前、行后空格,替换空行

    用 Notepad++ 打开,把每一个将要放在表中单元格的内容放一行(注: ^ 代表行首 $ 代表行尾) 去除行尾空格和空白行:按CTRL+H 选择正则表达式– 查找目标:\s+$ 替换为空 去除行首 ...

  6. shell如何传递变量到另一个脚本文件中

    http://www.jbxue.com/article/shell/20707.html本文介绍了shell脚本传递变量到另一个脚本文件中的方法,在脚本中调用另一脚本,即创建了一个子进程,感兴趣的朋 ...

  7. 6-关于#include<bits/stdc++.h>

    万能头文件#include<bits/stdc++.h> (转载)   最近在打cf时赛后翻阅别人的代码总是会发现一个陌生而奇怪的头文件#include<bits/stdc++.h& ...

  8. [mongoDB] mongoDb

    mongodb memcached redis        kv数据库(key/value) mongodb 文档数据库,存储的是文档(Bson->json的二进制化). 特点:内部执行引擎为 ...

  9. Java 设计模式系列(十五)迭代器模式(Iterator)

    Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...

  10. 初识STM32

    1.什么是STM32 A.ST是意法半导体,一个公司名,即SOC厂商,生产芯片的厂商.ARM公司是IP厂商,即只生产内核的厂商. B.M-Microelectronics的缩写,表示微控制器,大家注意 ...