# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## CPP

 `// C++ program to find number of sets without size A and B ` `#include ` `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

 `// 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

 `# 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#

 `// 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 `

Output:

```7
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.