A1059. Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N = p1^k1 * p2^k2 *…*pm^km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
- 97532468
Sample Output:
- 97532468=2^2*11*17*101*1291
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<math.h>
- using namespace std;
- typedef long long ll;
- typedef struct pt{
- int base, p;
- pt(){
- base = ;
- p = ;
- }
- }info;
- ll isPrime(ll N){
- ll sqr = (ll)sqrt(N * 1.0);
- if(N == )
- return ;
- for(int i = ; i <= sqr; i++){
- if(N % i == )
- return ;
- }
- return ;
- }
- ll primeTB[];
- info num[];
- ll findPrime(ll tb[], ll maxNum){
- int index = ;
- for(ll i = ; i <= maxNum; i++){
- if(isPrime(i)){
- tb[index++] = i;
- }
- }
- return index;
- }
- int main(){
- ll N, sqr, N2;
- scanf("%lld", &N);
- N2 = N;
- sqr = (int)sqrt(1.0 * N) + ;
- ll len = findPrime(primeTB, sqr);
- int pi = , index = , tag = ;
- for(ll i = ; N != && i < len; i++){
- tag = ;
- while(N % primeTB[i] == ){
- N = N / primeTB[i];
- num[index].base = primeTB[i];
- num[index].p++;
- tag = ;
- }
- if(tag == )
- index++;
- }
- if(N != ){
- num[index].base = N;
- num[index++].p = ;
- }
- if(index == ){
- num[].base = N;
- num[].p = ;
- }
- printf("%lld=", N2);
- printf("%d", num[].base);
- if(num[].p > ){
- printf("^%d", num[].p);
- }
- for(int i = ; i < index; i++){
- printf("*%d", num[i].base);
- if(num[i].p > ){
- printf("^%d", num[i].p);
- }
- }
- cin >> N;
- return ;
- }
总结:
1、判断素数的时候,循环条件为 i <= sqr。
2、生成的质数表可以范围到10^5, 也可以生成到 根号N的范围。
3、15 = 3 * 5,找因子只用找到根号N的范围,如果循环结束N仍然不等于1时,说明它就是大于根号N的一个因子,或者是N本身。
A1059. Prime Factors的更多相关文章
- PAT甲级——A1059 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT_A1059#Prime Factors
Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...
- [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 2014辽宁ACM省赛 Prime Factors
问题 L: Prime Factors 时间限制: 1 Sec 内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...
- PAT1059:Prime Factors
1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
随机推荐
- 挂载银行前置机Ukey到windows server2012虚拟机的操作记录
公司有跟银行对接的金融业务,需要配置银行前置机环境.通过KVM的WebVirtMgr管理平台创建windows server2008虚拟机,安装参考:kvm虚拟化管理平台WebVirtMgr部署-完整 ...
- zabbix中配置当memory剩余不足20%时触发报警
在zabbix中默认当内存剩余量不足2G的时候触发报警,并没有使用百分比来触发如下: 现在需要配置:当memory剩余不足20%时触发报警,具体操作方法如下: 1)创建itemConfiguratio ...
- LDAP学习笔记总结
一.LDAP概念LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根 ...
- 个人博客作业_week14
M1/M2阶段总结 我在M1阶段负责后端代码的开发,以及协助PM,在M2阶段负责PM,在为期将近一学期的团队软件开发过程中,我深刻体会到了团队协作的重要性,以及合理分配任务的重要性,没有一个好的时间规 ...
- SPRINT四则运算(第二天)
1.小组成员: 李豌湄:master 江丹仪:产品负责人 2.现状: a.已经下载APP分析他们的界面.优缺点和闪光点 b.已改进代码添加功能 3.任务认领: 完成任务的第一个模块: a.下载五个类 ...
- 5-Python3从入门到实战—基础之数据类型(列表-List)
Python从入门到实战系列--目录 列表定义 list:列表(list)是Python内置的一种数据类型,list是一种有序的集合,索引从0开始,可以进行截取.组合等: //创建列表 list1 = ...
- MySQL基础~~编程语法
常量 数值 字符串:单引号或者双引号括起来.包括普通字符串或者日期格式的字符串. 布尔值:false(FALSE)对应数字值为0.true(TRUE)对应数字值为1. NULL:可以参考http:// ...
- Using Android Phone to recover SD card formatted with DD command under linux
Using Android Phone to recover SD card formatted with DD command under linux 1. Formatted a sd card ...
- Hacked VisualSVN Server by PHP to allow user change password
index.php <?php$username = $_SERVER["PHP_AUTH_USER"]; //经过 AuthType Basic 认证的用户名$authed ...
- MiniDP与HDMI的关系
高清输出革命先行者:HDMI接口 HDMI高清晰度多媒体接口(英文:High Definition Multimedia Interface,HDMI)是一种数字化视频/音频接口技术,是适合影像传输的 ...