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:

  1. 97532468

Sample Output:

  1. 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,这个需要判断。

  1. #include<cstdio>
  2. #include<stack>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<stack>
  6. #include<set>
  7. #include<map>
  8. using namespace std;
  9. map<long long,int> fac;
  10. int main(){
  11. //freopen("D:\\INPUT.txt","r",stdin);
  12. long long i,nn,n;
  13. scanf("%lld",&nn);
  14. if(nn==){
  15. printf("1=1\n");
  16. return ;
  17. }
  18. n=nn;
  19. if(n%==){
  20. fac[]=;
  21. n/=;
  22. while(n%==){
  23. fac[]++;
  24. n/=;
  25. }
  26. }
  27. for(i=;i<n;i+=){
  28. if(n%i==){
  29. n/=i;
  30. fac[i]=;
  31. while(n%i==){
  32. n/=i;
  33. fac[i]++;
  34. }
  35. }
  36. }
  37. if(n!=){
  38. fac[n]=;
  39. }
  40. printf("%lld=",nn);
  41. map<long long,int>::iterator it=fac.begin();
  42. printf("%lld",it->first);
  43. if(it->second>){
  44. printf("^%d",it->second);
  45. }
  46. it++;
  47. for(;it!=fac.end();it++){
  48. printf("*%lld",it->first);
  49. if(it->second>){
  50. printf("^%d",it->second);
  51. }
  52. }
  53. printf("\n");
  54. return ;
  55. }

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. Java数组的基本讲解

    由于Java数组涵盖的内容比较多,这里从个人的角度对主要的内容进行相关的讲解. 如有不足,欢迎批评指正~ 1)Java数组是动态还是静态的啊?     Java语言是典型的静态语言,由此推断Java数 ...

  2. ASP.NET Core 中文文档 第四章 MVC(2.2)模型验证【转载】

    http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_2_2-validation.html 介绍模型验证 在一个应用程序将数据存储到数据库之前,这个应 ...

  3. python 基础 列表 小例子

    存主机ip到列表 host_list=[] netip='192.168.1' for hostip in range(1,254): ip = netip +str(hostip) host_lis ...

  4. linux命令-vim编辑模式

    按  i  键 进去编辑模式 左下角显示 插入 按 I  键 进入编辑模式 光标到行首 按 a 键 在光标的后一位 按A 键 光标在行尾 按 o 键 在光标下面另起一行 按O 键  在光标上面另起一行 ...

  5. excel2003, 2007最大行列、sheet数

    1.excel2003版本一个工作表最多可有65536行,行用数字1—65536表示;     256列,列用英文字母A—Z,AA—AZ,BA—BZ,……,IA—IV表示.2.一个工作簿中最多含有25 ...

  6. /*带动画效果的hover*/

    <!DOCTYPE html> /*带动画效果的hover*/ <html lang="en"> <head> <meta charset ...

  7. [51nod1267]4个数和为0(预处理+双指针)

    题意:判断能否从序列中找出4个数的和为0. 解题关键:n^2预处理任意两个数的和,sort一下,双指针进行判定. 此解法尚存在一个问题,就是左右枚举的时候如果相同的有许多的时候该左边移动还是右边移动 ...

  8. [dp]最长单调递增子序列LIS

    https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...

  9. 树莓派 Learning 002 装机后的必要操作 --- 01 解决上网问题

    树莓派 装机后的必要操作 - 解决上网问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 装机后的必要操作 解决上网问题 解决上网 ...

  10. 27.【转载】挖洞技巧:如何绕过URL限制

    大家对URL任意跳转都肯定了解,也知道他的危害,这里我就不细说了,过~ 大家遇到的肯定都是很多基于这样的跳转格式:http://www.xxx.xxx/xxx?xxx=http://www.xxx.x ...