A1059. Prime Factors
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
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<math.h>
- using namespace std;
- typedef long long ll;
- typedef struct pt{
- int base, p;
- pt(){
- base = ;
- p = ;
- }
- }info;
- ll isPrime(ll N){
- ll sqr = (ll)sqrt(N * 1.0);
- if(N == )
- return ;
- for(int i = ; i <= sqr; i++){
- if(N % i == )
- return ;
- }
- return ;
- }
- ll primeTB[];
- info num[];
- ll findPrime(ll tb[], ll maxNum){
- int index = ;
- for(ll i = ; i <= maxNum; i++){
- if(isPrime(i)){
- tb[index++] = i;
- }
- }
- return index;
- }
- int main(){
- ll N, sqr, N2;
- scanf("%lld", &N);
- N2 = N;
- sqr = (int)sqrt(1.0 * N) + ;
- ll len = findPrime(primeTB, sqr);
- int pi = , index = , tag = ;
- for(ll i = ; N != && i < len; i++){
- tag = ;
- while(N % primeTB[i] == ){
- N = N / primeTB[i];
- num[index].base = primeTB[i];
- num[index].p++;
- tag = ;
- }
- if(tag == )
- index++;
- }
- if(N != ){
- num[index].base = N;
- num[index++].p = ;
- }
- if(index == ){
- num[].base = N;
- num[].p = ;
- }
- printf("%lld=", N2);
- printf("%d", num[].base);
- if(num[].p > ){
- printf("^%d", num[].p);
- }
- for(int i = ; i < index; i++){
- printf("*%d", num[i].base);
- if(num[i].p > ){
- printf("^%d", num[i].p);
- }
- }
- cin >> N;
- return ;
- }
总结:
1、判断素数的时候,循环条件为 i <= sqr。
2、生成的质数表可以范围到10^5, 也可以生成到 根号N的范围。
3、15 = 3 * 5,找因子只用找到根号N的范围,如果循环结束N仍然不等于1时,说明它就是大于根号N的一个因子,或者是N本身。
A1059. Prime Factors的更多相关文章
- PAT甲级——A1059 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT_A1059#Prime Factors
Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...
- [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...
- 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 ...
- 2014辽宁ACM省赛 Prime Factors
问题 L: Prime Factors 时间限制: 1 Sec 内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...
- PAT1059:Prime Factors
1059. Prime Factors (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
随机推荐
- Tomcat通过自带的Cluster方式实现Session会话共享环境操作记录
一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败.要 ...
- 饿了么element UI<el-dialog>弹出层</el-dialog>修改默认样式不能在<style scoped>修改
如果在非scoped下,修改el-dialog自动添加的DIV类名的style加上important,可以覆盖原来的width,但这样会让整个项目的样式都乱套. 如果在scoped下修改style.所 ...
- CF1016 D. Vasya And The Matrix
传送门 [http://codeforces.com/group/1EzrFFyOc0/contest/1016/problem/D] 题意 已知矩阵n行m列,以及每一行,每一列所有元素的异或,用 a ...
- 《Linux内核》第七周 进程的切换和系统的一般执行过程 20135311傅冬菁
进程的切换和系统的一般执行过程 一.内容总结与分析 进程调度与进程调度时机 进程调度需求的分类: 第一种分类方式: I/O -bound(频繁进行I/O,通常会花很多时间等待I/O操作) CPU-bo ...
- 《linux内核设计与实现》读书笔记——第三章
- android开发之图表
在这里使用的插件为Mpchart,只以折线图为例.首先需要导入
- [what is machine learning?]
1.2 [what is machine learning?] 1.人:observation --> learing --> skill 机器:data --> ML --& ...
- Sprint冲刺第二阶段之6---10天(下)
11月24号——12月8号,这一个时间段学校的电压不是很稳定,时不时会断电,为了冲刺的完整性,我们商量决定把这一时间段做的事情写成一个连贯的小日记.然后统一在整个时间段一起发出来. 经过一个阶段的努力 ...
- Linux: HowTo See Directory Tree Structure
https://www.cyberciti.biz/faq/linux-show-directory-structure-command-line/ Linux: HowTo See Director ...
- Spring框架最简单的定时任务调用
package org.jeecgframework.core.timer; import org.springframework.scheduling.annotation.Scheduled; i ...