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& ...
随机推荐
- Lua常用API
转自:http://www.cnblogs.com/ringofthec/archive/2010/10/22/lua.html 1. 建一个新表 void lua_createtable (lua ...
- ios判断是否为iphone6或iphone6plus代码
转自:http://blog.csdn.net/lvxiangan/article/details/45288505 #define IS_IPAD (UI_USER_INTERFACE_IDIOM( ...
- javaScript之事件处理程序
事件就是用户或浏览器自身执行的某个动作,JavaScript与HTML的交互也是通过事件实现的.而相应某个事件的函数就叫做事件处理函数.包括以下几种: 1.HTML事件处理程序 某个元素支持的每 ...
- TMF SID性能相关实体介绍
TMF SID性能相关实体介绍 Copyright © TeleManagement Forum 2013. All Rights Reserved. This document and transl ...
- C# 5.0中新增特性
C# 5.0随着VisualStudio 2012一起正式发布了,让我们来看看C#5.0中增加了哪些功能. 1. 异步编程 在.Net 4.5中,通过async和await两个关键字,引入了一种新的基 ...
- 在Android中使用Protocol Buffers(中篇)
本文来自网易云社区. FlatBuffers 编码原理 FlatBuffers的Java库只提供了如下的4个类: ./com/google/flatbuffers/Constants.java ./c ...
- Codeforces - 102222H - Fight Against Monsters - 贪心
https://codeforc.es/gym/102222/problem/H 题意:有一堆怪兽,怪兽有HP和ATK.你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽.打的伤害递增,第一次1 ...
- C#中参数值传递和址传递
概论 我认为的形参和实参是这样的,形参是形式上的参量,和"抽象类"的概念差不多,不是实际存在的.不用的时候不占用内存,被调用的时候分配内存,调用结束,释放内存.类似于"抽 ...
- 51nod1521(set.upper_bound())
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1521 题意:中文题诶- 思路: 我们先看一下set容器的三个 ...
- uva12545 比特变换器(贪心)
uva12545 比特变换器(贪心) 输入两个等长的串S,T(长度小于100),其中S包含字符0,1,?,T中包含0和1.有三种操作:将S中的0变为1,?变为0或1,交换S中的任意两个字符.求将S变成 ...