📜  仅由N的除数形成的所有子集的乘积和

📅  最后修改于: 2021-04-22 07:19:08             🧑  作者: Mango

给定一个数N ,任务是找到仅由N的除数形成的所有可能子集的元素乘积之和。
例子:

朴素方法:针对此问题的朴素方法是从其除数生成所有可能的子集,然后计算每个子集的乘积。计算完每个子集的乘积后,将所有乘积相加,以找到所需的答案。此方法的时间复杂度为O(2 D ) ,其中D是N的除数的数量。

高效方法:高效方法背后的思想来自以下观察:

Let x and y is the divisor of N. 
Then sum of product of all subsets will be
   = x + y + x * y 
   = x(1 + y) + y + 1 - 1 
   = x(1 + y) + (1 + y) - 1 
   = (x + 1) * (y + 1) - 1
   = (1 + x) * (1 + y) - 1

Now let take three divisors x, y, z. 
Then sum of product of all subsets will be
   = x + y + z + xy + yz + zx + xyz 
   = x + xz + y + yz + xy + xyz + z + 1 - 1
   = x(1 + z) + y(1 + z) + xy(1 + z) + z + 1 - 1
   = (x + y + xy + 1) * (1 + z) - 1 
   = (1 + x) * (1 + y) * (1 + z) - 1

显然,根据上述观察结果,我们可以得出结论,如果{D 1 ,D 2 ,D 3 ,…D n }是N的除数,则所需答案将是:

(D1 + 1) * (D2 + 1) * (D3 + 1) * ... (Dn + 1)

下面是上述方法的实现:

C++
// C++ program to find the sum of
// product of all the subsets
// formed by only divisors of N
  
#include 
using namespace std;
  
// Function to find the sum of
// product of all the subsets
// formed by only divisors of N
int GetSum(int n)
{
  
    // Vector to store all the
    // divisors of n
    vector divisors;
  
    // Loop to find out the
    // divisors of N
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
  
            // Both 'i' and 'n/i' are the
            // divisors of n
            divisors.push_back(i);
  
            // Check if 'i' and 'n/i' are
            // equal or not
            if (i != n / i) {
                divisors.push_back(n / i);
            }
        }
    }
  
    int ans = 1;
  
    // Calculating the answer
    for (auto i : divisors) {
        ans *= (i + 1);
    }
  
    // Excluding the value
    // of the empty set
    ans = ans - 1;
  
    return ans;
}
  
// Driver Code
int main()
{
  
    int N = 4;
  
    cout << GetSum(N) << endl;
}


Java
// Java program to find the sum of product
// of all the subsets formed by only 
// divisors of N
import java.util.*;
class GFG {
  
// Function to find the sum of product
// of all the subsets formed by only 
// divisors of N
static int GetSum(int n)
{
  
    // Vector to store all the 
    // divisors of n 
    Vector divisors = new Vector();  
  
    // Loop to find out the
    // divisors of N
    for(int i = 1; i * i <= n; i++)
    {
       if (n % i == 0)
       {
             
           // Both 'i' and 'n/i' are the
           // divisors of n
           divisors.add(i);
             
           // Check if 'i' and 'n/i' are
           // equal or not
           if (i != n / i)
           {
               divisors.add(n / i);
           }
       }
    }
  
    int ans = 1;
  
    // Calculating the answer
    for(int i = 0; i < divisors.size(); i++)
    {
       ans *= (divisors.get(i) + 1);
    }
  
    // Excluding the value
    // of the empty set
    ans = ans - 1;
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 4;
  
    System.out.println(GetSum(N));
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program to find the sum of
# product of all the subsets
# formed by only divisors of N
  
# Function to find the sum of
# product of all the subsets
# formed by only divisors of N
def GetSum(n):
  
    # Vector to store all the
    # divisors of n
    divisors = []
  
    # Loop to find out the
    # divisors of N
    i = 1
    while i * i <= n :
        if (n % i == 0):
  
            # Both 'i' and 'n/i' are the
            # divisors of n
            divisors.append(i)
  
            # Check if 'i' and 'n/i' are
            # equal or not
            if (i != n // i):
                divisors.append(n // i)
                  
        i += 1
    ans = 1
  
    # Calculating the answer
    for i in divisors:
        ans *= (i + 1)
  
    # Excluding the value
    # of the empty set
    ans = ans - 1
    return ans
  
# Driver Code
if __name__ == "__main__":
      
    N = 4
    print(GetSum(N))
  
# This code is contributed by chitranayal


C#
// C# program to find the sum of product 
// of all the subsets formed by only 
// divisors of N 
using System;
using System.Collections.Generic; 
  
class GFG{ 
  
// Function to find the sum of product 
// of all the subsets formed by only 
// divisors of N 
static int GetSum(int n) 
{ 
  
    // Store all the 
    // divisors of n 
    List divisors = new List(); 
  
    // Loop to find out the 
    // divisors of N 
    for(int i = 1; i * i <= n; i++) 
    { 
        if (n % i == 0) 
        { 
                  
            // Both 'i' and 'n/i' are the 
            // divisors of n 
            divisors.Add(i); 
                  
            // Check if 'i' and 'n/i' are 
            // equal or not 
            if (i != n / i) 
            { 
                divisors.Add(n / i); 
            } 
        } 
    } 
  
    int ans = 1; 
  
    // Calculating the answer 
    foreach(int i in divisors) 
    { 
        ans *= (i + 1); 
    } 
  
    // Excluding the value 
    // of the empty set 
    ans = ans - 1; 
  
    return ans; 
} 
  
// Driver code 
public static void Main() 
{ 
    int N = 4; 
  
    Console.WriteLine(GetSum(N)); 
} 
}
  
// This code is contributed by sanjoy_62


输出:
29

时间复杂度: O(sqrt(N)) ,其中N是给定的数字。