Description
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
题意:将数字分解为k个数相乘的形式,不行输出-1
解法:先分解质因数,然后处理成k个数的形式,不行的话输出-1
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=300000; 4 int n,k; 5 int sum; 6 int cmd(int n) 7 { 8 for(int i=2; i*i<=n; i++) 9 { 10 if(n%i==0) 11 { 12 return 0; 13 } 14 } 15 return 1; 16 } 17 int main() 18 { 19 cin>>n>>k; 20 if(k==1) 21 { 22 cout<<n<<endl; 23 return 0; 24 } 25 if((n==2||cmd(n))&&k>1) 26 { 27 cout<<"-1"<<endl; 28 } 29 else 30 { 31 vector<int>q; 32 for(int i=2; i<=n; i++) 33 { 34 while(n!=i) 35 { 36 if(n%i==0) 37 { 38 q.push_back(i); 39 n=n/i; 40 } 41 else 42 break; 43 } 44 } 45 q.push_back(n); 46 if(q.size()<k) 47 { 48 cout<<"-1"<<endl; 49 return 0; 50 } 51 for(int i=0;i<k-1;i++) 52 { 53 cout<<q[i]<<" "; 54 } 55 sum=1; 56 for(int i=k-1;i<q.size();i++) 57 { 58 sum*=q[i]; 59 } 60 cout<<sum<<endl; 61 } 62 return 0; 63 }