Count of all possible pairs of disjoint subsets of integers from 1 to N - GeeksforGeeks

30 Aug, 2019
Given an integer N. Consider the set of first N natural numbers A = {1, 2, 3, ..., N}. Let M and P be two non-empty subsets of A. The task is to count the number of unordered pairs of (M, P) such that M and P are disjoint sets. Note that the order of M and P doesn’t matter.
Examples:
Input: N = 3Output: 6The unordered pairs are ({1}, {2}), ({1}, {3}),({2}, {3}), ({1}, {2, 3}), ({2}, {1, 3}), ({3}, {1, 2}).
Input: N = 2Output: 1
Input: N = 10Output: 28501
Approach:
Lets assume there are only 6 elements in the set {1, 2, 3, 4, 5, 6}.When you count the number of subsets with 1 as one of the element of first subset, it comes out to be 211.Counting number of subsets with 2 being one of the element of first subset, it comes out to be 65, because 1’s not included as order of sets doesn’t matter.Counting number of subset with 3 being one of the element of first set it comes out to be 65, here a pattern can be observed.Pattern:5 = 3 * 1 + 219 = 3 * 5 + 465 = 3 * 19 + 8211 = 3 * 65 + 16S(n) = 3 * S(n-1) + 2(n – 2)Expanding it until n->2 (means numbers of elements n-2+1=n-1)2(n-2) * 3(0) + 2(n – 3) * 31 + 2(n – 4) * 32 + 2(n – 5) * 33 + ... + 2(0) * 3(n – 2)From Geometric progression, a + a * r0 + a * r1 + ... + a * r(n – 1) = a * (rn – 1) / (r – 1)S(n) = 3(n – 1) – 2(n – 1). Remember S(n) is number of subsets with 1 as a one of the elements of first subset but to get the required result, Denoted by T(n) = S(1) + S(2) + S(3) + ... +S(n)It also forms a Geometric progression, so we calculate it by formula of sum of GPT(n) = (3n – 2n + 1 + 1)/2As we require T(n) % p where p = 109 + 7We have to use Fermats’s little theorema-1 = a(m – 2) (mod m) for modular divisionBelow is the implementation of the above approach:C++JavaPython3C#C++// C++ implementation of the approach  #include <bits/stdc++.h>using namespace std;#define p 1000000007  // Modulo exponentiation functionlong long power(long long x, long long y){    // Function to calculate (x^y)%p in O(log(y))    long long res = 1;    x = x % p;      while (y > 0) {        if (y & 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }      return res % p;}  // Driver functionint main(){    long long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From  Fermats’s little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    cout << x << "\n";}Java// Java implementation of the approachclass GFG { static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codepublic static void main(String[] args) {    long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    System.out.println(x);}}  // This code is contributed by Rajput-JiPython3# Python3 implementation of the approachp = 1000000007  # Modulo exponentiation functiondef power(x, y):          # Function to calculate (x^y)%p in O(log(y))    res = 1    x = x % p      while (y > 0):        if (y & 1):            res = (res * x) % p;        y = y >> 1        x = (x * x) % p      return res % p  # Driver Coden = 3  # Evaluating ((3^n-2^(n+1)+1)/2)%px = (power(3, n) % p + 1) % p  x = (x - power(2, n + 1) + p) % p  # From Fermats’s little theorem# a^-1 ? a^(m-2) (mod m)x = (x * power(2, p - 2)) % p  print(x)  # This code is contributed by Mohit KumarC#// C# implementation of the approachusing System;  class GFG{static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codestatic public void Main (){    long n = 3;          // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;          x = (x - power(2, n + 1) + p) % p;          // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)          x = (x * power(2, p - 2)) % p;    Console.Write(x);}}  // This code is contributed by ajit.Output:6
My Personal Notes
arrow_drop_upSave
Lets assume there are only 6 elements in the set {1, 2, 3, 4, 5, 6}.
When you count the number of subsets with 1 as one of the element of first subset, it comes out to be 211.
Counting number of subsets with 2 being one of the element of first subset, it comes out to be 65, because 1’s not included as order of sets doesn’t matter.
Counting number of subset with 3 being one of the element of first set it comes out to be 65, here a pattern can be observed.
Pattern:5 = 3 * 1 + 219 = 3 * 5 + 465 = 3 * 19 + 8211 = 3 * 65 + 16S(n) = 3 * S(n-1) + 2(n – 2)
5 = 3 * 1 + 219 = 3 * 5 + 465 = 3 * 19 + 8211 = 3 * 65 + 16S(n) = 3 * S(n-1) + 2(n – 2)
Expanding it until n->2 (means numbers of elements n-2+1=n-1)2(n-2) * 3(0) + 2(n – 3) * 31 + 2(n – 4) * 32 + 2(n – 5) * 33 + ... + 2(0) * 3(n – 2)From Geometric progression, a + a * r0 + a * r1 + ... + a * r(n – 1) = a * (rn – 1) / (r – 1)
S(n) = 3(n – 1) – 2(n – 1). Remember S(n) is number of subsets with 1 as a one of the elements of first subset but to get the required result, Denoted by T(n) = S(1) + S(2) + S(3) + ... +S(n)
It also forms a Geometric progression, so we calculate it by formula of sum of GPT(n) = (3n – 2n + 1 + 1)/2
As we require T(n) % p where p = 109 + 7We have to use Fermats’s little theorema-1 = a(m – 2) (mod m) for modular divisionBelow is the implementation of the above approach:C++JavaPython3C#C++// C++ implementation of the approach  #include <bits/stdc++.h>using namespace std;#define p 1000000007  // Modulo exponentiation functionlong long power(long long x, long long y){    // Function to calculate (x^y)%p in O(log(y))    long long res = 1;    x = x % p;      while (y > 0) {        if (y & 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }      return res % p;}  // Driver functionint main(){    long long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From  Fermats’s little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    cout << x << "\n";}Java// Java implementation of the approachclass GFG { static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codepublic static void main(String[] args) {    long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    System.out.println(x);}}  // This code is contributed by Rajput-JiPython3# Python3 implementation of the approachp = 1000000007  # Modulo exponentiation functiondef power(x, y):          # Function to calculate (x^y)%p in O(log(y))    res = 1    x = x % p      while (y > 0):        if (y & 1):            res = (res * x) % p;        y = y >> 1        x = (x * x) % p      return res % p  # Driver Coden = 3  # Evaluating ((3^n-2^(n+1)+1)/2)%px = (power(3, n) % p + 1) % p  x = (x - power(2, n + 1) + p) % p  # From Fermats’s little theorem# a^-1 ? a^(m-2) (mod m)x = (x * power(2, p - 2)) % p  print(x)  # This code is contributed by Mohit KumarC#// C# implementation of the approachusing System;  class GFG{static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codestatic public void Main (){    long n = 3;          // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;          x = (x - power(2, n + 1) + p) % p;          // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)          x = (x * power(2, p - 2)) % p;    Console.Write(x);}}  // This code is contributed by ajit.Output:6
My Personal Notes
arrow_drop_upSave
Below is the implementation of the above approach:C++JavaPython3C#C++// C++ implementation of the approach  #include <bits/stdc++.h>using namespace std;#define p 1000000007  // Modulo exponentiation functionlong long power(long long x, long long y){    // Function to calculate (x^y)%p in O(log(y))    long long res = 1;    x = x % p;      while (y > 0) {        if (y & 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }      return res % p;}  // Driver functionint main(){    long long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From  Fermats’s little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    cout << x << "\n";}Java// Java implementation of the approachclass GFG { static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codepublic static void main(String[] args) {    long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    System.out.println(x);}}  // This code is contributed by Rajput-JiPython3# Python3 implementation of the approachp = 1000000007  # Modulo exponentiation functiondef power(x, y):          # Function to calculate (x^y)%p in O(log(y))    res = 1    x = x % p      while (y > 0):        if (y & 1):            res = (res * x) % p;        y = y >> 1        x = (x * x) % p      return res % p  # Driver Coden = 3  # Evaluating ((3^n-2^(n+1)+1)/2)%px = (power(3, n) % p + 1) % p  x = (x - power(2, n + 1) + p) % p  # From Fermats’s little theorem# a^-1 ? a^(m-2) (mod m)x = (x * power(2, p - 2)) % p  print(x)  # This code is contributed by Mohit KumarC#// C# implementation of the approachusing System;  class GFG{static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codestatic public void Main (){    long n = 3;          // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;          x = (x - power(2, n + 1) + p) % p;          // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)          x = (x * power(2, p - 2)) % p;    Console.Write(x);}}  // This code is contributed by ajit.Output:6
My Personal Notes
arrow_drop_upSave
Below is the implementation of the above approach:
C++
Java
Python3
C#
// C++ implementation of the approach  #include <bits/stdc++.h>using namespace std;#define p 1000000007  // Modulo exponentiation functionlong long power(long long x, long long y){    // Function to calculate (x^y)%p in O(log(y))    long long res = 1;    x = x % p;      while (y > 0) {        if (y & 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }      return res % p;}  // Driver functionint main(){    long long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From  Fermats’s little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    cout << x << "\n";}
// Java implementation of the approachclass GFG { static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codepublic static void main(String[] args) {    long n = 3;      // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;      x = (x - power(2, n + 1) + p) % p;      // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)      x = (x * power(2, p - 2)) % p;    System.out.println(x);}}  // This code is contributed by Rajput-Ji
# Python3 implementation of the approachp = 1000000007  # Modulo exponentiation functiondef power(x, y):          # Function to calculate (x^y)%p in O(log(y))    res = 1    x = x % p      while (y > 0):        if (y & 1):            res = (res * x) % p;        y = y >> 1        x = (x * x) % p      return res % p  # Driver Coden = 3  # Evaluating ((3^n-2^(n+1)+1)/2)%px = (power(3, n) % p + 1) % p  x = (x - power(2, n + 1) + p) % p  # From Fermats’s little theorem# a^-1 ? a^(m-2) (mod m)x = (x * power(2, p - 2)) % p  print(x)  # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System;  class GFG{static int p = 1000000007;  // Modulo exponentiation functionstatic long power(long x, long y){    // Function to calculate (x^y)%p in O(log(y))    long res = 1;    x = x % p;      while (y > 0)    {        if (y % 2 == 1)            res = (res * x) % p;        y = y >> 1;        x = (x * x) % p;    }    return res % p;}  // Driver Codestatic public void Main (){    long n = 3;          // Evaluating ((3^n-2^(n+1)+1)/2)%p    long x = (power(3, n) % p + 1) % p;          x = (x - power(2, n + 1) + p) % p;          // From Fermats's little theorem    // a^-1 ? a^(m-2) (mod m)          x = (x * power(2, p - 2)) % p;    Console.Write(x);}}  // This code is contributed by ajit.
6

mohit kumar 29
Rajput-Ji
jit_t
number-theory
subset
Mathematical
number-theory
Mathematical
subset
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Count all possible paths from top left to bottom right of a mXn matrix
Modular multiplicative inverse
Program to multiply two matrices
Fizz Buzz Implementation
Check if a number is Palindrome
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Min Cost Path | DP-6