Sum of product of all subsets formed by only divisors of N
Given a number N, the task is to find the sum of the product of elements of all the possible subsets formed by only divisors of N.
Examples:
Input: N = 3
Output: 7
Explanation:
Divisors of 3 are 1 and 3. All possible subsets are {1}, {3}, {1, 3}.
Therefore, the sum of the product of all possible subsets are:
(1 + 3 + (1 * 3)) = 7.
Input: N = 4
Output: 29
Explanation:
Divisors of 4 are 1, 2 and 4. All possible subsets are {1}, {2}, {4}, {1, 2}, {1, 4}, {2, 4}, {1, 2, 4}.
Therefore, the sum of the product of all possible subsets are:
(1 + 2 + 4 + (1 * 2) + (1 * 4) + (2 * 4) + (1 * 2 * 4)) = 29.
Naive Approach: The naive approach for this problem is to generate all possible subsets from its divisors and then calculate the product of each of the subset. After calculating the product of every subset, add all the products to find the required answer. The time complexity for this approach is O(2D) where D is the number of divisors of N.
Efficient Approach: The idea behind the efficient approach comes from the following observation:
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
Clearly, from the above observation, we can conclude that if {D1, D2, D3, … Dn} are the divisors of N then the required answer will be:
(D1 + 1) * (D2 + 1) * (D3 + 1) * ... (Dn + 1)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int GetSum( int n)
{
vector< int > divisors;
for ( int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisors.push_back(i);
if (i != n / i) {
divisors.push_back(n / i);
}
}
}
int ans = 1;
for ( auto i : divisors) {
ans *= (i + 1);
}
ans = ans - 1;
return ans;
}
int main()
{
int N = 4;
cout << GetSum(N) << endl;
}
|
Java
import java.util.*;
class GFG {
static int GetSum( int n)
{
Vector<Integer> divisors = new Vector<Integer>();
for ( int i = 1 ; i * i <= n; i++)
{
if (n % i == 0 )
{
divisors.add(i);
if (i != n / i)
{
divisors.add(n / i);
}
}
}
int ans = 1 ;
for ( int i = 0 ; i < divisors.size(); i++)
{
ans *= (divisors.get(i) + 1 );
}
ans = ans - 1 ;
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
System.out.println(GetSum(N));
}
}
|
Python3
def GetSum(n):
divisors = []
i = 1
while i * i < = n :
if (n % i = = 0 ):
divisors.append(i)
if (i ! = n / / i):
divisors.append(n / / i)
i + = 1
ans = 1
for i in divisors:
ans * = (i + 1 )
ans = ans - 1
return ans
if __name__ = = "__main__" :
N = 4
print (GetSum(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int GetSum( int n)
{
List< int > divisors = new List< int >();
for ( int i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
divisors.Add(i);
if (i != n / i)
{
divisors.Add(n / i);
}
}
}
int ans = 1;
foreach ( int i in divisors)
{
ans *= (i + 1);
}
ans = ans - 1;
return ans;
}
public static void Main()
{
int N = 4;
Console.WriteLine(GetSum(N));
}
}
|
Javascript
<script>
function GetSum(n)
{
var divisors = [];
for ( var i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisors.push(i);
if (i != n / i) {
divisors.push(n / i);
}
}
}
var ans = 1;
for ( var i =0; i< divisors.length; i++)
{
ans *= (divisors[i]+1);
}
ans = ans - 1;
return ans;
}
var N = 4;
document.write( GetSum(N));
</script>
|
Time Complexity: O(sqrt(N)), where N is the given number.
Auxiliary Space: O(sqrt(N))
Last Updated :
09 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...