PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
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:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
题意:
将一个正整数分解质因数,注意坑点1=1.第一次学习质因数分解
题解:
Pollard Rho快速因数分解。时间复杂度为O(n^(1/4))。
程序分析:对 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)的更多相关文章
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- 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 ...
- 【PAT甲级】1059 Prime Factors (25 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 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 ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
随机推荐
- 织梦阿里云OSS解决方案
准备工作 申请OSS账号,并且创建一个public-read的bucket.这里需要权限为public-read是因为后面需要匿名访问. 详细步骤 1.开启织梦远程附件功能2.现在织梦还有远程附件还有 ...
- Java中ClassLoader浅析.
一.问题 请在Eclipse中新建如下类,并运行它: 1 package java.lang; 2 3 public class Long { 4 public static void main(St ...
- 如何下载oracle jdk|oracle jdk下载慢,要登录等等问题
wget -c --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup ...
- VSCodeUserSetup安装教程
VSCodeUserSetup: isual Studio Code是一个轻量级但功能强大的源代码编辑器,可在桌面上运行,适用于Windows,macOS和Linux.它内置了对JavaScript, ...
- TensorFlow(十六):TensorFlow GPU准备
一:安装cuda 下载地址 二:安装cuDNN 三:安装GPU版TensorFlow 注意:gpu版的TensorFlow打开tensorboard要使用:tensorboard --logdir C ...
- Linux使用Aria2命令下载BT种子/磁力/直链文件 转载
Linux使用Aria2命令下载BT种子/磁力/直链文件 博主: Rat's 发布时间:2017 年 10 月 10 日 26725 次浏览 8 条评论 1073 字数 分类:主机教程 首页 正文 分 ...
- eclipse中Maven项目启动报错“3 字节的 UTF-8 序列的字节 3 无效。”
发生原因不知道. 解决方法是在pom.xml中追加 <properties> <project.build.sourceEncoding>UTF-8</project.b ...
- CF1188B Count Pairs
[题目描述] 给定一个质数 \(p\) , 一个长度为 \(n\)n 的序列 \(a = \{ a_1,a_2,\cdots,a_n\}\)一个整数 \(k\). 求所有数对 \((i, j)\) ( ...
- sql server 发布订阅
[配置] 一. 发布方 复制 >> 如果有问题 C:\Windows\System32\drivers\etc hosts: 127.0.0.1 ?? 二. 订阅方 订阅方设置结束 三. ...
- Spring Boot :Failed to instantiate SLF4J LoggerFactory Reported exception:
Spring Boot出现以下错误: Failed to instantiate SLF4J LoggerFactory Reported exception: Failed to instantia ...