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个质因数相乘的形式,如果 k >= m 则情况存在,否则输出"-1".
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define clr(a) memset(a,0,sizeof(a))
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int N = 1010;
int a[MAXN];
int sum;
int Prime_num(int n){
clr(a);
sum=0;
for(int i=2;i*i<=n;i++){
if(n % i == 0){
while(n % i == 0){
n /= i;
a[sum++] = i;
}
}
}
if(n != 1){
a[sum ++] = n;
}
return sum ;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int k = Prime_num(n);
if(k >= m){
int ans = 1;
for(int i=0;i<m-1;i++){
printf("%d ",a[i]);
ans *= a[i];
}
printf("%d\n",n/ans);
}
else cout<<"-1"<<endl;
}
return 0;
}