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& ...
 
随机推荐
- spring扩展点之四:Spring Aware容器感知技术,BeanNameAware和BeanFactoryAware接口,springboot中的EnvironmentAware
			
aware:英 [əˈweə(r)] 美 [əˈwer] adj.意识到的;知道的;觉察到的 XXXAware在spring里表示对XXX感知,实现XXXAware接口,并通过实现对应的set-XXX ...
 - 能否自己也写一个类叫做java.lang.String?
			
这次的随笔很逗吧~没错,我们的确也可以自己在创建一个包java.lang,然后在 相应的包下面创建一个对应的类String,但是在每次jre运行的时候,我们都回去加载原来默认的java.lang.St ...
 - [poj3281]Dining(最大流+拆点)
			
题目大意:有$n$头牛,$f$种食物和$d$种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢 ...
 - SpringBoot04 项目热部署详解
			
1 热部署 优点:适用于本地和线上.无需重启服务器 2 热部署和热加载的联系和区别 2.1 联系 不用重启服务器就可以编译和部署项目 都是基于Java的类加载器实现 2.2 字面区别 热部署在服务器运 ...
 - Linux/Unix 指令使用说明的格式介绍(the Bash Command 'Usage' Syntax)
			
Linux/Unix 指令使用说明的格式介绍(the Bash Command 'Usage' Syntax) 摘自 金马的Blog 原文 http://www.lijinma.com/blo ...
 - 深入了解 Cloud Studio 开发在云端
			
Cloud Studio 为开发者提供了一个永不间断的云端工作站,不管有没有开发经验都可以毫无门槛的体验云端开发的乐趣,支持绝大部分编程语言.Cloud Studio 提供了完整的 Linux 环境, ...
 - C语言实现wc项目
			
该World Count项目用的是C语言编写,只实现了-c.-w.-l.三个功能,由于简单全部代码均由小编自己编写,用的是VS2013只支持windows平台cmd运行. 木有图形界面,参考了Linu ...
 - hdu1055
			
#include<iostream> #include<iomanip> #include<cstdio> #include<cstring> #inc ...
 - Boost Python官方样例(三)
			
导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...
 - SAS笔记(5) FLAG和计数器
			
考虑这样一种场景:我们有一份患者入院检查的数据,我们知道一个患者有可能会多次去医院做检查,每次检查的结果可能为阳性,也可能为阴性.我们现在关注的是某一个患者在若干次检查中是否出现了阳性结果,在R中我们 ...