A. k-Factorization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Given a positive integer n, find k integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.
Input
The first line contains two integers n and k (2 ≤ n ≤ 100000, 1 ≤ k ≤ 20).
Output
If it's impossible to find the representation of n as a product of k numbers, print -1.
Otherwise, print k integers in any order. Their product must be equal to n. If there are multiple answers, print any of them.
Examples
input
100000 2
output
2 50000
input
100000 20
output
-1
input
1024 5
output
2 64 2 2 2
题目大意:将数n分解为k个大于1的数的乘积(product居然有乘积的意思)
思路:分解质因数
#include <bits/stdc++.h>
#define manx 100005
using namespace std;
int main()
{
int n,k,ans[manx];
while(~scanf("%d%d",&n,&k)){
int cot=0;
for (int i=2; i<=n; i++){
while(1){
if (k==1 && n!=1){
ans[cot++]=n;
break;
}
if (!n || n==1){
printf("-1\n");
break;
}
if (n%i==0){ //分解质因数
k--;
ans[cot++]=i;
n/=i;
}
else break;
}
if (!n || n==1 || k==1) break;
}
if (k==1 && n!=1){
for (int i=0; i<cot; i++)
printf("%d ",ans[i]);
printf("\n");
}
}
return 0;
}