Given any positive integer , you are supposed to find all of its prime factors, and write them in the format .
Input Specification:
Each input file contains one test case which gives a positive integer in the range of long int.
Output Specification:
Factor N in the format
=
^
*
^
*
*
^
, where
's are prime factors of
in increasing order, and the exponent
is the number of
– hence when there is only one
,
is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
The meaning of problems
Number will be a prime factor decomposition.
Thinking
First, to calculate the prime numbers, range of data in question is long int, that is, . due to , only to all prime numbers within the calculated required 100,000. After obtaining these prime numbers are prime numbers divisible removal of x, until x = 1.
Possible after all the prime numbers are still not in addition to x 1, x this time is a big prime numbers. But the data is weak, do not consider this situation could go.
Note that the boundary where x = 1.
Code
#include <iostream>
using namespace std;
#define MAX_N 100000
bool not_prime[MAX_N];
int main() {
int x;
cin >> x;
cout << x << "=";
if (x == 1)
cout << "1";
for (int i = 2; i < MAX_N; ++i) // 欧拉筛
if (!not_prime[i])
for (int j = i + i; not_prime[j / i]; j += i)
not_prime[j] = true;
bool first = true;
for (int i = 2; i < MAX_N && x != 1; ++i) {
if (!not_prime[i]) {
int cnt = 0;
while (x % i == 0) {
x /= i;
++cnt;
}
if (cnt > 0) {
if (first)
first = false;
else
cout << '*';
cout << i;
if (cnt > 1)
cout << '^' << cnt;
}
}
}
if (x != 1)
cout << '*' << x;
}