LightOJ 1220

来源:互联网 发布:免费chinanet软件2017 编辑:程序博客网 时间:2024/06/14 13:56

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;}

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

Input13171073741824252147483647-214748364832-3264-644-4324-46656OutputCase 1: 1Case 2: 30Case 3: 2Case 4: 1Case 5: 31Case 6: 5Case 7: 5Case 8: 6Case 9: 3Case 10: 2Case 11: 1Case 12: 2Case 13: 3Input7-161664-64151736OutputCase 1: 1Case 2: 4Case 3: 6Case 4: 3Case 5: 1Case 6: 1Case 7: 2Input10-1048576-167772161048576167772161024102310112123OutputCase 1: 5Case 2: 3Case 3: 20Case 4: 24Case 5: 10Case 6: 1Case 7: 1Case 8: 2Case 9: 1Case 10: 1
  • 如有错误或遗漏,请私聊下UP,thx
原创粉丝点击