给定三个数字N,A,B 。任务是计算选择事物的方式数量,以使A或B都不存在大小集。答案可能非常大。因此,输出答案取模10 9 +7 。
注意:空集不被视为一种方法。
例子:
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
方法:想法是首先找到一些方法,包括大小集(包括A,B和空集)。然后删除大小为A,B的套数和空套数。
下面是上述方法的实现:
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
输出:
7