k-Factorization

Dream2016 is a cat hospital grade students. Half of the examination of human culture lesson this morning, there is a question: given a positive integer n, find the k integer greater than 1 (repeat) so that their product is equal to n.
The Input
The first line contains two integers n and K (n-2 ≤ ≤ 100000,. 1 ≤ K ≤ 20 is).
the output
If there is no valid answer outputs -1.
Otherwise, an arbitrary integer k sequentially output. Their product must be equal to n. If there are multiple schemes, any one can be output.
Example

Input

5 1

Output

5

Input

5 2

Output

-1

Input

1024 5

Output

2 64 2 2 2

#include<cstdio>
#include<vector>
#include <iostream>
#include<queue>
#define maxn 5005
#define maxm 200005
#define ri register int
using namespace std;
int n,m,k;
int a[120];
int main()
{
 while(cin>>n>>k)
 {
  if(k==1) 
  {
   printf("%d\n",n);
   continue;
  }
  int z=0;
  while(1)
  {
   for(ri i=2;i<=n;i++)
   {
    if(n%i==0)
    {
     n/=i;
     a[z++]=i;
     break;
    }
   }
   if(z==k-1||n==1)
   break;
  }
  if(z<=k-1&&n==1)
  {
   printf("-1\n");
  }
  else
  {
   for(ri i=0;i<z;i++)
   printf("%d ",a[i]);
   printf("%d\n",n);
  }
 }
}
Published 12 original articles · won praise 1 · views 194

Guess you like

Origin blog.csdn.net/csx_zzh/article/details/105115604