所有子集素数的乘积
给定一个大小为N的数组a[] 。子集的值是该子集中素数的乘积。在寻找价值副产品时,非素数被认为是 1。任务是找到所有可能子集的值的乘积。
例子:
Input: a[] = {3, 7}
Output: 20
The subsets are: {3} {7} {3, 7}
{3, 7} = 3 * 7 = 21
{3} = 3
{7} = 7
21 * 3 * 7 = 441
Input: a[] = {10, 2, 14, 3}
Output: 1679616
朴素方法:一种朴素的方法是使用幂集找到所有子集,然后通过将子集的所有值相乘来找到乘积。可以使用 Sieve 检查 Prime。
时间复杂度: O(2 N )
有效方法:一种有效的方法是使用观察来解决问题。如果我们写出所有子序列,一个共同的观察点是每个数字在一个子集中出现2 (N-1)次,因此将导致2 (N-1)作为对乘积的贡献。遍历数组并检查数组中的元素是否为素数。如果它是素数,那么它对答案的贡献是arr[i] 2(N-1)次。
下面是上述方法的实现:
C++
// C++ program to find the product of
// the multiplication of
// prime numbers in all possible subsets.
#include
using namespace std;
// Sieve method to check prime or not
void sieve(int n, vector& prime)
{
// Initially mark all primes
for (int i = 2; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
// Iterate and mark all the
// non primes as false
for (int i = 2; i <= n; i++) {
if (prime[i]) {
// Multiples of prime marked as false
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
}
// Function to find the sum
// of sum of all the subset
int sumOfSubset(int a[], int n)
{
// Get the maximum element
int maxi = *max_element(a, a + n);
// Declare a sieve array
vector prime(maxi + 1);
// Sieve function called
sieve(maxi, prime);
// Number of times an element
// contributes to the answer
int times = pow(2, n - 1);
int sum = 1;
// Iterate and check
for (int i = 0; i < n; i++) {
// If prime
if (prime[a[i]])
sum = sum * (pow(a[i], times)); // Contribution
}
return sum;
}
// Driver Code
int main()
{
int a[] = { 3, 7 };
int n = sizeof(a) / sizeof(a[0]);
cout << sumOfSubset(a, n);
}
Java
// Java program to find the product of
// the multiplication of
// prime numbers in all possible subsets.
import java.util.*;
class GFG
{
// Sieve method to check prime or not
static void sieve(int n, boolean []prime)
{
// Initially mark all primes
for (int i = 2; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
// Iterate and mark all the
// non primes as false
for (int i = 2; i <= n; i++)
{
if (prime[i])
{
// Multiples of prime marked as false
for (int j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int a[], int n)
{
// Get the maximum element
int maxi = Arrays.stream(a).max().getAsInt();
// Declare a sieve array
boolean []prime = new boolean[maxi + 1];
// Sieve function called
sieve(maxi, prime);
// Number of times an element
// contributes to the answer
int times = (int) Math.pow(2, n - 1);
int sum = 1;
// Iterate and check
for (int i = 0; i < n; i++)
{
// If prime
if (prime[a[i]])
sum = (int) (sum * (Math.pow(a[i], times)));
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 3, 7 };
int n = a.length;
System.out.println(sumOfSubset(a, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find the product of
# the multiplication of
# prime numbers in all possible subsets.
prime = [True for i in range(100)]
# Sieve method to check prime or not
def sieve(n, prime):
# Initially mark all primes
for i in range(1, n + 1):
prime[i] = True
prime[0] = prime[1] = False
# Iterate and mark all the
# non primes as false
for i in range(2, n + 1):
if (prime[i]):
# Multiples of prime marked as false
for j in range(2 * i, n + 1, i):
prime[j] = False
# Function to find the Sum
# of Sum of all the subset
def SumOfSubset(a, n):
# Get the maximum element
maxi = max(a)
# Declare a sieve array
# Sieve function called
sieve(maxi, prime)
# Number of times an element
# contributes to the answer
times = pow(2, n - 1)
Sum = 1
# Iterate and check
for i in range(n):
# If prime
if (prime[a[i]]):
Sum = Sum * (pow(a[i], times)) # Contribution
return Sum
# Driver Code
a = [3, 7]
n = len(a)
print(SumOfSubset(a, n))
# This code is contributed
# by Mohit Kumar
C#
// C# program to find the product of
// the multiplication of
// prime numbers in all possible subsets.
using System;
using System.Linq;
class GFG
{
// Sieve method to check prime or not
static void sieve(int n, Boolean []prime)
{
// Initially mark all primes
for (int i = 2; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
// Iterate and mark all the
// non primes as false
for (int i = 2; i <= n; i++)
{
if (prime[i])
{
// Multiples of prime marked as false
for (int j = i * i; j <= n; j += i)
{
prime[j] = false;
}
}
}
}
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
// Get the maximum element
int maxi = a.Max();
// Declare a sieve array
Boolean []prime = new Boolean[maxi + 1];
// Sieve function called
sieve(maxi, prime);
// Number of times an element
// contributes to the answer
int times = (int) Math.Pow(2, n - 1);
int sum = 1;
// Iterate and check
for (int i = 0; i < n; i++)
{
// If prime
if (prime[a[i]])
sum = (int) (sum * (Math.Pow(a[i], times)));
}
return sum;
}
// Driver Code
public static void Main(String[] args)
{
int []a = { 3, 7 };
int n = a.Length;
Console.WriteLine(sumOfSubset(a, n));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
441
时间复杂度:O(M log M) 用于预计算,其中 M 是最大元素,O(N) 用于迭代。
空间复杂度:O(M)
注意:由于arr[i] 2(N-1)可能非常大,答案可能会溢出,最好使用更大的数据类型和 mod 操作来保存答案。