数的素数拆分表示,由于数比较小,也没什么难度
#include<iostream>
#include<sstream>
#include<vector>
#include<map>
#include<algorithm>
#include<deque>
using namespace std;
namespace
{
vector<int> P;
int pow(int a, int b)
{
int res = 1;
while (b--)
res *= a;
return res;
}
void init()
{
P.push_back(2);
for (int i = 3; i <= 32767; i++)
{
bool prime = true;
for (size_t t = 0; t < P.size() && prime; t++)
if (i % P[t] == 0)
prime = false;
if (prime)
P.push_back(i);
}
}
bool is_prime(int num)
{
return binary_search(P.begin(), P.end(), num);
}
void put(map<int, int> &M, int num)
{
if (M.find(num) == M.end())
M[num] = 1;
else
M[num]++;
}
}
int main()
{
init();
string s;
map<int, int> M;
deque<pair<int, int> > Q;
while (getline(cin, s), s != "0")
{
M.clear();
Q.clear();
istringstream is(s);
int a, b, num = 1;
while (is >> a >> b)
num *= pow(a, b);
num--;
while (!is_prime(num))
{
for (size_t t = 0; t < P.size(); t++)
if (num % P[t] == 0)
{
put(M, P[t]);
num /= P[t];
break;
}
}
put(M, num);
for (map<int, int>::iterator it = M.begin(); it != M.end(); it++)
Q.push_front(*it);
cout << Q.front().first << " " << Q.front().second;
for (size_t t = 1; t < Q.size(); t++)
cout << " " << Q[t].first << " " << Q[t].second;
cout << endl;
}
return 0;
}