CodeForces - 797A【分解质因数】

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
5 1
output
5 
input
5 2
output
-1
input
1024 5
output

2 64 2 2 2

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e+5;
int main()
{
    int n, k;
    int f[maxn];
    cin >> n >> k;
    int cot = 0;
    for (int i = 2; i <= n; i++){
        while(n % i == 0){
            n/=i;
            f[cot++] = i;
        }
    }
    if(cot < k){
       cout << "-1" << endl;
    }
    else{
        for (int i = 0; i < k -1; i++){
            cout << f[i] << " ";
        }
        int sum = 1;
        for (int i = k -1; i < cot; i++){
            sum*=f[i];
        }
        cout << sum <<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37602930/article/details/80385922