#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// Prints a complete factorization in the form:
// if factors is empty: "1x<n>"
// else: "<factors[0]>x<factors[1]>x...x<n>"
void printFactorization(const vector<int>& factors, int lastFactor) {
if (factors.empty()) {
cout << "1x" << lastFactor << "\n";
} else {
for (size_t i = 0; i < factors.size(); ++i) {
cout << factors[i] << "x";
}
cout << lastFactor << "\n";
}
}
// Recursive helper function.
// n : the remaining product to factorize.
// start : the minimum factor to try (to enforce non-decreasing order)
// factors: the current factors chosen (by reference)
void printFactorizationsHelper(int n, int start, vector<int>& factors) {
// Print the current factorization (factors followed by the remaining factor n).
printFactorization(factors, n);
// Try all factors i from 'start' up to √n.
// If i divides n, then push i into factors and recurse with n/i.
// (Because factors are chosen in non-decreasing order, each factorization is unique.)
for (int i = start; i <= static_cast<int>(sqrt(n)); ++i) {
if (n % i == 0) {
factors.push_back(i);
printFactorizationsHelper(n / i, i, factors);
factors.pop_back();
}
}
}
int main() {
int n = 24;
vector<int> factors;
// We start with factor 2 since 1 is handled specially in printFactorization.
printFactorizationsHelper(n, 2, factors);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y21hdGg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBQcmludHMgYSBjb21wbGV0ZSBmYWN0b3JpemF0aW9uIGluIHRoZSBmb3JtOgovLyAgICBpZiBmYWN0b3JzIGlzIGVtcHR5OiAiMXg8bj4iCi8vICAgIGVsc2U6ICI8ZmFjdG9yc1swXT54PGZhY3RvcnNbMV0+eC4uLng8bj4iCnZvaWQgcHJpbnRGYWN0b3JpemF0aW9uKGNvbnN0IHZlY3RvcjxpbnQ+JiBmYWN0b3JzLCBpbnQgbGFzdEZhY3RvcikgewogICAgaWYgKGZhY3RvcnMuZW1wdHkoKSkgewogICAgICAgIGNvdXQgPDwgIjF4IiA8PCBsYXN0RmFjdG9yIDw8ICJcbiI7CiAgICB9IGVs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