LightOj 1220 Mysterious Bacteria(数论)
2016-03-17 11:12
267 查看
Mysterious Bacteria
DescriptionDr. 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 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
解题思路:
题目大意:给你一个x,求满足x = b^p,p最大是多少?
算法思想:
x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en。
p = gcd (e1,e2,.......en);
坑点:当x是负数的时候,p的值不能为偶数。
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1000005; int vis[maxn]; vector<int> prime; void get_prime(){ memset(vis,false,sizeof(vis)); vis[1] = true; for(int i = 2; i <= 1000000; i++){ int t = 1000000/i; for(int j = 2; j <= t; j++) vis[i*j] = true; } for(int i = 2; i <= 1000000; i++) if(!vis[i]) prime.push_back(i); } int gcd(int a, int b){ if(a%b == 0) return b; else return gcd(b,a%b); } int main (){ get_prime(); int T,t = 1; scanf("%d",&T); while(T--){ int ans = 0,flag = 0; ll n; scanf("%lld", &n); if(n < 0){ n = -n; flag = 1; } int len = prime.size(); for(int i = 0; i < len && prime[i] < n; i++){ int num = 0; if(n%prime[i] == 0){ while(n%prime[i] == 0){ n /= prime[i]; num++; } if(ans == 0) ans = num; else ans = gcd(ans,num); } } if(n != 1) ans = 1; if(flag){ while(ans%2 == 0) ans /= 2; } printf("Case %d: %d\n",t++,ans); } return 0; }
相关文章推荐
- eclipse 新建 Dynamic Web Project时jsp文件404错误的解决办法
- 顺序线性表的实现
- Java中使用正则表达式
- Linux 命令&和&&的区别
- 2333333333
- Makefile的编写
- 学习C层
- Mybatis 示例之 Association (2)
- Mybatis 示例之 Association
- Ubuntu上hi3531交叉编译环境arm-hisiv100nptl-linux搭建过程
- BUG克星:几款优秀的BUG跟踪管理软件
- bitmap指定位置添加小图片
- FastDFS配置文件详解之storage.conf
- English - Grammar(未完)
- Linux中select函数学习及实例笔记
- Spring3.x JSR-303
- CUBRID学习笔记 25 数据类型2
- 那些有趣的Webview细节
- Django--ajax
- cocos2d-x 3.0 内存管理机制