https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

  1. 97532468

Sample Output:

  1. 97532468=2^2*11*17*101*1291

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long int P;
  5. int prime[500010];
  6.  
  7. int main() {
  8. memset(prime, 1, sizeof(prime));
  9. for(int i = 2; i * i <= 500010; i ++)
  10. for(int j = 2; j * i < 500010; j ++)
  11. prime[j * i] = 0;
  12. scanf("%ld", &P);
  13.  
  14. printf("%ld=", P);
  15. if(P == 1) printf("1");
  16.  
  17. bool First = false;
  18. for(int i = 2; P >= 2; i ++) {
  19. int cnt = 0, flag = 0;
  20. while(prime[i] && P % i == 0) {
  21. cnt ++;
  22. P /= i;
  23. flag = 1;
  24. }
  25. if(flag) {
  26. if(First)
  27. printf("*");
  28. printf("%d", i);
  29. First = true;
  30. }
  31. if(cnt > 1)
  32. printf("^%d", cnt);
  33. }
  34. return 0;
  35. }

  素数表! Get

PAT 甲级 1059 Prime Factors的更多相关文章

  1. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

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

  2. PAT甲级——A1059 Prime Factors

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

  3. PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]

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

  4. PAT 1059 Prime Factors[难]

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

  5. 1059 Prime Factors (25分)

    1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...

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

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

  7. 1059. Prime Factors (25)

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

  8. 1059 Prime Factors(25 分)

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

  9. 【PAT甲级】1059 Prime Factors (25 分)

    题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...

随机推荐

  1. (数据科学学习手札55)利用ggthemr来美化ggplot2图像

    一.简介 R中的ggplot2是一个非常强大灵活的数据可视化包,熟悉其绘图规则后便可以自由地生成各种可视化图像,但其默认的色彩和样式在很多时候难免有些过于朴素,本文将要介绍的ggthemr包专门针对原 ...

  2. 柱体内温度分布图 MATLAB

    对于下底面和侧面绝热,上底面温度与半径平方成正比的柱体,绘制柱体内温度分布图. 这里给出两种尝试:1.散点图:2.切片云图 1. 散点图仿真 首先使用解析算法求的场解值的解析表达,其次求解Bessel ...

  3. Django模型中的元选项和object对象

    1.元选项,在模型类型中定义一个Meta类,用于设置元信息 class Student(models.Model): name = models.CharField(max_length=20) se ...

  4. 20155305 2016-2017-2 《Java程序设计》 实验五 Java网络编程及安全实验报告

    20155305 2016-2017-2 <Java程序设计> 实验五 Java网络编程及安全实验报告 实验内容 1.掌握Socket程序的编写. 2.掌握密码技术的使用. 3.设计安全传 ...

  5. 20155313 2016-2017-2 《Java程序设计》第四周学习总结

    20155313 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 6 继承与多态 面对对象中,子类继承父类,避免重复的行为定义,不过并非为了避免重复定义行为就 ...

  6. 20155330 2016-2017-2 《Java程序设计》第二周学习总结

    20155330 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 学习目标 了解Java编程风格 认识Java的类型与变量 掌握Java流程控制的方法(分支. ...

  7. P3940 分组

    P3940 分组 https://www.luogu.org/problemnew/show/P3940 官方题解http://pan.baidu.com/s/1eSAMuXk 分析: 并查集. 首先 ...

  8. jaron插件的用法

    一.dict字典插件的基本用法: <%@ taglib prefix="dict" uri="http://www.evan.jaron.com/tags/dict ...

  9. 「日常训练」 Fire!(UVA-11624)

    与其说是训练不如说是重温.重新写了Java版本的代码. import java.util.*; import java.math.*; import java.io.BufferedInputStre ...

  10. Phaser3 场景Scene之间的传值 -- HTML JAVASCRIPT 网页游戏开发

      PHASERJS3 一.首先当然得有至少有二个场景sceneA.js,sceneB.js 二.从场景A传值到场景B二种方法 1)通过事件this.events.emit('event key',{ ...