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 = p1^k1 * p2^k2 pm^km.

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 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.

Sample Input:
97532468

Sample Output:
97532468=2^211171011291

先用素数晒法把求出素数然后再进行枚举

#include
using namespace std;

#define  ll long long
int a[100005];
int  prime[100005];
int tot;

void make_prime()
{
    memset(a,1,sizeof(a));
    a[0]=0,a[1]=0;
    tot=0;
    for(int i=2; i<=100005; i++)
    {
        if(a[i])
        {
            prime[tot++]=i;
            for(int j=i+i; j<=100005; j+=i)
            {
                a[j]=0;
            }
        }
    }
}
int main()
{
    make_prime();
    ll n;
    scanf("%lld",&n);
    if(n==1)
    {
        printf("1=1");
        return 0;
    }
    printf("%lld=",n);
    int cnt=0;
    for(int i=0; i=2; i++)
    {
        cnt=0;

        while(n%prime[i]==0)
        {
            n/=prime[i];
            cnt++;
        }
        if(cnt==0) continue;
        if(cnt==1)
        {
            printf("%d",prime[i]);
        }
        else
        {
            printf("%d^%d",prime[i],cnt);
        }
        if(n>1)
        {
            printf("*");
        }
    }
}

你可能感兴趣的:(PAT-1059 Prime Factors (25分)【素数】)