📌  相关文章
📜  在桌子的两边布置2 * N人,在对边布置X和Y人的方式的数目

📅  最后修改于: 2021-04-29 02:25:45             🧑  作者: Mango

给定三个整数NXY。任务是找到在桌子的两边布置2 * N人的方式,每边有N个椅子,使得X人在一侧, Y人在另一侧。
注意: X和Y均小于或等于N。
例子:

方法 :
总人数2 * N。让两边都称为A和B。A边的X人和B边的Y人,则剩下2 * N – X – Y个人。我们可以在其中选择NX作为A面2*N - X - Y\choose N-X   方式,其余人员将自动坐在B的另一侧。在N的每一侧都进行了排列!方法。沿桌子的两侧布置2 * N个人的方式为2*N - X - Y\choose N-X   .N!N!
下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to find factorial of a number
int factorial(int n)
{
    if (n <= 1)
        return 1;
    return n * factorial(n - 1);
}
 
// Function to find nCr
int nCr(int n, int r)
{
    return factorial(n) / (factorial(n - r) * factorial(r));
}
 
 
// Function to find the number of ways to arrange 2*N persons
int NumberOfWays(int n, int x, int y)
{
    return nCr(2*n-x-y, n-x) * factorial(n) * factorial(n);
}
 
 
// Driver code
int main()
{
    int n = 5, x = 4, y = 2;
     
    // Function call
    cout << NumberOfWays(n, x, y);
     
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
    // Function to returns factorial of n
    static int factorial(int n)
    {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
     
    // Function to find nCr
    static int nCr(int n, int r)
    {
        return factorial(n) / (factorial(n - r) *
                               factorial(r));
    }
     
    // Function to find the number of ways
    // to arrange 2*N persons
    static int NumberOfWays(int n, int x, int y)
    {
        return nCr(2 * n - x - y, n - x) *
               factorial(n) * factorial(n);
    }
     
    // Driver code
    public static void main (String[] args)
                  throws java.lang.Exception
    {
        int n = 5, x = 4, y = 2;
         
        // Function call
        System.out.println(NumberOfWays(n, x, y));        
    }
}
 
// This code is contributed by Nidhiva


Python3
# Python3 implementation for the above approach
 
# Function to find factorial of a number
def factorial(n):
 
    if (n <= 1):
        return 1;
    return n * factorial(n - 1);
 
# Function to find nCr
def nCr(n, r):
 
    return (factorial(n) /
           (factorial(n - r) * factorial(r)));
 
# Function to find the number of ways
# to arrange 2*N persons
def NumberOfWays(n, x, y):
 
    return (nCr(2 * n - x - y, n - x) *
            factorial(n) * factorial(n));
 
# Driver code
n, x, y = 5, 4, 2;
 
# Function call
print(int(NumberOfWays(n, x, y)));
 
# This code is contributed by PrinciRaj1992


C#
// C# implementation for the above approach
using System;
     
class GFG
{
     
    // Function to returns factorial of n
    static int factorial(int n)
    {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
     
    // Function to find nCr
    static int nCr(int n, int r)
    {
        return factorial(n) / (factorial(n - r) *
                               factorial(r));
    }
     
    // Function to find the number of ways
    // to arrange 2*N persons
    static int NumberOfWays(int n, int x, int y)
    {
        return nCr(2 * n - x - y, n - x) *
            factorial(n) * factorial(n);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5, x = 4, y = 2;
         
        // Function call
        Console.WriteLine(NumberOfWays(n, x, y));        
    }
}
 
// This code is contributed by Princi Singh


PHP


Javascript


输出:
57600