Open In App

Find number of ways to form sets from N distinct things with no set of size A or B

Given three numbers N, A, B. The task is to count the number of ways to select things such that there exists no set of size either A or B. Answer can be very large. So, output answer modulo 109+7. Note: Empty set is not consider as one of the way. Examples:

Input: N = 4, A = 1, B = 3 Output: 7 Explanation: The number of ways to form sets of size 2 are 6 (4C2). The number of ways to form sets of size 4 are 1 (4C4). Input: N = 10, A = 4, B = 9 Output: 803

Approach: The idea is to first find the number of ways including sets of size including A, B and empty sets. Then the remove the number of the sets of size A, B and empty sets. Below is the implementation of the above approach: 




// C++ program to find number of sets without size A and B
#include <bits/stdc++.h>
using namespace std;
#define mod (int)(1e9 + 7)
 
// Function to find a^m1
int power(int a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (1LL * a * a) % mod;
    // If m1 is odd, then return a * a^m1/2 * a^m1/2
    else if (m1 & 1)
        return (1LL * a * power(power(a, m1 / 2), 2)) % mod;
    else
        return power(power(a, m1 / 2), 2) % mod;
}
 
// Function to find factorial of a number
int factorial(int x)
{
    int ans = 1;
    for (int i = 1; i <= x; i++)
        ans = (1LL * ans * i) % mod;
 
    return ans;
}
 
// Function to find inverse of x
int inverse(int x)
{
    return power(x, mod - 2);
}
 
// Function to find nCr
int binomial(int n, int r)
{
    if (r > n)
        return 0;
 
    int ans = factorial(n);
 
    ans = (1LL * ans * inverse(factorial(r))) % mod;
 
    ans = (1LL * ans * inverse(factorial(n - r))) % mod;
 
    return ans;
}
 
// Function to find number of sets without size a and b
int number_of_sets(int n, int a, int b)
{
    // First calculate all sets
    int ans = power(2, n);
 
    // Remove sets of size a
    ans = ans - binomial(n, a);
 
    if (ans < 0)
        ans += mod;
 
    // Remove sets of size b
    ans = ans - binomial(n, b);
 
    // Remove empty set
    ans--;
 
    if (ans < 0)
        ans += mod;
 
    // Return the required answer
    return ans;
}
 
// Driver code
int main()
{
    int N = 4, A = 1, B = 3;
 
    // Function call
    cout << number_of_sets(N, A, B);
 
    return 0;
}




// Java program to find number of sets without size A and B
import java.util.*;
 
class GFG{
static final int mod =(int)(1e9 + 7);
  
// Function to find a^m1
static int power(int a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (int) ((1L * a * a) % mod);
    // If m1 is odd, then return a * a^m1/2 * a^m1/2
    else if (m1 % 2 == 1)
        return (int) ((1L * a * power(power(a, m1 / 2), 2)) % mod);
    else
        return power(power(a, m1 / 2), 2) % mod;
}
  
// Function to find factorial of a number
static int factorial(int x)
{
    int ans = 1;
    for (int i = 1; i <= x; i++)
        ans = (int) ((1L * ans * i) % mod);
  
    return ans;
}
  
// Function to find inverse of x
static int inverse(int x)
{
    return power(x, mod - 2);
}
  
// Function to find nCr
static int binomial(int n, int r)
{
    if (r > n)
        return 0;
  
    int ans = factorial(n);
  
    ans = (int) ((1L * ans * inverse(factorial(r))) % mod);
  
    ans = (int) ((1L * ans * inverse(factorial(n - r))) % mod);
  
    return ans;
}
  
// Function to find number of sets without size a and b
static int number_of_sets(int n, int a, int b)
{
    // First calculate all sets
    int ans = power(2, n);
  
    // Remove sets of size a
    ans = ans - binomial(n, a);
  
    if (ans < 0)
        ans += mod;
  
    // Remove sets of size b
    ans = ans - binomial(n, b);
  
    // Remove empty set
    ans--;
  
    if (ans < 0)
        ans += mod;
  
    // Return the required answer
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 4, A = 1, B = 3;
  
    // Function call
    System.out.print(number_of_sets(N, A, B));
  
}
}
 
// This code contributed by sapnasingh4991




# Python3 program to find number of
# sets without size A and B
mod = 10**9 + 7
 
