pat1059. Prime Factors (25)
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 = 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:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
思路:N = p1^k1 * p2^k2 *…*pm^k(p1<p2<..pm-1<pm) 则从i=3开始循环,i依次增大,nn除以自己的质因数,不断减少。这里要注意,N最大的质因数可能只有一个,就是最后剩下的nn,这个需要判断。
#include<cstdio>
#include<stack>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
map<long long,int> fac;
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
long long i,nn,n;
scanf("%lld",&nn);
if(nn==){
printf("1=1\n");
return ;
}
n=nn;
if(n%==){
fac[]=;
n/=;
while(n%==){
fac[]++;
n/=;
}
}
for(i=;i<n;i+=){
if(n%i==){
n/=i;
fac[i]=;
while(n%i==){
n/=i;
fac[i]++;
}
}
}
if(n!=){
fac[n]=;
}
printf("%lld=",nn);
map<long long,int>::iterator it=fac.begin();
printf("%lld",it->first);
if(it->second>){
printf("^%d",it->second);
}
it++;
for(;it!=fac.end();it++){
printf("*%lld",it->first);
if(it->second>){
printf("^%d",it->second);
}
}
printf("\n");
return ;
}
pat1059. Prime Factors (25)的更多相关文章
- PAT1059:Prime Factors
1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 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 (素数因子)
1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- PAT (Advanced Level) 1059. Prime Factors (25)
素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
随机推荐
- Python-Redis的String操作
Ubuntu安装Redis sch01ar@ubuntu:~$ sudo apt install redis-server sch01ar@ubuntu:~$ redis-server sch01ar ...
- EchoServer和EchoClient模型的改进1之多线程
在之前的EchoServer模型个EchoClient模型中,客户端和服务端只是单纯的一一对应的关系,如果存在多个客户端和一个服务端,这就需要具体处理了.在这里我们明显想到的第一种方案是使用多线程处理 ...
- 关于overflow:hidden (转)
关于overflow:hidden (本文只针对hidden这个值的用处进行阐述) 关于overflow:hidden;很多人都知道他是溢出隐藏的一个属性,但是并不是很多人知道它的一些神奇的地方! ...
- R: 时间处理(R自带函数)
################################################### 问题:时间处理 18.4.28 如何用,as.Date()规范日期.计算日期的加减等?? ...
- Leetcode:9. Palindrome Number
这题要求不能使用额外的空间,我也就没做,看了下别人的代码,挺有意义的一道题目,出坏了. 解题思路:从右往左颠倒过来,看看这个值和原来的x值是不是一样,最后还要注意像20这种情况,也是的 public ...
- Spring入门第二十八课
事务的传播行为 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播,例如:方法可能继续在现有事务中运行,也可能开启一个新的事务,并在自己的事务中运行. 事务的传播行为可以由传播属性指定.Spr ...
- hdu1078
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; i ...
- Openstack swift 学习笔记
Swift 不是文件系统或者实时的数据存储系统,而是对象存储,用于长期存储永久类型的静态数据.这些数据可以检索.调整和必要时进行更新.Swift最适合虚拟机镜像.图片.邮件和存档备份这类数据的存储. ...
- 分层图最短路【bzoj2834】: 回家的路
分层图最短路[bzoj2834]: 回家的路 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2834 这道题难在建边. 自己写的时候想到了 ...
- GPT分区
GUID分区表(简称GPT.使用GUID分区表的磁盘称为GPT磁盘).它具有如下优点: 1.支持2TB以上的硬盘. 2.分区个数没有限制.但是Windows系统最多支持128个分区. 3. ...