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 = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

解析:素数筛选,预处理一下就o了

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;#define e exp(1)
#define p acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b) {return b?gcd(b,a%b):a;
}const int maxn=5e5+5;
int cnt=0;
int prime[maxn],f[maxn+5];
void init()
{for(int i=2; i<maxn; i++){if(!f[i]){prime[cnt++]=i;for(int j=i; j<maxn; j+=i){f[j]=1;}}}
}
int main()
{init();ll n;scanf("%lld",&n);printf("%lld=",n);if(n==1){puts("1");return 0;}int count=0,flag=0;for(int i=0; i<cnt; i++){if(n==1)break;if(n%prime[i]==0){printf("%d",prime[i]);n/=prime[i];count=1;flag=0;while(n%prime[i]==0){if(n==0)break;if(!flag)printf("^");n/=prime[i];count++;flag=1;}if(flag)printf("%d",count);if(n!=1)printf("*");}}puts("");return 0;
}

1059 Prime Factors相关推荐

  1. PAT甲级1059 Prime Factors :[C++题解]分解质因子

    文章目录 题目分析 题目链接 题目分析 暴力求质因数 下面i就是质因子,s是质因子i的阶数. 暴力的时间复杂度O(n),会超时 void divide(int n){for(int i=2;i< ...

  2. PAT 1059 Prime Factors[难]

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

  3. 1059 Prime Factors(25 分)(cj)

    1059 Prime Factors(25 分) Given any positive integer N, you are supposed to find all of its prime fac ...

  4. 1059. Prime Factors (25)

    题目如下: Given any positive integer N, you are supposed to find all of its prime factors, and write the ...

  5. 【题目分析】1059 Prime Factors (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any positive integer N, you are supposed to find all of its ...

  6. 1059 Prime Factors(25 分)

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

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

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

  8. 1059. Prime Factors (25)--taste

    点击打开链接 注意: 1.对于输入为1的情况要特判 2.factor在int范围内开到10就足够 3.sqrt是范围,由于n后面被修改故范围要单写,且sqrt是double int sqr=sqrt( ...

  9. 1059 Prime Factors

    1. 第一次测试点三错误,由于1既不是质数也不是合数,因此对于1来说需要有一个特殊判断,输出:1=1,但是一开始多加了一个等号. 2. 本题需要数学基础好,两个重点: (1)会打印某个范围内的素数表 ...

最新文章

  1. Kubernetes1.1源码分析(二)
  2. 【Linux系统编程】进程间通信--无名管道(pipe)
  3. Linux网络/firewalld和netfilter/netfilter/iptables语法
  4. 关于css 的AST 语法树的理解
  5. openwrt源码分析_openwrt 15.05.1源码
  6. 【codevs1048】【codevs115406TG】石子归并、能量项链,序列dp的典型题目
  7. 如何做出好的文档 (要学习前人的经验,不要闭门造车)
  8. html元素可以有多个ID吗?
  9. 上周热点回顾(11.11-11.17)
  10. newifi3 高恪魔改_newifi 3 四大路由器固件优劣分析
  11. 机器学习模型度量方法,分类及回归模型评估
  12. 如何在Java中实现画图(图文并茂)(内附美丽的分型图片哦)
  13. 阿里云服务器设置swap交换分区
  14. nginx正向代理配置
  15. 两台华为交换机组网互通方法
  16. Hadoop学习笔记之HDFS
  17. Linux unzip的使用方法 怎么使用unzip解压zip压缩文件
  18. 第二次作业-熟悉使用工具
  19. Maya打造绝色东方含蓄美女详细教程,学会3D建模都能给自己做老婆了,你还在等什么呢?
  20. 使用HD Tune扫描一块1TB的机械硬盘需要多长时间?

热门文章

  1. js替换iframe的内容
  2. Servlet的认识
  3. 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)
  4. 树的Prufer 编码和最小生成树计数
  5. jQuery $.post $.ajax用法
  6. 大宝的读后感:(转载)
  7. Latex入门:编辑器(texmaker+texlive)安装
  8. latex大写运算符号
  9. java 下载url图片_java下载url图片链接
  10. [云炬创业管理笔记]第二章测试3