# Function to find a^m1
def power(a, m1):
    if (m1 == 0):
        return 1
    elif (m1 == 1):
        return a
    elif (m1 == 2):
        return (a * a) % mod
          
    # If m1 is odd, then return a * a^m1/2 * a^m1/2
    elif (m1 & 1):
        return (a * power(power(a, m1 // 2), 2)) % mod
    else:
        return power(power(a, m1 // 2), 2) % mod
 
# Function to find factorial of a number
def factorial(x):
    ans = 1
    for i in range(1, x + 1):
        ans = (ans * i) % mod
 
    return ans
 
# Function to find inverse of x
def inverse(x):
    return power(x, mod - 2)
 
# Function to find nCr
def binomial(n, r):
    if (r > n):
        return 0
 
    ans = factorial(n)
 
    ans = (ans * inverse(factorial(r))) % mod
 
    ans = (ans * inverse(factorial(n - r))) % mod
 
    return ans
 
# Function to find number of sets without size a and b
def number_of_sets(n, a, b):
     
    # First calculate all sets
    ans = power(2, n)
 
    # Remove sets of size a
    ans = ans - binomial(n, a)
 
    if (ans < 0):
        ans += mod
 
    # Remove sets of size b
    ans = ans - binomial(n, b)
 
    # Remove empty set
    ans -= 1
 
    if (ans < 0):
        ans += mod
 
    # Return the required answer
    return ans
 
# Driver code
if __name__ == '__main__':
    N = 4
    A = 1
    B = 3
 
    # Function call
    print(number_of_sets(N, A, B))
 
# This code is contributed by mohit kumar 29   




// C# program to find number of sets without size A and B
using System;
 
class GFG{
static readonly int mod =(int)(1e9 + 7);
   
// Function to find a^m1
static int power(int a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (int) ((1L * a * a) % mod);
    // If m1 is odd, then return a * a^m1/2 * a^m1/2
    else if (m1 % 2 == 1)
        return (int) ((1L * a * power(power(a, m1 / 2), 2)) % mod);
    else
        return power(power(a, m1 / 2), 2) % mod;
}
   
// Function to find factorial of a number
static int factorial(int x)
{
    int ans = 1;
    for (int i = 1; i <= x; i++)
        ans = (int) ((1L * ans * i) % mod);
   
    return ans;
}
   
// Function to find inverse of x
static int inverse(int x)
{
    return power(x, mod - 2);
}
   
// Function to find nCr
static int binomial(int n, int r)
{
    if (r > n)
        return 0;
   
    int ans = factorial(n);
   
    ans = (int) ((1L * ans * inverse(factorial(r))) % mod);
   
    ans = (int) ((1L * ans * inverse(factorial(n - r))) % mod);
   
    return ans;
}
   
// Function to find number of sets without size a and b
static int number_of_sets(int n, int a, int b)
{
    // First calculate all sets
    int ans = power(2, n);
   
    // Remove sets of size a
    ans = ans - binomial(n, a);
   
    if (ans < 0)
        ans += mod;
   
    // Remove sets of size b
    ans = ans - binomial(n, b);
   
    // Remove empty set
    ans--;
   
    if (ans < 0)
        ans += mod;
   
    // Return the required answer
    return ans;
}
   
// Driver code
public static void Main(String[] args)
{
    int N = 4, A = 1, B = 3;
   
    // Function call
    Console.Write(number_of_sets(N, A, B));
}
}
  
// This code is contributed by PrinciRaj1992




const mod = 1e9 + 7;
 
// Function to find a^m1
function power(a, m1)
{
  if (m1 === 0) return 1;
  else if (m1 === 1) return a;
  else if (m1 === 2) return (a * a) % mod;
   
  // If m1 is odd, then return a * a^m1/2 * a^m1/2
  else if (m1 % 2 === 1) return (a * power(power(a, Math.floor(m1 / 2)), 2)) % mod;
  else return power(power(a, Math.floor(m1 / 2)), 2) % mod;
}
 
// Function to find factorial of a number
function factorial(x) {
  let ans = 1;
  for (let i = 1; i <= x; i++)
    ans = (ans * i) % mod;
 
  return ans;
}
 
// Function to find inverse of x
function inverse(x) {
  return power(x, mod - 2);
}
 
// Function to find nCr
function binomial(n, r) {
  if (r > n) return 0;
 
  let ans = factorial(n);
 
  ans = (ans * inverse(factorial(r))) % mod;
 
  ans = (ans * inverse(factorial(n - r))) % mod;
 
  return ans;
}
 
// Function to find number of sets without size a and b
function number_of_sets(n, a, b) {
  // First calculate all sets
  let ans = power(2, n);
 
  // Remove sets of size a
  ans = ans - binomial(n, a);
 
  if (ans < 0) ans = (ans + mod) % mod;
 
  // Remove sets of size b
  ans = ans - binomial(n, b);
 
  // Remove empty set
  ans--;
 
  if (ans < 0) ans = (ans + mod) % mod;
 
  // Return the required answer
  return ans;
}
 
// Example usage
const N = 4, A = 1, B = 3;
console.log(number_of_sets(N, A, B));
 
// This code is contributed by anskalyan3.

Output:
7

Time complexity: O(nlogn) because using a for loop and power function

Auxiliary Space: O(logn)


Article Tags :