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:

  1. 97532468

Sample Output:

  1. 97532468=2^2*11*17*101*1291
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<math.h>
  5. using namespace std;
  6. typedef long long ll;
  7. typedef struct pt{
  8. int base, p;
  9. pt(){
  10. base = ;
  11. p = ;
  12. }
  13. }info;
  14. ll isPrime(ll N){
  15. ll sqr = (ll)sqrt(N * 1.0);
  16. if(N == )
  17. return ;
  18. for(int i = ; i <= sqr; i++){
  19. if(N % i == )
  20. return ;
  21. }
  22. return ;
  23. }
  24. ll primeTB[];
  25. info num[];
  26. ll findPrime(ll tb[], ll maxNum){
  27. int index = ;
  28. for(ll i = ; i <= maxNum; i++){
  29. if(isPrime(i)){
  30. tb[index++] = i;
  31. }
  32. }
  33. return index;
  34. }
  35. int main(){
  36. ll N, sqr, N2;
  37. scanf("%lld", &N);
  38. N2 = N;
  39. sqr = (int)sqrt(1.0 * N) + ;
  40. ll len = findPrime(primeTB, sqr);
  41. int pi = , index = , tag = ;
  42. for(ll i = ; N != && i < len; i++){
  43. tag = ;
  44. while(N % primeTB[i] == ){
  45. N = N / primeTB[i];
  46. num[index].base = primeTB[i];
  47. num[index].p++;
  48. tag = ;
  49. }
  50. if(tag == )
  51. index++;
  52. }
  53. if(N != ){
  54. num[index].base = N;
  55. num[index++].p = ;
  56. }
  57. if(index == ){
  58. num[].base = N;
  59. num[].p = ;
  60. }
  61. printf("%lld=", N2);
  62. printf("%d", num[].base);
  63. if(num[].p > ){
  64. printf("^%d", num[].p);
  65. }
  66. for(int i = ; i < index; i++){
  67. printf("*%d", num[i].base);
  68. if(num[i].p > ){
  69. printf("^%d", num[i].p);
  70. }
  71. }
  72. cin >> N;
  73. return ;
  74. }

总结:

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. Tomcat通过自带的Cluster方式实现Session会话共享环境操作记录

    一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败.要 ...

  2. 饿了么element UI<el-dialog>弹出层</el-dialog>修改默认样式不能在<style scoped>修改

    如果在非scoped下,修改el-dialog自动添加的DIV类名的style加上important,可以覆盖原来的width,但这样会让整个项目的样式都乱套. 如果在scoped下修改style.所 ...

  3. CF1016 D. Vasya And The Matrix

    传送门 [http://codeforces.com/group/1EzrFFyOc0/contest/1016/problem/D] 题意 已知矩阵n行m列,以及每一行,每一列所有元素的异或,用 a ...

  4. 《Linux内核》第七周 进程的切换和系统的一般执行过程 20135311傅冬菁

    进程的切换和系统的一般执行过程 一.内容总结与分析 进程调度与进程调度时机 进程调度需求的分类: 第一种分类方式: I/O -bound(频繁进行I/O,通常会花很多时间等待I/O操作) CPU-bo ...

  5. 《linux内核设计与实现》读书笔记——第三章

  6. android开发之图表

    在这里使用的插件为Mpchart,只以折线图为例.首先需要导入

  7. [what is machine learning?]

    1.2 [what is machine learning?] 1.人:observation -->  learing  -->  skill 机器:data --> ML --& ...

  8. Sprint冲刺第二阶段之6---10天(下)

    11月24号——12月8号,这一个时间段学校的电压不是很稳定,时不时会断电,为了冲刺的完整性,我们商量决定把这一时间段做的事情写成一个连贯的小日记.然后统一在整个时间段一起发出来. 经过一个阶段的努力 ...

  9. Linux: HowTo See Directory Tree Structure

    https://www.cyberciti.biz/faq/linux-show-directory-structure-command-line/ Linux: HowTo See Director ...

  10. Spring框架最简单的定时任务调用

    package org.jeecgframework.core.timer; import org.springframework.scheduling.annotation.Scheduled; i ...