LightOJ1220 —— 质因数分解
题目链接:https://vjudge.net/problem/LightOJ-1220
| Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
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 such that x is a perfect pth power.
Sample Input |
Output for Sample Input |
|
3 17 1073741824 25 |
Case 1: 1 Case 2: 30 Case 3: 2 |
题意:
给出一个数x(可以为负数,绝对值大于等于2), 求使得满足 x = b^p 的最大p。
题解:
1.对x进行质因子分解。
2.取x所有的质因子个数的最大公约数p,如果x为负数,那么p就只能为奇数,所以一直除以2,直到p为奇数。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; bool notprime[MAXN+];
int prime[MAXN+];
void getPrime()
{
memset(notprime, false, sizeof(notprime));
notprime[] = notprime[] = true;
prime[] = ;
for (int i = ; i<=MAXN; i++)
{
if (!notprime[i])prime[++prime[]] = i;
for (int j = ; j<=prime[ ]&& prime[j]<=MAXN/i; j++)
{
notprime[prime[j]*i] = true;
if (i%prime[j] == ) break;
}
}
} int fatCnt;
LL factor[][];
void getFactors(LL n)
{
LL tmp = n;
fatCnt = ;
for(int i = ; prime[i]<=tmp/prime[i]; i++)
{
if(tmp%prime[i]==)
{
factor[++fatCnt][] = prime[i];
factor[fatCnt][] = ;
while(tmp%prime[i]==) tmp /= prime[i], factor[fatCnt][]++;
}
}
if(tmp>) factor[++fatCnt][] = tmp, factor[fatCnt][] = ;
} int gcd(int a, int b)
{
return b==?a:gcd(b, a%b);
} int main()
{
getPrime();
int T, kase = ;
scanf("%d", &T);
while(T--)
{
LL n;
scanf("%lld", &n);
getFactors(abs(n));
LL p = factor[][];
for(int i = ; i<=fatCnt; i++)
p = gcd(p, factor[i][]); while(n< && p%==) p /= ;
printf("Case %d: %d\n", ++kase, p);
}
return ;
}
LightOJ1220 —— 质因数分解的更多相关文章
- 求n!质因数分解之后素数a的个数
n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...
- AC日记——质因数分解 1.5 43
43:质因数分解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
- 整数分解 && 质因数分解
输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <strin ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- POJ1365 - Prime Land(质因数分解)
题目大意 给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式 题解 预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解....其实主要就是字符 ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu1405 第六周J题(质因数分解)
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- POj3421 X-factor Chains(质因数分解+排列组合)
POj3421X-factor Chains 一开始没读懂题意,不太明白 Xi | Xi+1 where a | b means a perfectly divides into b的意思,后来才发现 ...
随机推荐
- TensorFlow——tensorflow指定CPU与GPU运算
1.指定GPU运算 如果安装的是GPU版本,在运行的过程中TensorFlow能够自动检测.如果检测到GPU,TensorFlow会尽可能的利用找到的第一个GPU来执行操作. 如果机器上有超过一个可用 ...
- Springboot 集成 Thymeleaf 及常见错误
Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...
- Linux 嵌入式启动以及优化
转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201312/19-2717.html 第一步: BootLoader -- U boot 1 ...
- python--文件处理1
1. 读取文件 方法: all_the_text = open('thefile.txt').read() 但是为了安全起见还是给打开的文件对象指定一个名字,这样在完成之后可以迅速关掉,防止无 ...
- 为什么HierachyViewer无法连接真机调试
关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个人理解:Hi ...
- 算法之美--3.2.2 MP算法
这块硬骨头,放在这里半年的时间了,一直没有动,今天周末看看,书上把过程写的比较详细,自己基本也看懂了,但是对代码本身的编写还是比较生疏,要经常复习,估计才能看透,后面有看了kmp;这两者之间的关系也是 ...
- servlet基础梳理(一)
将近一个月没看servlet了,再加上第一次学习也没有深入.仅仅是笼统的看了一遍,编了一点基础案例就过去了,如今再去看感觉跟没学过一样.这里再用一点时间把这些基础都梳理一下,加深印象并为以后高速复习做 ...
- CSDN站点系统升级公告
各位尊敬的CSDN用户: 你们好. CSDN站点将于2015年12月17日23时-12月18日08时进行系统升级维护,升级维护期间,CSDN站点将会受到影响.可能会导致博客.下载频道及站点其它功能无法 ...
- S3C2440 IIS操作 uda134x录放音
IIS(Inter-IC Sound)由飞利浦公司开发.是一种经常使用的音频设备接口,主要用于CD.MD.MP3等设备. s3c2440一共同拥有5个引脚用于IIS:IISDO.IISDI.IISSC ...
- 关于position的小总结
position:relative/absolute/fixed/static ...... relative:相对定位. 脱离标准流,相对自己原来(标准流)的位置定位.absolute:绝对定位. ...