Dadas 2n niñas y divididas al azar en dos subgrupos, cada uno con n niñas. La tarea es contar la cantidad de formas en que se pueden formar grupos de manera que dos hermosas chicas estén en grupos diferentes.
Ejemplo:
Entrada: 4
Salida: 4
Sea el grupo r1, r2, b1, b2 donde b1 y b2 son chicas hermosas Los
grupos son: ((r1, b1) (r2, b2)), ((r1, b2) (r2, b1) ), ((r2, b2) (r1, b1)), ((r2, b1) (r1, b2))
Entrada: 8
Salida: 40
Enfoque: Hay dos formas en las que las dos hermosas chicas yacen en diferentes grupos y correspondientes a cada forma, las chicas restantes (2n – 2) se pueden dividir en dos grupos. Por lo tanto, el número total
de formas es 2 *
Código de implementación:
C++
// CPP Program to count // Number of ways in which two // Beautiful girls are in different group #include <bits/stdc++.h> using namespace std; // This function will // return the factorial of a given number int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result = result * i; return result; } // This function will calculate nCr of given // n and r int nCr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // This function will // Calculate number of ways int calculate_result(int n) { int result = 2 * nCr((n - 2), (n / 2 - 1)); return result; } // Driver Code int main(void) { int a = 2, b = 4; cout << calculate_result(2 * a) << endl; cout << calculate_result(2 * b) << endl; return 0; }
Java
//Java Program to count // Number of ways in which two // Beautiful girls are in different group import java.io.*; class GFG { // This function will // return the factorial of a given number static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result = result * i; return result; } // This function will calculate nCr of given // n and r static int nCr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // This function will // Calculate number of ways static int calculate_result(int n) { int result = 2 * nCr((n - 2), (n / 2 - 1)); return result; } // Driver Code public static void main (String[] args) { int a = 2, b = 4; System.out.println( calculate_result(2 * a)); System.out.print(calculate_result(2 * b)); } } // This code is contributed by inder_verma..
Python3
# Python3 Program to count # Number of ways in which two # Beautiful girls are in different group # This function will # return the factorial of a # given number def factorial(n) : result = 1 for i in range(1, n + 1) : result *= i return result # This function will calculate nCr of given # n and r def nCr(n, r) : return (factorial(n) // (factorial(r) * factorial(n - r))) # This function will # Calculate number of ways def calculate_result(n) : result = 2 * nCr((n -2), (n // 2 - 1)) return result # Driver code if __name__ == "__main__" : a, b = 2, 4 print(calculate_result(2 * a)) print(calculate_result(2 * b)) # This code is contributed by # ANKITRAI1
C#
//C# Program to count // Number of ways in which two // Beautiful girls are in different groupusing System; using System; public class GFG { // This function will // return the factorial of a given number static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result = result * i; return result; } // This function will calculate nCr of given // n and r static int nCr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // This function will // Calculate number of ways static int calculate_result(int n) { int result = 2 * nCr((n - 2), (n / 2 - 1)); return result; } // Driver Code public static void Main () { int a = 2, b = 4; Console.WriteLine( calculate_result(2 * a)); Console.Write(calculate_result(2 * b)); } } // This code is contributed by Subhadeep
PHP
<?php // PHP Program to count Number // of ways in which two Beautiful // girls are in different group // This function will return // the factorial of a given number function factorial($n) { $result = 1; for ($i = 1; $i <= $n; $i++) $result = $result * $i; return $result; } // This function will calculate // nCr of given n and r function nCr($n, $r) { return factorial($n) / (factorial($r) * factorial($n - $r)); } // This function will // Calculate number of ways function calculate_result($n) { $result = 2 * nCr(($n - 2), ($n / 2 - 1)); return $result; } // Driver Code $a = 2; $b = 4; echo calculate_result(2 * $a) . "\n"; echo calculate_result(2 * $b) . "\n"; // This Code is contributed by mits ?>
Javascript
// Javascript Program to count Number // of ways in which two Beautiful // girls are in different group // This function will return // the factorial of a given number function factorial(n) { let result = 1; for (let i = 1; i <= n; i++) result = result * i; return result; } // This function will calculate // nCr of given n and r function nCr(n, r) { return factorial(n) / (factorial(r) * factorial(n - r)); } // This function will // Calculate number of ways function calculate_result(n) { let result = 2 * nCr((n - 2), (n / 2 - 1)); return result; } // Driver Code let a = 2; let b = 4; document.write(calculate_result(2 * a) + "<br>"); document.write(calculate_result(2 * b) + "<br>"); // This Code is contributed by gfgking
4 40
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ankit15697 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA