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& ...
随机推荐
- Java数组的基本讲解
由于Java数组涵盖的内容比较多,这里从个人的角度对主要的内容进行相关的讲解. 如有不足,欢迎批评指正~ 1)Java数组是动态还是静态的啊? Java语言是典型的静态语言,由此推断Java数 ...
- ASP.NET Core 中文文档 第四章 MVC(2.2)模型验证【转载】
http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_2_2-validation.html 介绍模型验证 在一个应用程序将数据存储到数据库之前,这个应 ...
- python 基础 列表 小例子
存主机ip到列表 host_list=[] netip='192.168.1' for hostip in range(1,254): ip = netip +str(hostip) host_lis ...
- linux命令-vim编辑模式
按 i 键 进去编辑模式 左下角显示 插入 按 I 键 进入编辑模式 光标到行首 按 a 键 在光标的后一位 按A 键 光标在行尾 按 o 键 在光标下面另起一行 按O 键 在光标上面另起一行 ...
- excel2003, 2007最大行列、sheet数
1.excel2003版本一个工作表最多可有65536行,行用数字1—65536表示; 256列,列用英文字母A—Z,AA—AZ,BA—BZ,……,IA—IV表示.2.一个工作簿中最多含有25 ...
- /*带动画效果的hover*/
<!DOCTYPE html> /*带动画效果的hover*/ <html lang="en"> <head> <meta charset ...
- [51nod1267]4个数和为0(预处理+双指针)
题意:判断能否从序列中找出4个数的和为0. 解题关键:n^2预处理任意两个数的和,sort一下,双指针进行判定. 此解法尚存在一个问题,就是左右枚举的时候如果相同的有许多的时候该左边移动还是右边移动 ...
- [dp]最长单调递增子序列LIS
https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...
- 树莓派 Learning 002 装机后的必要操作 --- 01 解决上网问题
树莓派 装机后的必要操作 - 解决上网问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 装机后的必要操作 解决上网问题 解决上网 ...
- 27.【转载】挖洞技巧:如何绕过URL限制
大家对URL任意跳转都肯定了解,也知道他的危害,这里我就不细说了,过~ 大家遇到的肯定都是很多基于这样的跳转格式:http://www.xxx.xxx/xxx?xxx=http://www.xxx.x ...