给定两个正整数K和X ,任务是找到具有 LCM X的K 个正整数(允许重复)的最小可能总和。
例子:
Input: K = 2, X = 6
Output: 5
Explanation:
K(= 2) positive integers of minimum possible sum having LCM X(= 6) are { 2, 3 }.
Therefore, the minimum possible sum of K(= 2) positive integers = (2 + 3) = 5.
Therefore, the required output is 5.
Input: K = 3 X = 11
Output: 13
Explanation:
K(= 3) positive integers of minimum possible sum having LCM X(= 11) are { 1, 1, 11 }.
Therefore, the minimum possible sum of K(= 3) positive integers = (1 + 1 + 11) = 13.
Therefore, the required output is 13.
方法:该问题可以使用贪心技术解决。这个想法是以素数幂的乘积的形式来表示X。从X的所有素幂中以各种可能的方式选择K 个素幂并计算它们各自的和。最后,打印所有这些中可能的最小总和。请按照以下步骤解决问题:
- 初始化一个数组,比如primePow[] ,以存储X 的所有素数幂。
- 如果阵列primePow []的长度小于或等于K,则包括primePow []以K的正整数和所有的数组元素中的k的正整数必须是1的剩余元件。最后,打印K 个正整数的总和
Illustration:
If K = 5, X = 240
primePow[] = { 24, 31, 51 } = { 16, 3, 5 }
K positive integers will be { 16, 3, 5, 1, 1 }
Therefore, the sum of K positive integers will be 26
- 否则,以所有可能的方式将primePow[]数组划分为K 个组并计算它们各自的总和。最后,打印K 个正整数的最小和。
If K = 3, X = 210
primePow[] = { 21, 31, 51, 51 } = { 2, 3, 5, 7 }
Partition primePow[] array into { { 2, 3 }, { 5 }, { 7 } }
210 = (2 * 3) * (5) * 7 = 6 * 5 * 7
K positive integers will be { 6, 5, 7 }
Therefore, the sum of K(= 3) positive integers will be 18
下面是上述实现的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the prime
// power of X
vector primePower(int X)
{
// Stores prime powers of X
vector primePow;
// Iterate over the range [2, sqrt(X)]
for (int i = 2; i * i <= X; i++) {
// If X is divisible by i
if (X % i == 0) {
// Stores prime power
int p = 1;
// Calculate prime power
// of X
while (X % i == 0) {
// Update X
X /= i;
// Update p
p *= i;
}
// Insert prime powers
// into primePow[]
primePow.push_back(p);
}
}
// If X exceeds 1
if (X > 1) {
primePow.push_back(X);
}
return primePow;
}
// Function to calculate the
// sum of array elements
int getSum(vector& ar)
{
// Stores sum of
// array elements
int sum = 0;
// Traverse the array
for (int i : ar) {
// Update sum
sum += i;
}
return sum;
}
// Function to partition array into K groups such
// that sum of elements of the K groups is minimum
int getMinSum(int pos, vector& arr,
vector& primePow)
{
// If count of prime powers
// is equal to pos
if (pos == primePow.size()) {
return getSum(arr);
}
// Stores minimum sum of of each
// partition of arr[] into K groups
int res = INT_MAX;
// Traverse the array arr[]
for (int i = 0; i < arr.size();
i++) {
// Include primePow[pos] into
// i-th groups
arr[i] *= primePow[pos];
// Update res
res = min(res, getMinSum(pos + 1,
arr, primePow));
// Remove factors[pos] from
// i-th groups
arr[i] /= primePow[pos];
}
return res;
}
// Utility function to calculate minimum sum
// of K positive integers whose LCM is X
int minimumSumWithGivenLCM(int k, int x)
{
// Stores all prime powers of X
vector primePow = primePower(x);
// Stores count of prime powers
int n = primePow.size();
// Stores minimum sum of K positive
// integers whose LCM is X
int sum = 0;
// If n is less than
// or equal to k
if (n <= k) {
// Traverse primePow[] array
for (int i : primePow) {
// Update sum
sum += i;
}
// Update sum
sum += k - n;
}
else {
// arr[i]: Stores element in i-th group
// by partitioning the primePow[] array
vector arr(k, 1);
// Update sum
sum = getMinSum(0, arr, primePow);
}
return sum;
}
// Driver Code
int main()
{
int k = 3, x = 210;
cout << minimumSumWithGivenLCM(k, x);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the prime
// power of X
static Vector primePower(int X)
{
// Stores prime powers of X
Vector primePow = new Vector();
// Iterate over the range [2, Math.sqrt(X)]
for (int i = 2; i * i <= X; i++) {
// If X is divisible by i
if (X % i == 0) {
// Stores prime power
int p = 1;
// Calculate prime power
// of X
while (X % i == 0) {
// Update X
X /= i;
// Update p
p *= i;
}
// Insert prime powers
// into primePow[]
primePow.add(p);
}
}
// If X exceeds 1
if (X > 1) {
primePow.add(X);
}
return primePow;
}
// Function to calculate the
// sum of array elements
static int getSum(int []ar)
{
// Stores sum of
// array elements
int sum = 0;
// Traverse the array
for (int i : ar) {
// Update sum
sum += i;
}
return sum;
}
// Function to partition array into K groups such
// that sum of elements of the K groups is minimum
static int getMinSum(int pos, int []arr,
Vector primePow)
{
// If count of prime powers
// is equal to pos
if (pos == primePow.size()) {
return getSum(arr);
}
// Stores minimum sum of of each
// partition of arr[] into K groups
int res = Integer.MAX_VALUE;
// Traverse the array arr[]
for (int i = 0; i < arr.length;
i++) {
// Include primePow[pos] into
// i-th groups
arr[i] *= primePow.get(pos);
// Update res
res = Math.min(res, getMinSum(pos + 1,
arr, primePow));
// Remove factors[pos] from
// i-th groups
arr[i] /= primePow.get(pos);
}
return res;
}
// Utility function to calculate minimum sum
// of K positive integers whose LCM is X
static int minimumSumWithGivenLCM(int k, int x)
{
// Stores all prime powers of X
Vector primePow = primePower(x);
// Stores count of prime powers
int n = primePow.size();
// Stores minimum sum of K positive
// integers whose LCM is X
int sum = 0;
// If n is less than
// or equal to k
if (n <= k) {
// Traverse primePow[] array
for (int i : primePow) {
// Update sum
sum += i;
}
// Update sum
sum += k - n;
}
else {
// arr[i]: Stores element in i-th group
// by partitioning the primePow[] array
int []arr = new int[k];
Arrays.fill(arr, 1);
// Update sum
sum = getMinSum(0, arr, primePow);
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
int k = 3, x = 210;
System.out.print(minimumSumWithGivenLCM(k, x));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to find the prime
# power of X
def primePower(X):
# Stores prime powers of X
primePow = []
# Iterate over the range [2, sqrt(X)]
for i in range(2, X + 1):
if i * i > X + 1:
break
# If X is divisible by i
if (X % i == 0):
# Stores prime power
p = 1
# Calculate prime power
# of X
while (X % i == 0):
# Update X
X //= i
# Update p
p *= i
# Insert prime powers
# into primePow[]
primePow.append(p)
# If X exceeds 1
if (X > 1):
primePow.append(X)
return primePow
# Function to calculate the
# sum of array elements
def getSum(ar):
# Stores sum of
# array elements
sum = 0
# Traverse the array
for i in ar:
# Update sum
sum += i
return sum
# Function to partition array into K groups such
# that sum of elements of the K groups is minimum
def getMinSum(pos, arr, primePow):
# If count of prime powers
# is equal to pos
if (pos == len(primePow)):
return getSum(arr)
# Stores minimum sum of of each
# partition of arr[] into K groups
res = 10**9
# Traverse the array arr[]
for i in range(len(arr)):
# Include primePow[pos] into
# i-th groups
arr[i] *= primePow[pos]
# Update res
res = min(res, getMinSum(pos + 1, arr, primePow))
#Remove factors[pos] from
#i-th groups
arr[i] //= primePow[pos]
return res
# Utility function to calculate minimum sum
# of K positive integers whose LCM is X
def minimumSumWithGivenLCM(k, x):
# Stores all prime powers of X
primePow = primePower(x)
# Stores count of prime powers
n = len(primePow)
# Stores minimum sum of K positive
# integers whose LCM is X
sum = 0
# If n is less than
# or equal to k
if (n <= k):
# Traverse primePow[] array
for i in primePow:
# Update sum
sum += i
# Update sum
sum += k - n
else:
# arr[i]: Stores element in i-th group
# by partitioning the primePow[] array
arr = [1] * (k)
# Update sum
sum = getMinSum(0, arr, primePow)
return sum
# Driver Code
if __name__ == '__main__':
k = 3
x = 210
print(minimumSumWithGivenLCM(k, x))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the prime
// power of X
static List primePower(int X)
{
// Stores prime powers of X
List primePow = new List();
// Iterate over the range [2, Math.Sqrt(X)]
for(int i = 2; i * i <= X; i++)
{
// If X is divisible by i
if (X % i == 0)
{
// Stores prime power
int p = 1;
// Calculate prime power
// of X
while (X % i == 0)
{
// Update X
X /= i;
// Update p
p *= i;
}
// Insert prime powers
// into primePow[]
primePow.Add(p);
}
}
// If X exceeds 1
if (X > 1)
{
primePow.Add(X);
}
return primePow;
}
// Function to calculate the
// sum of array elements
static int getSum(int []ar)
{
// Stores sum of
// array elements
int sum = 0;
// Traverse the array
foreach(int i in ar)
{
// Update sum
sum += i;
}
return sum;
}
// Function to partition array into K groups such
// that sum of elements of the K groups is minimum
static int getMinSum(int pos, int []arr,
List primePow)
{
// If count of prime powers
// is equal to pos
if (pos == primePow.Count)
{
return getSum(arr);
}
// Stores minimum sum of of each
// partition of []arr into K groups
int res = int.MaxValue;
// Traverse the array []arr
for(int i = 0; i < arr.Length; i++)
{
// Include primePow[pos] into
// i-th groups
arr[i] *= primePow[pos];
// Update res
res = Math.Min(res, getMinSum(
pos + 1, arr, primePow));
// Remove factors[pos] from
// i-th groups
arr[i] /= primePow[pos];
}
return res;
}
// Utility function to calculate minimum sum
// of K positive integers whose LCM is X
static int minimumSumWithGivenLCM(int k, int x)
{
// Stores all prime powers of X
List primePow = primePower(x);
// Stores count of prime powers
int n = primePow.Count;
// Stores minimum sum of K positive
// integers whose LCM is X
int sum = 0;
// If n is less than
// or equal to k
if (n <= k)
{
// Traverse primePow[] array
foreach(int i in primePow)
{
// Update sum
sum += i;
}
// Update sum
sum += k - n;
}
else
{
// arr[i]: Stores element in i-th group
// by partitioning the primePow[] array
int []arr = new int[k];
for(int l = 0; l < arr.Length; l++)
arr[l] = 1;
// Update sum
sum = getMinSum(0, arr, primePow);
}
return sum;
}
// Driver Code
public static void Main(String[] args)
{
int k = 3, x = 210;
Console.Write(minimumSumWithGivenLCM(k, x));
}
}
// This code is contributed by aashish1995
Javascript
18
时间复杂度: O(sqrt(X) + 3 Y ),其中 Y 是质因子的最大数量
辅助空间: O(K + Y)