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 N=p_1^{k_1} \times p_2^{k_2}\times ⋯\times p_m^{k_m} 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 N N in the format N = p 1 k 1 × p 2 k 2 × ⋯ × p m k m N=p_1^{k_1} \times p_2^{k_2}\times ⋯\times p_m^{k_m} N=p1k1×p2k2××pmkm​​ , where p i p_i pi are prime factors of N N N in increasing order, and the exponent k i k_i ki is the number of ​—— hence when there is only one is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^211171011291

解题思路

  题目大意: 给定一个正整数N,找到它所有的质因子,并写成如下格式:
N = p 1 k 1 × p 2 k 2 × ⋯ × p m k m N=p_1^{k_1} \times p_2^{k_2}\times ⋯\times p_m^{k_m} N=p1k1×p2k2××pmkm
  解题思路: 这道题的主要难点在找质因子上,如果用穷举法暴力遍历的方式去一个一个找质因子,会用到两层循环,时间复杂度在 O ( n 2 ) O(n^2) O(n2),那样至少有两个点会运行超时。
  这里介绍一个更高效的找质因子的方法——厄拉多塞素数筛选法 (详情参考链接),该方法能把N以内的质因子的筛选,时间复杂度降到 O ( n × l o g ( l o g ( n ) ) ) O(n\times log(log(n))) O(n×log(log(n)))
  在选出所有的质因子之后就可以统计其计算系数了,可以用map直接进行统计。
  注意一个边界点,当N自己是一个质数的时候,直接输出自己。更特别的,当N为1时,直接输出“1=1”。

/*
** @Brief:No.1059 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-12-25
** @Solution: https://blog.csdn.net/CV_Jason/article/details/85282890
*/
#include<iostream>
#include<vector>
#include<map>
#define MAX 100000
using namespace std;
vector<bool> isPrime(MAX);
vector<int> prime;
int N;

void allPrime(){
	fill(isPrime.begin(),isPrime.end(),1);
	for(int i=2;i*i<MAX;i++){
		for(int j=i*i;j<MAX;j+=i){
			isPrime[j] = 0;
		}
	}
	for(int i=2;i<MAX;i++){
		if(isPrime[i]){
			prime.push_back(i);
		}
	}
} 
 
int main(){
	while(cin>>N){
		allPrime();
		map<int,int> factor;
		if(N==1){
			cout<<"1=1"<<endl;
			return 0;
		}
		int temp = N;
		while(N>1){
			for(auto elem:prime){
				while(N%elem==0){
					factor[elem]++;
					N/=elem;
				}
			}
			if(N == temp){
				cout<<N<<"="<<N<<endl;
				return 0;
			}
		}
		auto it = factor.begin();
		cout<<temp<<"="<<it->first;		
		if(it->second>1)
			cout<<"^"<<it->second;
		for(++it;it!=factor.end();it++){
			cout<<"*"<<it->first;
			if(it->second>1)
				cout<<"^"<<it->second;
		}
		cout<<endl;
	}
	return 0;
}

在这里插入图片描述

总结

  如果知道厄拉多塞筛选法,这道题很快就能做出来了,如果不知道,这道题近乎无解,看来有时候出了编程功底之外,一定的数学常识也是必备的。其实有时候觉得编程到底就是数学……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值