给定一个正数n,我们需要精确打印k个正数(均大于1),以使这k个数的乘积为n。如果不存在这样的k个数字,则打印-1。如果有很多可能的答案,则必须打印其中一个对k个数字进行排序的答案。
例子:
Input : n = 54, k = 3
Output : 2, 3, 9
Note that 2, 3 and 9 are k numbers
with product equals to n.
Input : n = 54, k = 8
Output : -1
这个问题使用非常相似的想法来打印给定数字的所有素数。
这个想法很简单。首先,我们计算n的所有素数因子并将其存储在向量中。注意,我们存储每个质数与其在质因数分解中出现的次数一样多。现在要找到k个大于1的数字,我们检查向量的大小是否大于或等于k。
- 如果大小小于k,则打印-1。
- 否则,我们打印第一个k-1因数,因为它来自矢量,最后一个因数是矢量所有其余元素的乘积。
请注意,我们以排序方式插入了所有素数,因此对向量中的所有数字都进行了排序。这也满足了我们对k个数的排序条件。
C++
// C++ program to find if it is possible to
// write a number n as product of exactly k
// positive numbers greater than 1.
#include
using namespace std;
// Prints k factors of n if n can be written
// as multiple of k numbers. Else prints -1.
void kFactors(int n, int k)
{
// A vector to store all prime factors of n
vector P;
// Insert all 2's in vector
while (n%2 == 0)
{
P.push_back(2);
n /= 2;
}
// n must be odd at this point
// So we skip one element (i = i + 2)
for (int i=3; i*i<=n; i=i+2)
{
while (n%i == 0)
{
n = n/i;
P.push_back(i);
}
}
// This is to handle when n > 2 and
// n is prime
if (n > 2)
P.push_back(n);
// If size(P) < k, k factors are not possible
if (P.size() < k)
{
cout << "-1" << endl;
return;
}
// printing first k-1 factors
for (int i=0; i
Java
// Java program to find if it is possible to
// write a number n as product of exactly k
// positive numbers greater than 1.
import java.util.*;
class GFG
{
// Prints k factors of n if n can be written
// as multiple of k numbers. Else prints -1.
static void kFactors(int n, int k)
{
// A vector to store all prime factors of n
ArrayList P = new ArrayList();
// Insert all 2's in list
while (n % 2 == 0)
{
P.add(2);
n /= 2;
}
// n must be odd at this point
// So we skip one element (i = i + 2)
for (int i = 3; i * i <= n; i = i + 2)
{
while (n % i == 0)
{
n = n / i;
P.add(i);
}
}
// This is to handle when n > 2 and
// n is prime
if (n > 2)
P.add(n);
// If size(P) < k, k factors are
// not possible
if (P.size() < k)
{
System.out.println("-1");
return;
}
// printing first k-1 factors
for (int i = 0; i < k - 1; i++)
System.out.print(P.get(i) + ", ");
// calculating and printing product
// of rest of numbers
int product = 1;
for (int i = k - 1; i < P.size(); i++)
product = product * P.get(i);
System.out.println(product);
}
// Driver code
public static void main(String[] args)
{
int n = 54, k = 3;
kFactors(n, k);
}
}
// This code is contributed
// by chandan_jnu
Python3
# Python3 program to find if it is possible
# to write a number n as product of exactly k
# positive numbers greater than 1.
import math as mt
# Prints k factors of n if n can be written
# as multiple of k numbers. Else prints -1
def kFactors(n, k):
# list to store all prime factors of n
a = list()
#insert all 2's in list
while n % 2 == 0:
a.append(2)
n = n // 2
# n must be odd at this point
# so we skip one element(i=i+2)
for i in range(3, mt.ceil(mt.sqrt(n)), 2):
while n % i == 0:
n = n / i;
a.append(i)
# This is to handle when n>2 and
# n is prime
if n > 2:
a.append(n)
# if size(a)
C#
// C# program to find if it is possible to
// write a number n as product of exactly k
// positive numbers greater than 1.
using System;
using System.Collections;
class GFG
{
// Prints k factors of n if n can be written
// as multiple of k numbers. Else prints -1.
static void kFactors(int n, int k)
{
// A vector to store all prime factors of n
ArrayList P = new ArrayList();
// Insert all 2's in list
while (n % 2 == 0)
{
P.Add(2);
n /= 2;
}
// n must be odd at this point
// So we skip one element (i = i + 2)
for (int i = 3; i * i <= n; i = i + 2)
{
while (n % i == 0)
{
n = n / i;
P.Add(i);
}
}
// This is to handle when n > 2 and
// n is prime
if (n > 2)
P.Add(n);
// If size(P) < k, k factors are not possible
if (P.Count < k)
{
Console.WriteLine("-1");
return;
}
// printing first k-1 factors
for (int i = 0; i < k - 1; i++)
Console.Write(P[i]+", ");
// calculating and printing product of rest
// of numbers
int product = 1;
for (int i = k - 1; i < P.Count; i++)
product = product*(int)P[i];
Console.WriteLine(product);
}
// Driver code
static void Main()
{
int n = 54, k = 3;
kFactors(n, k);
}
}
// This code is contributed by chandan_jnu
PHP
2 and
// n is prime
if ($n > 2)
array_push($P, $n);
// If size(P) < k, k factors are
// not possible
if (count($P) < $k)
{
echo "-1\n";
return;
}
// printing first k-1 factors
for ($i = 0; $i < $k - 1; $i++)
echo $P[$i] . ", ";
// calculating and printing product
// of rest of numbers
$product = 1;
for ($i = $k - 1; $i < count($P); $i++)
$product = $product * $P[$i];
echo $product;
}
// Driver Code
$n = 54;
$k = 3;
kFactors($n, $k);
// This code is contributed by mits
?>
Javascript
输出:
2, 3, 9