PAT甲级 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}}\times{p_{2}}^{k_{2}}\times\cdot \cdot \cdot\times{p_{m}}^{k_{m}}

输入

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值