LightOJ-1220 Mysterious Bacteria (素数打表+欧几里得算法+唯一分解定理)给出x,求x=a^p,最大的指数
題目大意:
x = b^p, x只有一個(gè)因子的p次冪構(gòu)成
如果24 = 2^3*3^1,p應(yīng)該是gcd(3, 1) = 1,即24 = 24^1
324 = 3^4*2^2=(3^2*2)^2,p應(yīng)該是gcd(4, 2) = 2,即324 = 18^2
所以p = gcd(x1, x2, x3, ... , xn){歐幾里得算法求取最大公約數(shù)};
*本題有一個(gè)坑,就是x可能為負(fù)數(shù),如果x為負(fù)數(shù)的話,x = b^q, q必須使奇數(shù),所以將x轉(zhuǎn)化為正數(shù)求得的解如果是偶數(shù)的話必須將其一直除2轉(zhuǎn)化為奇數(shù)
題目:
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?xdays. 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
AC代碼:
#include<iostream> #include<string.h> typedef long long ll; #include<stdio.h> using namespace std; #define M 1000010 int dp[M]; int book[M]; int t,k; ll m; void dfs() {k=0;memset(dp,0,sizeof(dp));memset(book,0,sizeof(book));for(int i=2; i<M; i++)/**因?yàn)槿魏我粋€(gè)整數(shù)都可以由一個(gè)素?cái)?shù)經(jīng)過(guò)乘法運(yùn)算得到,故可通過(guò)素?cái)?shù)打表的方式求得某數(shù)的唯一分解(得到冪次最大)*/if(!book[i]){dp[k++]=i;/*記錄素?cái)?shù)*/for(int j=2; i*j<M; j++)book[j*i]=1;} } int gcd(int a,int b) {return !b?a:gcd(b,a%b); } int main() {cin>>t;int tt=1;dfs();while(t--){int flag=0;cin>>m;if(m<0)/**本題的坑:m可能為負(fù)數(shù)*/{flag=1;m=-m;}int ans=0;for(int i=0;i<k&&dp[i]*dp[i]<=m;i++)/**care:remember停止條件為i<k&&dp[i]*dp[i]<=m,缺一不可*/{if(m%dp[i]==0){int a=0;while(m%dp[i]==0){m/=dp[i];/**唯一分解定理:直接對(duì)m的值進(jìn)行操作,得到x=a1^b1*a2^b2.....an^bn*/a++;}if(ans=0)ans=a;elseans=gcd(ans,a);/**歐幾里得算法:求得到的素?cái)?shù)的冪次進(jìn)行求取最大公約數(shù)操作*/}if(m==1)break;}if(m>1)/**則m為素?cái)?shù),因數(shù)只有1和自身*/ans=1;if(flag)/**填坑:若為負(fù)數(shù),冪次不可能為偶數(shù),故由最大公約數(shù)求出最大的奇數(shù)*/{while(ans%2==0)ans/=2;}printf("Case %d: %d\n",tt++,ans);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的LightOJ-1220 Mysterious Bacteria (素数打表+欧几里得算法+唯一分解定理)给出x,求x=a^p,最大的指数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 流放之路召唤师最强BD
- 下一篇: 拓展欧几里得小结(初级理解)