当前位置:编程学习 > C/C++ >>

POJ_1365_Prime_Land

//懒得解释
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
#define Max 110000
#define Size 100000
#define Type int
bool isPrime[Max];
Type primeTable[Max];
Type expCount[Max];

void primeRadiation(const Type& size){
    memset(isPrime,true,sizeof(isPrime));
    isPrime[1]=false;
    for(Type i=4;i<=size;i+=2) isPrime[i]=false;
    for(Type i=3;i<=sqrt(size);++i){
        if(isPrime[i]==true){
            const Type step=2*i;
            for(Type j=i*i;j<=size;j+=step) isPrime[j]=false;
        }
    }
}

int setPrimeTable(const Type& size){
    memset(primeTable,0,sizeof(primeTable));
    int index=0;
    for(int i=1;i<=size;++i){
        if(isPrime[i]==true){
            index++;
            primeTable[index]=i;
        }
    }
    return index;
}

void countExp(Type num){
    memset(expCount,0,sizeof(expCount));
    Type index=1;
    while(num!=1){
        if(num%primeTable[index]==0){
            num/=primeTable[index];
            expCount[index]++;
        }
        else{
            index++;
        }
    }
}

int main(){
    char c;
    Type base;
    Type exp;
    primeRadiation(Size);
    Type t=setPrimeTable(Size);
    while(1){
        Type ans=1;
        cin>>base;
        if(base==0) break;
        cin>>exp;
        ans*=pow(base,exp);
        while(1){
            c=getchar();
            if(c=='\n') break;
            cin>>base;
            cin>>exp;
            ans*=pow(base,exp);
        }
        Type temp=ans-1;
        countExp(temp);
        for(Type i=temp;i>=1;--i){
            if(expCount[i]!=0) cout<<primeTable[i]<<" "<<expCount[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,