1059 Prime Factors (25 分)
 

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=1.第一次学习质因数分解

题解:

Pollard Rho快速因数分解。时间复杂度为O(n^(1/4))。

将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对 n 进行分解质因数,应先找到一个最小的质数 i,然后按下述步骤完成: 
(1)如果这个质数 i 恰等于 n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n != i,但n能被 i 整除,则应打印出 i 的值,并用 n 除以 i 的商,作为新的正整数你n,
 重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

AC代码:

  1. #include<iostream>
  2. #include<queue>
  3. typedef long long ll;
  4. using namespace std;
  5. ll n;
  6. queue<ll>q;
  7. int main(){
  8. cin>>n;
  9. ll oldN=n;
  10. if(n == ) // 这一段代码非常重要 ,需要考虑n=1的情况
  11. {
  12. cout<<n<<'='<<;
  13. return ;
  14. }
  15. for(ll i=;i<=n;i++){
  16. int num=;
  17. while(n!=i)
  18. {
  19. if(n%i==){
  20. n/=i;
  21. num++;
  22. }else{
  23. break;
  24. }
  25. }
  26. if(n==i){
  27. num++;
  28. }
  29. if(num!=){
  30. q.push(i);
  31. q.push(num);
  32. }
  33. }
  34. cout<<oldN<<"=";
  35. int f=;
  36. while(!q.empty()){
  37. if(f==) cout<<"*";
  38. else f=;
  39. ll x=q.front();q.pop();
  40. ll y=q.front();q.pop();
  41. if(y!=) cout<<x<<"^"<<y;
  42. else cout<<x;
  43. }
  44. return ;
  45. }

PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)的更多相关文章

  1. 1059 Prime Factors (25分)

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

  2. PAT 甲级 1059 Prime Factors

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

  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 (25 分)

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

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

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

  6. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  7. 1059. Prime Factors (25)

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

  8. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  9. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

随机推荐

  1. P1462 通往奥格瑞玛的道路[最短路+二分+堆优化]

    题目来源:洛谷 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描 ...

  2. JS多线程之Web Worker

    什么是Web Worker web worker 是运行在后台的 JavaScript,不会影响页面的性能. 当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web wor ...

  3. JDK源码那些事儿之ArrayBlockingQueue

    线程在JDK中是非常重要的一块内容,所有的应用服务都离不开线程的相关操作,对于大量线程的使用都是推荐使用线程池来进行操作和管理,JDK本身提供的线程池在服务中经常被使用到,以往经常使用Executor ...

  4. python 查询每周最后一个工作日

    背景: 做定时任务时,一般都是写死每周五XXXXX,但有时遇到节假日的情况,周五可能不是本周最后一个工作日 代码如下: import urllib2,datetime,json nowTime = d ...

  5. numpy 参考:https://mp.weixin.qq.com/s?__biz=MzU1MjYzNjQwOQ==&mid=2247486010&idx=1&sn=e42e6706e0e285ecbfdbbd76fb4ff352&chksm=fbfe50accc89d9ba56a3167c519638f1327a5c5bf12ed59dd8c6de9b2c25baeec1f1f8ad5fb7&

    a=np.array([,,,]) b=np.arange() print(a,b) [ ] [ ] 对应相乘 c2=a*b      [ 0  2  6 12] 对应相乘再求和  c3=a.dot( ...

  6. VIM--保存和退出等命令

    在 Linux 中使用 vim 时,输入 vim xxx.file 按 ESC,左下角就可以进行输入 :w 保存但不退出 :wq 保存并退出 :q 退出 :q! 强制退出,不保存 :e! 放弃所有修改 ...

  7. LOJ P10151 分离与合体 题解

    Analysis 区间dp+记录路径 用dfs倒着找倒数第几次合并 #include<iostream> #include<cstdio> #include<cstrin ...

  8. 【csp模拟赛九】--dfs2

    dfs 代码: #include<algorithm> #include<iostream> #include<cstdio> using namespace st ...

  9. luogu P3386 【模板】二分图匹配

    二次联通门 : luogu P3386 [模板]二分图匹配 /* luogu P3386 [模板]二分图匹配 最大流 设置源点,汇点,连到每条边上 跑一边最大流即可 */ #include <i ...

  10. 安装cartographer遇到Unrecognized syntax identifier "proto3". This parser only recognizes "proto2"问题

    https://stackoverflow.com/questions/38605734/mac-cannot-find-eigen3 https://blog.csdn.net/qq_4214518 ...