Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format 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 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^2*11*17*101*1291
C++:
/*
@Date : 2018-02-01 19:40:32
@Author : 酸饺子 (changzheng300@foxmail.com)
@Link : https://github.com/SourDumplings
@Version : $Id$
*/
/*
https://www.patest.cn/contests/pat-a-practise/1059
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
vector<long long> primeNum = {2};
int output = 0;
int k = 0;
bool isPrime(long long num)
{
auto it = primeNum.begin();
for (; it != primeNum.end(); ++it)
{
if (num % *it == 0)
return false;
}
return true;
}
long long nextPrime(long long last_one)
{
long long next_prime;
if (last_one == 2)
next_prime = 3;
else
for (next_prime = last_one + 2; !isPrime(next_prime); next_prime += 2);
return next_prime;
}
void deFactor(long long N)
{
if (N == 1)
putchar('\n');
else
{
long long last_try = primeNum[primeNum.size()-1];
k = 0;
while (N % last_try != 0)
{
last_try = nextPrime(last_try);
primeNum.push_back(last_try);
}
primeNum.push_back(last_try);
while (N % last_try == 0)
{
++k;
N /= last_try;
}
if (output++)
putchar('*');
if (k > 1)
printf("%lld^%d", last_try, k);
else
printf("%lld", last_try);
deFactor(N);
}
return;
}
int main(int argc, char const *argv[])
{
long long N;
scanf("%lld", &N);
printf("%lld=", N);
if (N == 1)
printf("1\n");
else
deFactor(N);
return 0;
}