We need to solve: given n and k, find k integers > 1 (not necessarily distinct) such that product = n. If impossible, print -1.
Constraints: n up to 100000, k up to 20.
We need to factor n into k factors > 1. This is essentially expressing n as product of k numbers > 1. Since factors can be composite themselves, we can break down prime factors.
Observation: The minimal product for k numbers > 1 is 2^k. So if n < 2^k, impossible. Also, if we factor n into prime factors, the maximum number of factors we can get (by splitting into primes) is the sum of exponents (i.e., total number of prime factors counting multiplicities). If we want exactly k factors, we can combine primes to form composite factors. So condition: we can represent n as product of k factors > 1 if and only if the total number of prime factors (with multiplicity) is at least k (since each factor must be at least 2, but we can combine multiple primes into one factor). Actually, we also need that n >= 2^k (as above). But are there cases where total prime factors >= k but n < 2^k? Since product of primes = n, but if n is small, e.g., n=4, k=3: total prime factors = 2 (2,2) which is less than 3, so fails. n=8, k=3: prime factors = 3 (2,2,2) and n=8 >= 8? Actually 2^3 =8, equals. So okay. n=9, k=3: prime factors = 2 (3,3) < 3 so impossible. n=12, k=3: prime factors = 3 (2,2,3), n=12>=8? 2^3=8, 12>=8. So possible. But what about n=6, k=3: prime factors = 2 (2,3) < 3, impossible. So the necessary and sufficient condition: The total count of prime factors (with multiplicity) must be >= k, because we can always break the product into k numbers by splitting primes into separate numbers (each factor being prime) only if the count of primes >= k. But we can also combine primes to form larger factors, so if we have more primes than k, we can combine them to reduce the number of factors. Actually we need exactly k factors. If we have m prime factors (with multiplicities) and m >= k, we can group them into exactly k factors (each >1) by merging some primes together. Because we can start with each prime as separate factor, giving m factors. Then we can repeatedly multiply two factors together to reduce the count by 1 (since product stays same). So as long as we have at least 2 factors, we can combine them. So we can go from m factors down to any number between 1 and m (provided we always have at least one factor). But to get exactly k factors, we need to be able to combine until we have k. That requires that m >= k, because we can combine until we have exactly k factors (since we can combine arbitrarily, e.g., combine two primes into one composite factor, reducing count by 1). However, there is a catch: When we combine factors, we must ensure each factor remains >1. That's fine since all factors are >1 (primes or composites). So m >= k is sufficient. But is it also necessary? Suppose m < k, can we still have k factors? Since each factor must be >1, we could break a composite factor into multiple factors, e.g., 4 = 2 * 2, but that would increase the total number of prime factors? Actually if we have a composite factor like 4, we can further factor it into 22, thereby increasing the number of factors. So starting from the prime factorization (which gives m factors), we can split any factor that is composite into more factors, as long as it is not prime. But can we split a prime? No. So to increase the total number of factors beyond m, we need to split composite numbers into smaller factors >1. For example, n=4 (2^2) has m=2. For k=3, we need 3 factors >1 product=4. Is it possible? 4 = 221 but 1 not allowed, so impossible. Because any factorization of 4 into integers >1 yields at most 2 factors (22) or 4 alone. So indeed, the maximum number of factors we can have is m (the total number of primes when you fully factor into primes). Actually can we have more factors than m? Suppose n=8 = 2^3, m=3. Could we have 4 factors? 8 = 2221? No. 8 = 2221 not allowed. 8 = 222? that's 3. Could we split 2 into something? No. 8 = 42? 42=8, that's 2 factors. To get 4 factors, we would need something like 222? but product already 8, need factor 1. So max factors is m, because any factorization into integers >1 corresponds to a partition of the multiset of prime factors into groups; each group's product is a factor. The number of factors is the number of groups, which cannot exceed the total number of prime factors (since each group must contain at least one prime factor). So maximum is exactly m (when each group is a single prime). Thus necessary condition: k <= m. Also we need n >= 2^k (since each factor >=2), but if k <= m, then n = product of m primes each >=2, so n >= 2^m >= 2^k (since m >= k). So n >= 2^k automatically if m >= k? Not necessarily: if some primes are >2, then product might be larger, but min product given m primes is 2^m. So if m >= k, then 2^m >= 2^k? Since m >= k, yes 2^m >= 2^k. But n itself could be less than 2^k? Actually n = product of the primes, each at least 2. So n >= 2^m >= 2^k. So n >= 2^k automatically. So the only necessary and sufficient condition is that the total number of prime factors (with multiplicity) m >= k.
But wait: Edge case: n = 1? But n >=2. So fine.
Thus algorithm: factor n into primes. Count total number of prime factors (including multiplicities) as total_primes. If total_primes < k, output -1. Else, we need to output k numbers >1 whose product = n. We can produce list of prime factors (with repetitions). We have more primes than needed. We need exactly k factors. So we can take the first k-1 factors as the first k-1 primes (or any grouping), and the last factor as the product of the remaining primes. However, we must ensure all factors >1, which they will be.
But careful: The product of the remaining primes could be 1 if there are none left? But since total_primes >= k, after taking k-1 primes, there is at least 1 prime left, so product >1. Good.
Thus we can output: For i=1..k-1, output one prime factor (from list), and output the product of the rest. That yields exactly k factors.
But need to ensure that the product of the rest is >1. That's fine.
But is there any case where we have exactly total_primes = k? Then we just output all primes.
But we need to ensure that each factor is strictly greater than 1, which holds.
Thus solution seems trivial after prime factorization.
But check examples: Input 100000 2 -> n=100000=10^5? Actually 100000 = 10^5 = 2^5 * 5^5? Let's factor: 100000=10^5 = (2*5)^5 = 2^5 * 5^5, so total primes = 10. Since k=2, we output one prime (say 2) and product of remaining 9 primes: 2^4 * 5^5 = 16 * 3125 = 50000. So output "2 50000", matches sample. Another representation could be different.
Second example: 100000 20: total_primes=10 <20, so -1, matches.
Third: 1024 5: 1024=2^10, total_primes=10>=5. So we can output, say first 4 primes as 2,2,2,2 and remaining product=2^6=64, yields: 2 2 2 2 64, which is permutation of sample output (2 64 2 2 2). Good.
Thus solution is straightforward.
But wait: Are there any constraints that the factors must be strictly greater than 1, but also not necessarily prime. So our method works.
Edge Cases: If n is prime and k=1, total_primes=1 >=1, output n. That's allowed. If n is prime and k>1, total_primes=1 < k, impossible, output -1. Indeed, prime cannot be expressed as product of more than 1 integer >1.
If n=2^1 (prime) and k=1 => output 2.
If n=2^1 and k=2 => total_primes=1<2 => -1, correct since 2 cannot be expressed as product of two >1 numbers.
Thus solution: Factor n using trial division up to sqrt(n). Since n <= 100000, simple method fine.
Pseudo:
- Read n, k.
- Do prime factorization: For i from 2 to sqrt(n), while n%i ==0, add i to list, n/=i. After loop, if n>1, add n.
- Let m = size of list (total primes).
- If m < k: print -1.
- Else:
- For i from 0 to k-2: print list[i] (the first k-1 primes).
- Compute product = product of remaining primes from index k-1 to m-1.
- Print product.
But ensure that product is printed as integer, may be large but within 100000 (actually product of remaining primes = original n divided by product of first k-1 primes). Since n <= 100000, product fits.
Alternatively, we could directly output primes and last factor.
Implementation details: Use list (vector) to store prime factors. After factoring, if m < k: output -1. Otherwise, output first k-1 factors, then compute remaining product = original n divided by product of first k-1? But careful: original n may have been modified during factorization, so we might store original n before factorization. Or we can compute product of the first k-1 primes and then compute last = original_n / prod_first. But need to be careful about integer division.
Simpler: after building list, we can just compute last factor by multiplying the rest of the list. Or we can just output the list elements accordingly.
Better: Use vector factors; then if size < k: print -1; else: for i=0; i<k-1; i++) print factors[i]; then compute remaining = 1; for i=k-1; i<factors.size(); i++) remaining *= factors[i]; print remaining.
Works.
But note: The order of factors does not matter.
Complexity: O(sqrt(n)) factoring.
Potential pitfalls: The product of remaining may be 1? That would happen only if we have exactly k-1 primes but we have total m >= k, so remaining count at least 1, product at least 2.
Thus solution.
But wait: Is the condition m >= k truly sufficient for all n? Consider n=4, k=3: m=2, fails => -1. Good.
Consider n=36=2^23^2, m=4, k=4 => output 2,2,3,3. k=3 => can output first two primes (2,2) and product of rest = 33=9 => 2,2,9 product 36. That's valid, each >1. k=2 => output 2 and 18? Actually first prime 2, rest product = 233=18 => 2,18 product 36. k=1 => output product of all =36. So all fine.
Consider n=16, m=4, k=5: m=4<5 => -1. So correct.
Consider n=8, m=3, k=3 => 2,2,2. k=4 => -1.
Thus algorithm stands.
Now we should implement.
But there might be nuance: The problem says "strictly greater than 1". So we need to ensure no factor equals 1. Our method yields factors >1 because primes are >1 and product of primes >1.
So solution is simple.
We'll produce C++ code.
Edge Cases: n=1? Not possible because n>=2.
Input format: Two integers separated by space. Output: numbers separated by spaces, perhaps trailing space allowed.
We'll do:
int n,k;
cin>>n>>k;
vector primes;
int temp=n;
for(int i=2; i*i<=temp; i++){
while(temp%i==0){
primes.push_back(i);
temp/=i;
}
}
if(temp>1) primes.push_back(temp);
if(primes.size()<k){
cout<<-1<<endl;
return 0;
}
for(int i=0; i<k-1; i++){
cout<<primes[i]<<" ";
}
int last=1;
for(int i=k-1; i<primes.size(); i++){
last *= primes[i];
}
cout<<last<<endl;
But careful: The product 'last' may be large? Since n up to 100000, product remains within int.
That's it.
But is there any scenario where we might need to output factors in any order and they can be composite? Our method yields composite only possibly for last factor, but that's allowed.
Alternatively, we could output all primes and then combine some to meet exactly k. The above works.
Potential nuance: When k==1, we need to output just one factor: product of all primes. In our loop for k-1, if k=1, then k-1=0, we skip printing any primes. Then compute last = product of all primes from index 0? Actually for loop: for i=0;i<k-1;i++ runs 0 times. Then last = product of primes from i=k-1 to end; k-1=0, so last = product of all primes. So we output last alone. That's correct.
Now testing with examples:
Example1: n=100000, k=2. primes = [2,2,2,2,2,5,5,5,5,5] (10 primes). size=10>=2. k-1=1 => print first prime 2. Then last = product of rest 9 primes = 50000. Output "2 50000". Fine.
Example3: 1024=2^10, primes = ten 2's. size=10>=5. Print first 4: 2 2 2 2, last = 2^6=64. Output as in sample (order may differ but accepted). Good.
Thus solution passes.
But wait: There might be a subtle condition: "k integers (not necessary distinct) such that all these integers are strictly greater than 1". Our method always works if m>=k. But is there any scenario where m>=k but still impossible due to something else? Consider n=2^1, m=1, k=1 -> possible. If n is composite but with large prime factor? e.g., n= 97*2 =194, m=2, k=2 -> possible: 2 and 97. k=3: m=2<3 => impossible. So fine.
What about n=8, m=3, k=2: size>=2, first prime=2, last=2*2=4 => output 2 and 4, product=8, both >1. That's fine.
Thus condition is sufficient.
Therefore answer is straightforward.
I will craft final solution accordingly.
But I recall that some problems of this type may require minimal factors? Not needed.
Double-check: The problem statement: "find k integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n." So any representation works.
Thus solution.
Now write final answer in proper code.
Potential improvements: We need to factor efficiently. Since n <= 100000, O(sqrt(n)) is fine.
We should include iostream, vector, etc.
I'll produce final answer with explanation.
But the user asked for reasoning and final answer. We'll provide solution explanation and code.
I'll also mention that the condition is total number of prime factors >= k.
Alright. We can represent a number as a product of integers greater than 1 exactly when we have at least that many prime factors (counted with multiplicities). If we have more prime factors than needed, we can combine some into a single factor; if we have exactly the right number, we use each prime as a separate factor.
Solution Plan
- Factorize
n into its prime factors (with repetitions) and store them in a list.
- Let
m be the total number of such prime factors.
- If
m < k, it's impossible – output -1.
- Otherwise:
- Print the first
k-1 prime factors.
- The remaining factor is the product of all the rest of the prime factors.
- Output that product as the
k-th number.
Complexity
- Factorization takes (O(\sqrt{n})) time, which is acceptable because (n \le 10^5).
- The rest is linear in the number of factors (at most ~20 because (2^{20} > 10^6)? Actually (2^{20}=1,048,576 > 100,000), but the number of prime factors of a number ≤ 100,000 is at most 16 (since (2^{16}=65536), (2^{17}=131072) – so at most 17). So it's efficient.
C++ Implementation
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> primes;
int temp = n;
for (int i = 2; i * i <= temp; ++i) {
while (temp % i == 0) {
primes.push_back(i);
temp /= i;
}
}
if (temp > 1) {
primes.push_back(temp);
}
if (primes.size() < k) {
cout << -1 << endl;
return 0;
}
// Print first k-1 primes
for (int i = 0; i < k - 1; ++i) {
cout << primes[i] << " ";
}
// Compute the product of the remaining primes
int last = 1;
for (int i = k - 1; i < primes.size(); ++i) {
last *= primes[i];
}
cout << last << endl;
return 0;
}