题目链接:https://cn.vjudge.net/problem/LightOJ-1220

题意

给x=y^p,问p最大多少

注意x可能负数

思路

唯一分解定理,求各素因数指数的GCD

注意负数的情况,gcd一定要是奇数,这样就是最大奇GCD

只需每次求gcd后除2即可

提交过程

WA*2 负数问题
AC

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+20;
int factors[100][2], fsize, primes[maxn/10], psize;
bool isprime[maxn];
void initPrimes(void){
memset(isprime, true, sizeof(isprime));
isprime[0]=isprime[1]=false;
for (int i=2; i<=maxn; i++){
if(isprime[i]) primes[psize++]=i;
for (int j=0; j<psize && i*primes[j]<=maxn; j++){
isprime[primes[j]*i]=false;
if (i%primes[j]==0) break;
}
}
} void getFactors(long long n){
fsize=0;
// size of isprime can be sqrt(maxn)
for (int i=0; i<psize && primes[i]*primes[i]<=n; i++){
if (n%primes[i]==0){
factors[fsize][0]=primes[i];
factors[fsize][1]=0;
while (n%primes[i]==0) factors[fsize][1]++, n/=primes[i];
fsize++;
}
}
if (n>1){
factors[fsize][0]=n;
factors[fsize++][1]=1;
}
} long long gcd(long long a, long long b){
return (b==0)?a:gcd(b, a%b);
} int main(void){
int T, kase=0; initPrimes();
scanf("%d", &T);
while (T--){
long long num;
bool neg=false; scanf("%lld", &num);
if (num<0) num=-num, neg=true;
getFactors(num); long long exp=factors[0][1];
if (neg) while (exp && exp%2==0) exp/=2;
for (int i=1; i<fsize; i++){
exp=gcd(exp, factors[i][1]);
if (neg) while (exp && exp%2==0) exp/=2;
}
printf("Case %d: %lld\n", ++kase, exp);
} return 0;
}
Time Memory Length Lang Submitted
1224kB 1324 C++ 2018-07-30 18:00:17

LightOJ-1220 Mysterious Bacteria 唯一分解定理 带条件的最大公因数的更多相关文章

  1. LightOJ 1220 Mysterious Bacteria(唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1220 Mysterious Bacteria Time Limit:500MS     Memo ...

  2. LightOj 1220 - Mysterious Bacteria (分解质因子x=b^p 中的 x 求最大的 p)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1220 题意:已知 x=bp 中的 x 求最大的 p,其中 x b p 都为整数 x = ...

  3. LightOJ 1220 Mysterious Bacteria 水题

    暴力就行了,找出素因子,正的最多是30,然后负的最多是31(这一点wa了一次) #include <cstdio> #include <iostream> #include & ...

  4. LightOj 1220 Mysterious Bacteria

    题目大意: 给出一个x,求满足x = b^p,p最大是多少? 解题思路: x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en. p = gcd (e1,e ...

  5. LightOJ - 1341 Aladdin and the Flying Carpet 唯一分解定理LightOJ 1220Mysterious Bacteria

    题意: ttt 组数据,第一个给定飞毯的面积为 sss,第二个是毯子的最短的边的长度大于等于这个数,毯子是矩形但不是正方形. 思路: 求出 sss 的所有因子,因为不可能是矩形,所以可以除以 222, ...

  6. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  7. LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...

  8. lightoj 1236 正整数唯一分解定理

    A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     6 ...

  9. LightOJ - 1236 (唯一分解定理)

    题意:求有多少对数对(i,j)满足lcm(i,j) = n,1<=i<=j, 1<=n<=1e14. 分析:根据整数的唯一分解定理,n可以分解为(p1^e1)*(p2^e2)* ...

随机推荐

  1. css pointer-event

    详见:张鑫旭 CSS3 pointer-events:none应用举例及扩展 pointer-events:none ,可以使事件穿透, 如: 2 覆盖在 1 上面. 给 2 设置 pointer-e ...

  2. Unity中 Animator 与Animation 区别

    ①Animation和Animator 虽然都是控制动画的播放,但是它们的用法和相关语法都是大有不同的.Animation 控制一个动画的播放,而Animator是多个动画之间相互切换,并且Anima ...

  3. C编译时`true' undeclared (first use in this function)

    在编译C语言时有时会遇到这样的错误提示: 'true' undeclared (first use in this function) or `false' undeclared (first use ...

  4. 用 JavaScript 实现简单拼图游戏

    本篇主要讲解,如何利用原生的 JavaScript 来实现一个简单的拼图小游戏. 线上体验地址:拼图 一.游戏的基础逻辑 想用一门语言来开发游戏,必须先了解如何使用这门语言来实现一些基础逻辑,比如图像 ...

  5. 马上着手开发 iOS 应用程序

    https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/chapters/Introd ...

  6. jpa自定义条件分页查询

    主要依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  7. ASP.NET-表单验证-DataAnnotations

    DataAnnotations  [数据注解,数据注释] 需要引入两个脚本文件 <script src="@Url.Content("~/Scripts/jquery.val ...

  8. [ReactVR] Add Shapes Using 3D Primitives in React VR

    React VR ships with a handful of 3D primitives. We'll importprimitives like <Sphere/>, <Box ...

  9. UVALive - 2031 Dance Dance Revolution 三维dp

    题目大意:有一个胖子在玩跳舞机.刚開始的位置在(0,0).跳舞机有四个方向键,上左下右分别相应1,2,3,4.如今有下面规则 1.假设从0位置移动到随意四个位置,消耗能量2 2.假设从非0位置跳到相邻 ...

  10. pcap网络抓包 无法import pcap

    坑爹的不知道从哪里看到说仅仅有pcap最多仅仅支持到python2.5,然后又是easy install又是安装pip就是无法成功import pcap... 我的python版本号是2.7.8. s ...