Sum of sum of all subsets of a set formed by first N natural numbers
Last Updated :
21 Sep, 2022
Given N, and ff(N) = f(1) + f(2) + …… + f(N), where f(k) is the sum of all subsets of a set formed by first k natural numbers. The task is to find ff(N) modulo 1000000007.
Examples:
Input: 2
Output: 7
f(1) + f(2)
f(1) = 1 = 1
f(2) = 1 + 2 + {1 + 2} = 6
Input: 3
Output: 31
f(1) + f(2) + f(3)
f(1) = 1 = 1
f(2) = 1 + 2 + {1 + 2} = 6
f(3) = 1 + 2 + 3 + {1 + 2} + {2 + 3} + {1 + 3} + {1 + 2 + 3} = 24
Approach: Find a pattern of the sequence that will form. The values of f(1), f(2), f(3) are 1, 6 and 31 respectively. Let’s find f(4).
f(4) = 1 + 2 + 3 + 4 + {1 + 2} + {1 + 3} + {1 + 4}
+ {2 + 3} + {2 + 4} + {3 + 4} + {1 + 2 + 3} + {1 + 2 + 4}
+ {1 + 3 + 4} + {2 + 3 + 4} + {1 + 2 + 3 + 4} = 80.
Hence ff(N) will be
ff(1) = f(1) = 1
ff(2) = f(1) + f(2) = 7
ff(3) = f(1) + f(2) + f(3) = 31
ff(4) = f(1) + f(2) + f(3) + f(4) = 111
.
.
.
The series formed is 1, 7, 31, 111… There exists a formula for it which is 2^n*(n^2 + n + 2) – 1. where, N is starting from zero.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define mod (int)(1e9 + 7)
int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int check( int n)
{
n--;
int ans = n * n;
if (ans >= mod)
ans %= mod;
ans += n + 2;
if (ans >= mod)
ans %= mod;
ans = (power(2, n, mod) % mod * ans % mod) % mod;
ans = (ans - 1 + mod) % mod;
return ans;
}
int main()
{
int n = 4;
cout << check(n) << endl;
return 0;
}
|
C
#include <stdio.h>
#define mod (int)(1e9 + 7)
int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int check( int n)
{
n--;
int ans = n * n;
if (ans >= mod)
ans %= mod;
ans += n + 2;
if (ans >= mod)
ans %= mod;
ans = (power(2, n, mod) % mod * ans % mod) % mod;
ans = (ans - 1 + mod) % mod;
return ans;
}
int main()
{
int n = 4;
printf ( "%d\n" ,check(n));
return 0;
}
|
Java
class Geeks {
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 ) {
if (y != 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int check( int n)
{
int mod = ( int )(1e9 + 7 );
n--;
int ans = n * n;
if (ans >= mod)
ans %= mod;
ans += n + 2 ;
if (ans >= mod)
ans %= mod;
ans = (power( 2 , n, mod) % mod *
ans % mod) % mod;
ans = (ans - 1 + mod) % mod;
return ans;
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(check(n));
}
}
|
Python3
mod = ( int )( 1e9 + 7 )
def power(x,y,p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def check(n):
n = n - 1
ans = n * n
if (ans > = mod):
ans % = mod
ans + = n + 2
if (ans > = mod):
ans % = mod
ans = ( pow ( 2 , n, mod) % mod * ans % mod) % mod
ans = (ans - 1 + mod) % mod
return ans
if __name__ = = '__main__' :
n = 4
print (check(n))
|
C#
using System;
class GFG
{
static int power( int x, int y,
int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int check( int n)
{
int mod = ( int )(1e9 + 7);
n--;
int ans = n * n;
if (ans >= mod)
ans %= mod;
ans += n + 2;
if (ans >= mod)
ans %= mod;
ans = (power(2, n, mod) % mod *
ans % mod) % mod;
ans = (ans - 1 + mod) % mod;
return ans;
}
public static void Main(String []args)
{
int n = 4;
Console.WriteLine(check(n));
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function check( $n )
{
$mod = 1e9+7;
$n --;
$ans = $n * $n ;
if ( $ans >= $mod )
$ans %= $mod ;
$ans += $n + 2;
if ( $ans >= $mod )
$ans %= $mod ;
$ans = (power(2, $n , $mod ) %
$mod * $ans %
$mod ) % $mod ;
$ans = ( $ans - 1 + $mod ) % $mod ;
return $ans ;
}
$n = 4;
echo check( $n ) . "\n" ;
?>
|
Javascript
<script>
const mod = (1e9 + 7);
function power( x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function check( n)
{
n--;
let ans = n * n;
if (ans >= mod)
ans %= mod;
ans += n + 2;
if (ans >= mod)
ans %= mod;
ans = (power(2, n, mod) % mod * ans % mod) % mod;
ans = (ans - 1 + mod) % mod;
return ans;
}
let n = 4;
document.write(check(n));
</script>
|
Time complexity: O(log n).
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...