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:

97532468

Sample Output:

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代码:

#include<iostream>
#include<queue>
typedef long long ll;
using namespace std;
ll n;
queue<ll>q;
int main(){
cin>>n;
ll oldN=n;
if(n == ) // 这一段代码非常重要 ,需要考虑n=1的情况
{
cout<<n<<'='<<;
return ;
}
for(ll i=;i<=n;i++){
int num=;
while(n!=i)
{
if(n%i==){
n/=i;
num++;
}else{
break;
}
}
if(n==i){
num++;
}
if(num!=){
q.push(i);
q.push(num);
}
}
cout<<oldN<<"=";
int f=;
while(!q.empty()){
if(f==) cout<<"*";
else f=;
ll x=q.front();q.pop();
ll y=q.front();q.pop();
if(y!=) cout<<x<<"^"<<y;
else cout<<x;
}
return ;
}

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. MySQL的索引优化,查询优化

    MySQL逻辑架构 如果能在头脑中构建一幅MySQL各组件之间如何协同工作的架构图,有助于深入理解MySQL服务器.下图展示了MySQL的逻辑架构图. MySQL逻辑架构,来自:高性能MySQL My ...

  2. Django REST framework+Vue 打造生鲜电商项目(笔记六)

    (部分代码来自https://www.cnblogs.com/derek1184405959/p/8836205.html) 九.个人中心功能开发 1.drf的api文档自动生成 (1) url #d ...

  3. HaspMap和ConcurrentHashMap

    参考自:http://www.importnew.com/28263.html HaspMap和ConcurrentHashMap(康科瑞特哈希迈普) Java7 HashMap 不支持并发操作,Ha ...

  4. docker:设置国内镜像仓储

    修改docker仓储镜像 vi /etc/docker/daemon.json 增加下面数据 { "registry-mirrors": ["https://xwx6wx ...

  5. javascript权威指南第14章 表单脚本示例代码

    HTML部分 <!DOCTYPE html> <html> <head> <title></title> </head> < ...

  6. Elasticsearch 使用:创建、插入、查询、更新、删除

    Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上. Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索 ...

  7. Linq找不到行或行已更改

    1.发生这种情况第一时间是确认了database明明存在这条数据 2.然后确认了Linq查找的条件中是否有连接条件使得连续更新中发生变化 3.最后发现原来是Linq使用的表实际中有个field由not ...

  8. CF707D Persistent Bookcase 可持久化线段树

    维护一个二维零一矩阵(n,m<=1000),支持四种操作(不超过10^5次): 将(i,j)置一 将(i,j)置零 将第i行零一反转yu 回到第K次操作前的状态 每次操作后输出全局一共有多少个一 ...

  9. 使用u盘安装linux(manjaro)时Grub报错

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/manjaro_install_problem_grub 错误 e ...

  10. spark map函数中使用println无法输出

    问题 // 每个点为hardData中的一个Array val hardData = spark.read.textFile(args(0)).rdd .map(_.split(" &quo ...