Dados tres números N, A, B . La tarea es contar el número de formas de seleccionar cosas de modo que no exista un conjunto de tamaño A o B . La respuesta puede ser muy grande. Entonces, salida módulo de respuesta 10 9 +7 .
Nota: El conjunto vacío no se considera como uno de los caminos.
Ejemplos:
Entrada: N = 4, A = 1, B = 3
Salida: 7
Explicación:
El número de formas de formar conjuntos de tamaño 2 es 6 ( 4 C 2 ).
El número de formas de formar conjuntos de tamaño 4 es 1 ( 4 C 4 ).Entrada: N = 10, A = 4, B = 9
Salida: 803
Enfoque: La idea es primero encontrar el número de formas que incluyen conjuntos de tamaño que incluyen A, B y conjuntos vacíos. Luego elimine el número de los juegos de tamaño A, B y juegos vacíos.
A continuación se muestra la implementación del enfoque anterior:
CPP
// 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
// 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
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA