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. 挂载银行前置机Ukey到windows server2012虚拟机的操作记录

    公司有跟银行对接的金融业务,需要配置银行前置机环境.通过KVM的WebVirtMgr管理平台创建windows server2008虚拟机,安装参考:kvm虚拟化管理平台WebVirtMgr部署-完整 ...

  2. zabbix中配置当memory剩余不足20%时触发报警

    在zabbix中默认当内存剩余量不足2G的时候触发报警,并没有使用百分比来触发如下: 现在需要配置:当memory剩余不足20%时触发报警,具体操作方法如下: 1)创建itemConfiguratio ...

  3. LDAP学习笔记总结

    一.LDAP概念LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根 ...

  4. 个人博客作业_week14

    M1/M2阶段总结 我在M1阶段负责后端代码的开发,以及协助PM,在M2阶段负责PM,在为期将近一学期的团队软件开发过程中,我深刻体会到了团队协作的重要性,以及合理分配任务的重要性,没有一个好的时间规 ...

  5. SPRINT四则运算(第二天)

    1.小组成员: 李豌湄:master 江丹仪:产品负责人 2.现状: a.已经下载APP分析他们的界面.优缺点和闪光点  b.已改进代码添加功能 3.任务认领: 完成任务的第一个模块: a.下载五个类 ...

  6. 5-Python3从入门到实战—基础之数据类型(列表-List)

    Python从入门到实战系列--目录 列表定义 list:列表(list)是Python内置的一种数据类型,list是一种有序的集合,索引从0开始,可以进行截取.组合等: //创建列表 list1 = ...

  7. MySQL基础~~编程语法

    常量 数值 字符串:单引号或者双引号括起来.包括普通字符串或者日期格式的字符串. 布尔值:false(FALSE)对应数字值为0.true(TRUE)对应数字值为1. NULL:可以参考http:// ...

  8. 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 ...

  9. Hacked VisualSVN Server by PHP to allow user change password

    index.php <?php$username = $_SERVER["PHP_AUTH_USER"]; //经过 AuthType Basic 认证的用户名$authed ...

  10. MiniDP与HDMI的关系

    高清输出革命先行者:HDMI接口 HDMI高清晰度多媒体接口(英文:High Definition Multimedia Interface,HDMI)是一种数字化视频/音频接口技术,是适合影像传输的 ...