题目
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N =
输入
Each input file contains one test case which gives a positive integer N in the range of long int.
输出
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.
样例输入
97532468
样例输出
97532468=2^2*11*17*101*1291
题意理解
给你一个数,需要输出这个数是由那些质约数构成的,如果约数幂大于等于2的话,输出这个质数的幂次。
那么我们只要用埃氏筛出质数然后在这个质数表里面找约数即可。
这题注意如果是1的话特判一下即可
代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
long long n;
bool prime[N];
void find(){
prime[0]=prime[1]=1;
for(int i=2;i<N;i++)
if(!prime[i])
for(int j=i+i;j<N;j+=i)
prime[i]=1;
}
map<int,int>ma;
void get_pr(int x){
for(int i=2;i<=x;i++){
if(x%i==0&&prime[i]){
while(x%i==0){
ma[i]++;
x/=i;
}
}
}
}
int main(){
find();
cin>>n;
if(n==1){
cout<<"1=1"<<endl;
return 0;
}
get_pr(n);
cout<<n<<"=";
int fl=1;
for(auto it=ma.begin();it!=ma.end();it++){
if(fl){
fl=0;
if(it->second>=2)cout<<it->first<<"^"<<it->second;
else cout<<it->first;
}
else {
cout<<"*";
if(it->second>=2)cout<<it->first<<"^"<<it->second;
else cout<<it->first;
}
}
return 0;
}