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的更多相关文章

  1. PAT甲级——A1059 Prime Factors

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

  2. PAT_A1059#Prime Factors

    Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...

  3. [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. 这道题跟 ...

  4. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  5. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  6. 2014辽宁ACM省赛 Prime Factors

    问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...

  7. PAT1059:Prime Factors

    1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  8. PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...

  9. PAT 1059 Prime Factors[难]

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

随机推荐

  1. Webpack 2 视频教程 005 - Webpack 编译输出日志

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  2. HTTP请求头和响应头部包括的信息有哪些?

    每个HTTP请求和响应都会带有相应的头部信息.默认情况下,在发送XHR请求的同时,还会发送下列头部信息: Accept:浏览器能够处理的内容类型 Accept-Charset:浏览器能够显示的字符集 ...

  3. 后台跑包方法 断开ssh程序也能继续执行的方法screen命令

    aircrack-ng -w 字典路径 握手包路径 screen -S 001创建会话 screen -ls  列出窗口列表 screen -r 5位数字  进入会话指令 如果会话恢复不了,则是有可能 ...

  4. Daily Scrumming* 2015.12.21(Day 13)

    一.团队scrum meeting照片 大部分成员编译请假,故今天没有开scrum meeting 二.成员工作总结 姓名 任务ID 迁入记录 江昊 无 无 任务说明: 今日准备编译测验,请假 遇到问 ...

  5. 《linux内核设计与实现》第四章

    调度程序负责决定哪个进程投入运行,何时运行以及运行多长时间.只有通过调度程序合理调度,系统资源才能最大限度发挥作用,多进程才会有并发执行的效果. 最大限度地利用处理器时间的原则是,只要有可以执行的进程 ...

  6. 《Linux内核分析》第八周:进程的切换和系统的一般执行过程

    杨舒雯(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验目的: 使用gdb ...

  7. JSONObject使用方法详解

    1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包. 2.下载jar包 http:// ...

  8. 深入浅出——float

    FLOAT  参考张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]的CSS float浮动的深入研究.详解及拓展 1.FLOAT的特性 float属性的初衷只是为了实现文字 ...

  9. Java循环中try...finally...遇到continue

    一段很简单的代码,先自己在大脑中给出结果: for (int i = 0; i < 5; i++) { System.out.println("enter: i=" + i) ...

  10. python之time模块:获取当前时间

    time模块举例 import time # 获取当前时间戳 t = time.time() print('1)获取当前时间戳:', t) # 当前时间的struct_time形式 t = time. ...