1059 Prime Factors
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 = p1k1×p2k2×⋯×pmkm.
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
解析:素数筛选,预处理一下就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相关推荐
- PAT甲级1059 Prime Factors :[C++题解]分解质因子
文章目录 题目分析 题目链接 题目分析 暴力求质因数 下面i就是质因子,s是质因子i的阶数. 暴力的时间复杂度O(n),会超时 void divide(int n){for(int i=2;i< ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- 1059 Prime Factors(25 分)(cj)
1059 Prime Factors(25 分) Given any positive integer N, you are supposed to find all of its prime fac ...
- 1059. Prime Factors (25)
题目如下: Given any positive integer N, you are supposed to find all of its prime factors, and write the ...
- 【题目分析】1059 Prime Factors (25 分)
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any positive integer N, you are supposed to find all of its ...
- 1059 Prime Factors(25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- 1059. Prime Factors (25)--taste
点击打开链接 注意: 1.对于输入为1的情况要特判 2.factor在int范围内开到10就足够 3.sqrt是范围,由于n后面被修改故范围要单写,且sqrt是double int sqr=sqrt( ...
- 1059 Prime Factors
1. 第一次测试点三错误,由于1既不是质数也不是合数,因此对于1来说需要有一个特殊判断,输出:1=1,但是一开始多加了一个等号. 2. 本题需要数学基础好,两个重点: (1)会打印某个范围内的素数表 ...
最新文章
- Kubernetes1.1源码分析(二)
- 【Linux系统编程】进程间通信--无名管道(pipe)
- Linux网络/firewalld和netfilter/netfilter/iptables语法
- 关于css 的AST 语法树的理解
- openwrt源码分析_openwrt 15.05.1源码
- 【codevs1048】【codevs115406TG】石子归并、能量项链,序列dp的典型题目
- 如何做出好的文档 (要学习前人的经验,不要闭门造车)
- html元素可以有多个ID吗?
- 上周热点回顾(11.11-11.17)
- newifi3 高恪魔改_newifi 3 四大路由器固件优劣分析
- 机器学习模型度量方法,分类及回归模型评估
- 如何在Java中实现画图(图文并茂)(内附美丽的分型图片哦)
- 阿里云服务器设置swap交换分区
- nginx正向代理配置
- 两台华为交换机组网互通方法
- Hadoop学习笔记之HDFS
- Linux unzip的使用方法 怎么使用unzip解压zip压缩文件
- 第二次作业-熟悉使用工具
- Maya打造绝色东方含蓄美女详细教程,学会3D建模都能给自己做老婆了,你还在等什么呢?
- 使用HD Tune扫描一块1TB的机械硬盘需要多长时间?