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 ...
随机推荐
- P1273 有线电视网[分组背包+树形dp]
题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...
- centos安装nodejs和配置npm
1.下载安装nodejs 1 wget http://nodejs.org/dist/v7.4.0/node-v7.4.0.tar.gz 2 yum install gcc openssl-devel ...
- redis 发展史 应用场景
引言 在Web应用发展的初期,那时关系型数据库受到了较为广泛的关注和应用, 原因是因为那时候Web站点基本上访问和并发不高.交互也较少. 而在后来,随着访问量的提升,使用关系型数据库的Web站点多多少 ...
- BZOJ4706 B君的多边形 (超级卡特兰数/施罗德数)
题目 权限题QAQ 题解 超级卡特兰数/施罗德数 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 1 ...
- git删除指定commit
1.使用git log 命令,查看已提交的记录.例如红色圈出的commit是本次要删除的commit. 2.先找到此次提交之前的一次提交的commit 1d6b81b138f89735265900b9 ...
- 008——转载——c#获取当前日期时间
(一)转载——c#获取当前日期时间 我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日 ...
- MAC OS系统替换homebrew使用阿里云的镜像源
MAC OS系统替换homebrew使用阿里云的镜像源 2019-03-03 15:13:42 南通SEO 阅读数 2024更多 分类专栏: 解决方案 MAC OS系统替换homebrew使用阿里 ...
- Oracle 绑定变量窥视
绑定变量窥视功能是数据库的一个特性,自ORACLE9i版本开始引入,默认是开启的. “绑定变量窥视”表示,查询优化器在第一次调用游标时,会观察用户定义的绑定变量的值,允许优化器来确认过滤条件的选择性, ...
- MongoDB 分片管理(一)检查集群状态
一.检查集群状态 1.1 使用sh.status()查看集群摘要信息 1.使用sh.status()可以查看分片信息.数据库信息.集合信息 sh.status() 如果数据块较多时,使用sh.stat ...
- Codefest 2019 比赛总结
蒟蒻的心路历程 上来看B,结果不会... 回来做A,写完之后nantf已经切B了. 回来做B,花了13min磕了出来. 继续做C,自闭. 继续做D,花了10min磕了出来. 继续做E,一开始有点自闭, ...