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. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [二] 基本使用

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 使用环境 Visual Studio 2017 .NET ...

  2. Python_复习_习题_29

    # 之前做得的题 以后再遇到能保证会# 下周二考 :所有的知识# 面试题:认真对待## 三元运算符# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果# 接收结果的变量 = “ ...

  3. github第一次作业链接

    https://github.com/xuhuzi/test/blob/master/test1 https://github.com/xuhuzi/test/blob/master/test2 ht ...

  4. Android 學習之旅!(1)

    就這樣就過去了一年加一個學期,現在是大二第二個學期而且是下半學期了,以前都是無所事事,沒事睡睡覺,打打遊戲就過去了,但是想到家境和以後的路,我還是決心自己找點東西學習下,以後出去還能有一技之長(雖然可 ...

  5. MyBatis 返回类型resultType为map时的null值不返回问题

    问题一:    查询结果集中 某字段 的值为null,在map中不包含该字段的key-value对 解决:在mybatis.xml中添加setting参数 <!-- 在null时也调用 sett ...

  6. Maven的课堂笔记4

    9.Maven与MyEclipse2014结合 MyEclipse10以上的版本,对Maven支持的就比较好 9.2 Myeclipse配置 本地文件夹的C盘的.m2文件夹下必须得有这个setting ...

  7. Tools (StExBar vs Cmder)which can switch to command line window on context menu in windows OS

    https://tools.stefankueng.com/StExBar.html https://github.com/cmderdev/cmder

  8. C/C++关键字 new/delete和malloc/free

    基本上new/delete来自于C++,作为对对象的创建.因此在使用new创建对象时候new会调用对象的构造函数,同样delete会调用对象的析构函数释放对象.而malloc/free操作的是直接的内 ...

  9. Linux安装的分区问题

    对于一般的外部SATA存储设备,在ubuntu中会显示在/etc/sd*中,例如: SATA1插槽上的文件名:/dev/sda SATA5插槽上的文件名:/dev/sdb USB磁盘:/dev/sdc ...

  10. awk、sed、grep三大shell文本处理工具之sed的应用

    sed 流编辑器 对文本中的行,逐行处理 非交互式的编辑器 是一个编辑器 1.工作流程 1)将文件的第一行读入到自己的缓存空间(模式空间--pattern space),删除掉换行符 2)匹配,看一下 ...