1059 Prime Factors
题意:
给出一个int型正整数N,要求把N分解成若干个质因子,如N=97532468,则把N分解成:97532468=2^2*11*17*101*1291。质因子按增序输出,如果某个质因子的幂是1,则1不输出。
思路:质因子分解的基础题。
首先,定义如下因子的结构体,用于存放最终的结果。因为N是一个int范围的正整数,由于2*3*5*7*11*13*17*19*23*29>INT_MAX,也就是说,任意一个int型整数,可分解出来的不同的质因子的个数不会超过10个,因此,数组只要开到10就够了。
struct Factor{
int fac;//质因子
int cnt;//质因子出现的次数
}factor[];
在对正整数N进行分解之前,首先要获取素数表,令其存在数组prime中,这样一来,则逐个判断这个prime数组中的素数,若整除,则一直用N除以当前这个素数,记录这个素数出现的次数,除到不能整除为止,再进入下一个素数的判断。
但是,如果我们要求N=2,147,483,647(INT_MAX)的质因子呢?难道需要求出所有整型的素数吗?这样做当然没有错,但却会超时(判断素数的时间复杂度是O(sqrt(N)),枚举获取1~N的全部素数的时间复杂度是O(N),因此总的时间复杂度是O(N*sqrt(N)),若N>10^5基本就超时了),事实上,对于质因子分解,需要明白这样一个事实——
对于任何一个整数,如果它是素数,则不可被分解,因子只有1和其本身;如果它是合数,则除了1和本身之外,它的因子必然是在sqrt(n)两侧成对出现的,此时,这些质因子要么全部小于等于sqrt(n);要么只存在一个质因子大于sqrt(n),而其他质因子全部小于等于sqrt(n)。基于此,我们考虑,int型的最大值是2,147,483,647,而sqrt(2,147,483,647)≈46,341,根据刚才所说的结论,也就是说一个int型整数若不能被46341以内的素数整除的话,说明因子就是其本身了。因此,我在获取素数表的getPrime()函数中直接硬编码了。
代码:
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
struct Factor{
int fac;//质因子
int cnt;//质因子出现的次数
}factor[];
vector<int> prime;//素数表
//判断素数
bool isPrime(int n)
{
) return false;
int sqr=(int)sqrt(n);
;i<=sqr;i++)
) return false;
return true;
}
//获取素数表,打表思想
void getPrime()
{
;i<;i++)
if(isPrime(i)) prime.push_back(i);
}
int main()
{
int val;
scanf("%d",&val);
getPrime();
int temp=val;
;//factor数组的长度
;i<prime.size();i++){
int p=prime[i];
){
factor[len].fac=p;
factor[len].cnt=;
){
factor[len].cnt++;
temp/=p;
}
len++;
) break;//表示除尽
}
}
){//考虑存在因子大于sqrt(n)的情况(有可能是素数,有可能不是素数,如9998=2*4999)
factor[len].fac=temp;
factor[len].cnt=;
len++;
}
printf("%d=",val);
) printf(");
;i<len;i++){
printf("%d",factor[i].fac);
) printf("^%d",factor[i].cnt);
) printf("*");
}
;
}
1059 Prime Factors的更多相关文章
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- 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 ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- 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 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 (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
随机推荐
- django Models 常用的字段和参数
1.字段 CharField IntegerField floatField DateTimeField DateField DecimalField 2.参数 null default choice ...
- Java Redis Pipeline 使用示例
1. 参考的优秀文章 Request/Response protocols and RTT 2. 来源 原来,系统中一个树结构的数据来源是Redis,由于数据增多.业务复杂,查询速度并不快.究其原因, ...
- PHP使用前的了解
PHP简介 PHP 是什么? PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器")是一种通用开源脚本语言. PHP 脚本在服务器上执行 ...
- storm 入门介绍(持续更新)
storm的集群表面上看和hadoop的集群非常像.但是在Hadoop上面你运行的是MapReduce的Job, 而在Storm上面你运行的是Topology.它们是非常不一样的 — 一个关键的区别是 ...
- LeetCode OJ:Linked List Cycle II(循环链表II)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- C++轮子队-软件需求规格说明书
团队Github项目仓库 软件规格需求说明书 引言 编写目的 软件规格需求说明书书了“2048俄罗斯方块”1.0版本的软件功能性需求和非功能性需求. 文档约定 描述编写文档时所采用的标准或排版约定,包 ...
- Node.js 问题集合
使用node合并多个接口, 最后获取数据慢的问题 暂时没解决方法 pm2 访问 ip 记录到日志 ...
- laravel5表单验证
学习laravel框架有一段时间了,觉得它自带的表单验证特别好用,和大家分享分享 对于一些验证规则手册上都有,相信大家看了就会,我简单的说下怎么使用自定义正则验证: 验证手机号:'tel' => ...
- angularJs 模拟jQuery中的this
在angularJs中,this指向$scope!可以$event配合使用$(event.target)实现,代码如下: HTML部分: <p ng-click="testClick( ...
- 神经网络中的Softmax激活函数
Softmax回归模型是logistic回归模型在多分类问题上的推广,适用于多分类问题中,且类别之间互斥的场合. Softmax将多个神经元的输出,映射到(0,1)区间内,可以看成是当前输出是属于各个 ...