您的位置:首页 > 其它

LightOJ 1220 - Mysterious Bacteria 【数论 - 预处理】

2017-12-12 22:05 489 查看

1220 - Mysterious Bacteria

Time Limit: 0.5 second(s) Memory Limit: 32 MB

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

Output for Sample Input

Case 1: 1

Case 2: 30

Case 3: 2

题意: 给你一个int范围内的数为n,问你写成 n=bp成立的情况下,p的最大取值

分析: 看清范围 -> int范围,包括负数和零,一开始根号的复杂度可以求出所有的质因数,但是考虑负数情况很多,懒得讨论,所以直接预处理了所有的数,因为int嘛,很小,但要考虑下特殊情况,这里边界很重要,一定要初始化下,还有就是当n == 0 或者是 1,-1时, 理论是无穷大,这里考虑为1,然后只需要选出最大幂数的即可

参考代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<pair<ll,int> > s;
const int INF_MAX = 0x7fffffff,INF_MIN = 0x80000000;

void init() {
ll MIN = INF_MIN;
ll MAX = INF_MAX;
for(ll i = -50000;i <= 50000;i++) {
if(i == -1 || i == 0 || i == 1) {
s.push_back(make_pair(i,1));
continue;
}
ll t = 1;
for(int j = 1;j < 33;j++) {
t *= i;
if(t <= MAX && t >= MIN) {
s.push_back(make_pair(t,j));
}
}
}
}

int main() {
init();
int T;cin>>T;
for(int t = 1;t <= T;t++) {
ll n;cin>>n;
if(n == 0) {
printf("Case %d: %d\n",t,1);
continue;
}
int res = 1;
bool flg = false;
for(int i = 0;i < s.size();i++) {
if(n == s[i].first) {
flg = true;
res = max(res,s[i].second);
}
}
printf("Case %d: %d\n",t,res);
}
return 0;
}


附: 几组样例需要的可以拿走

Input
13
17
1073741824
25
2147483647
-2147483648
32
-32
64
-64
4
-4
324
-46656
Output
Case 1: 1
Case 2: 30
Case 3: 2
Case 4: 1
Case 5: 31
Case 6: 5
Case 7: 5
Case 8: 6
Case 9: 3
Case 10: 2
Case 11: 1
Case 12: 2
Case 13: 3

Input
7
-16
16
64
-64
15
17
36
Output
Case 1: 1
Case 2: 4
Case 3: 6
Case 4: 3
Case 5: 1
Case 6: 1
Case 7: 2

Input
10
-1048576
-16777216
1048576
16777216
1024
1023
101
121
2
3
Output
Case 1: 5
Case 2: 3
Case 3: 20
Case 4: 24
Case 5: 10
Case 6: 1
Case 7: 1
Case 8: 2
Case 9: 1
Case 10: 1


如有错误或遗漏,请私聊下UP,thx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: