1059. Prime Factors (25)

时间限制
50 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HE, Qinming

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)的更多相关文章

  1. PAT1059:Prime Factors

    1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  2. 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 ...

  3. 1059 Prime Factors (25分)

    1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...

  4. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  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 Advanced 1059 Prime Factors (25) [素数表的建⽴]

    题目 Given any positive integer N, you are supposed to find all of its prime factors, and write them i ...

  7. PAT-1059 Prime Factors (素数因子)

    1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...

  8. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  9. PAT (Advanced Level) 1059. Prime Factors (25)

    素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

随机推荐

  1. oracle--pl/sql变量定义----

    一.变量介绍 在编写pl/sql程序时,可以定义变量和常量:在pl/sql程序中包括有: 1).标量类型(scalar) 2).复合类型(composite) --用于操作单条记录 3).参照类型(r ...

  2. 代码,用c++实现线性链表

    #include <iostream> #include <stdio.h> #include <malloc.h> using namespace std; #d ...

  3. USACO-Friday the Thirteenth(黑色星期五)-Section1.2<3>

    [英文原题] Friday the Thirteenth Is Friday the 13th really an unusual event? That is, does the 13th of t ...

  4. JAVA之J2EE学习路线

    摘自:http://blog.csdn.net/hsc456/article/details/51970559   历经2,3个月,终于学完了J2EE的方方面面,虽然还是一知半解,好歹也算是整条路都走 ...

  5. Spring入门第二十五课

    使用具名参数 直接看代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dr ...

  6. rest framework 权限

    一.权限示例 需求:不同的用户类型有不同的权限 普通用户:只能查看个人信息相关,序号:1 VIP 用户:只能查看个人信息相关,序号:2 SVIP 用户:查看订单相关信息,序号:3 1.新建 app/u ...

  7. webconfig配置详解--转

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  8. Loadrunner监控服务器资源

    LoadRunner 加载监听服务器的步骤如下: 1.在 LoadRunner Controller 下,将工作面板切换到 Run状态,Available Graphs 栏 ,System Resou ...

  9. DOMContentLoaded时间触发与js,css,img的关联

    DOMContentLoaded触发原理: 1.规范总是那么的晦涩,但至少有一点是可以明确了的,就是在JS(不包括动态插入的JS)执行完之后,才会触发DOMContentLoaded事件. 2.DOM ...

  10. Java8 使用 stream().sorted()对List集合进行排序

    集合对像定义 集合对象以学生类(StudentInfo)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项. 使用stream().sorted()进行排序,需要该类实现 Comparab ...