(LightOJ 1220)Mysterious Bacteria

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 exactlyx days. Now RC-01 produces exactlyp new deadly Bacteria wherex = bp (whereb, p are integers). More generally,x is a perfectpth power. Given the lifetimex 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 integerT (≤ 50), denoting the number of test cases.

   Each case starts with a line containing an integerx. You can assume thatx 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 integerp such thatx is a perfectpth power.

Sample Input

3

17

1073741824

25

Sample Output

Case 1: 1

Case 2: 30

Case 3: 2


题意:

给一个整数x,求满足x = bp(均为整数)条件的最大p值

则求出x=a1p1 *a2p2*a3p3……anpn

其中a1……an均为素数

然后求ans = gcd(p1,p2,……,pn);

有一个坑,就是n可能为负数,这时,ans必须为奇数……


Code

#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
         
         
#include 
         
         
           #include 
          
            #include 
           
             #include 
            
              #include 
              #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #define maxn (1000000 + 10) #define pi acos(-1.0) using namespace std; typedef long long int LLI; bool mark[maxn + 10]; LLI isprime[maxn + 10],p = 0; vector 
                  
                    Vec; LLI gcd(LLI a, LLI b) { return b == 0 ? a : gcd(b, a % b); } void sieve_prime() { memset(mark, true, sizeof(mark)); mark[0] = mark[1] = false; for (LLI i = 2; i * i <= maxn; i++) if (mark[i]) for (LLI j = i * i; j < maxn; j = j + i) mark[j] = false; } bool Solve(LLI n) { for(LLI i = 0; i < p && isprime[i] * isprime[i] <= n; i ++) { LLI temp = 0; while(n % isprime[i] == 0) { temp ++; n = n / isprime[i]; } if(temp != 0) Vec.push_back(temp); } if(n > 1) return false; else return true; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); sieve_prime(); for(LLI i = 0; i <= maxn; i ++) if(mark[i]) isprime[p ++] = i; LLI t,n; scanf("%lld",&t); for(LLI Case = 1; Case <= t; Case ++) { int op = 0; scanf("%lld",&n); if(n < 0) n = -n,op = 1; bool flag = Solve(n); if(!flag) printf("Case %lld: 1\n",Case); else { LLI ans = Vec[0]; for(LLI i = 1; i < Vec.size(); i ++) ans = gcd(ans,Vec[i]); if(op == 1) while(ans % 2 == 0) ans = ans / 2; printf("Case %lld: %lld\n",Case,ans); } Vec.clear(); } return 0; } 
                   
                  
                 
                
               
             
            
           
         
        
        
       
       
      
      
     
     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值