1220 - Mysterious Bacteria--LightOj1220 (gcd)
2016-06-01 16:00
330 查看
http://lightoj.com/volume_showproblem.php?problem=1220
题目大意: 给你一个x,求出满足 x=b^p, p最大是几。
分析:x=p1^a1*p2^a2*...*pn^an;
p最大是gcd(a1,a2,...,an)。
///他该诉你x,b,p都是整数,所以x,b有可能是负数。当x是负数时,p不能是偶数。
题目大意: 给你一个x,求出满足 x=b^p, p最大是几。
分析:x=p1^a1*p2^a2*...*pn^an;
p最大是gcd(a1,a2,...,an)。
///他该诉你x,b,p都是整数,所以x,b有可能是负数。当x是负数时,p不能是偶数。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> using namespace std; typedef long long LL; #define N 100000 #define ESP 1e-8 #define INF 0x3f3f3f3f #define memset(a,b) memset(a,b,sizeof(a)) LL prime , k; bool vis ; void Prime() { memset(vis, false); k = 0; for(int i=2; i<N; i++) { if(vis[i] == 0) { prime[k ++] = i; for(int j=i+i; j<N; j+=i) { vis[j] = 1; } } } } LL gcd(LL a, LL b) { LL r; while(b) { r = a%b; a = b; b = r; } return a; } LL solve(LL n) { LL ans, sum; ans = 0; sum = 1; int flag = 0; for(int i=0; prime[i]*prime[i] <= n; i++) { if(n%prime[i] == 0) { ans = 0; while(n%prime[i] == 0) { n /= prime[i]; ans ++; } if(flag == 0) { sum = ans; flag = 1; } else if(flag == 1) sum = gcd(sum, ans); } } if(n>1) sum = 1; return sum; } int main() { int T, t=1; Prime(); scanf("%d", &T); while(T --) { LL n; scanf("%lld", &n); int b = 0; if(n<0) { n = -n; b = 1; } LL sum = solve(n); if(b == 1 && sum%2 == 0) { while(sum%2 == 0) sum/=2; } printf("Case %d: %lld\n", t++, sum); } return 0; }
相关文章推荐
- Sass与Compress实战:第三章
- mysql, PostgreSQL,Oracle对比——sql,驱动和数据库URL
- java调用Kafka的Consumer
- poj1328——Radar Installation(贪心)
- onMeasure()笔记《一》
- php安装composer是报错解决
- C# Access数据库增删查改的简单方法
- 位移运算符
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- rhel7.2 yum
- shell编程学习
- C++异常处理assert,throw,exit用法
- iOS 数组中是否包含某个字符串(BOOL)
- oracle笔记——索引
- leetcode (30) Substring with Concatenation of All Words
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
- Eclipse离线安装ADT
- windows 下编译64位的 Boost 1. 61. 0
- 关于IAR编译器定位不到函数或关键字问题!
- IOS键盘遮挡住了文本框