數論題集1-8-Mysterious Bacteria (篩法求素數+唯一分解定理)
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
Sample Input
3
17
1073741824
25
Sample Output
Case 1: 1
Case 2: 30
Case 3: 2
題意:言簡意賅!!!
就是給你一個數 N , N = x ^ p 求 p 的 最大值
emmm......一眼看起來好像挺好寫
唯一分解定理嘛 , N = x1^p1 * x2^p2 * x3^p3 *............*xn^pn
然後去找max(p1 , p2 , ...,pn)
真好 ,然而 題意當然不是這樣的。。。。。。
題目說求N = x ^ p 中 p 的最大值 , 也就是說只有一個質因子
比如 12 = 2^2 * 3 ^1 答案是2 ?no!!!!
12 = 12^1 答案是 1
還有就是N的值可能是負數~~只有負數的奇次方才能為負數,
so.......如果求出來的p是偶數的話 ,我們要不斷的除2 , 直到為奇數再輸出
注意!!!
N要開 long long 不然過不了
還有!!!
篩法求素數的時候,不要加sqrt()了 , 血的教訓啊~~
下面程式碼:
#include <bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
const int maxn = 1e5+10;
bool vis[maxn] = {0};
int prime[maxn];
int ret;
void IsPrime()
{
ret = 0;
vis[1] = 1;
for(int i = 2 ; i <= maxn ; i++)
{
if(vis[i] == 0)
{
prime[ret++] = i;
for(int j = 2 ; j * i < maxn ; j++)
{
vis[i*j] = 1;
}
}
}
}
signed main()
{
int t , ans;
ll n;
IsPrime();
cin >> t;
for(int k = 1 ; k <= t ; k++)
{
cin >> n;
int sum = 0 ;
int flag = 0;
if(n < 0)
{
n = -n;
flag = 1;
}
for(int i = 0 ; i < ret ; i++)
{
if(n % prime[i] == 0)
{
ans = 0;
while(n % prime[i] == 0)
{
ans++;
n /= prime[i];
}
if(sum == 0)
{
sum = ans;
}
else
{
sum = __gcd(sum , ans);
}
}
}
if(n > 1)
{
sum = 1;
}
if(flag == 1)
{
while(sum % 2 == 0)
{
sum = sum/2;
}
}
printf("Case %lld: %lld\n" , k , sum);
}
return 0;
}