📜  在X个袋子中分配M个项目,以使第一个袋子包含N个项目的可能性

📅  最后修改于: 2021-06-25 16:33:47             🧑  作者: Mango

给定三个整数NMX 。任务是找到在X个袋子中分配M个物品的概率,以使第一个袋子包含N个物品
例子:

方法 :
通常,将N个物品放入K袋的方式为N-1\choose K-1

  • 在X袋中保存M件物品的方式是M-1\choose X-1
  • 在(X-1)袋中保存(MN)物品的方式是M-N-1\choose X-2  。由于第一个袋子包含N个物品。
  • 概率是M-N-1\choose X-2  / M-1\choose X-1

下面是上述方法的实现:

C++
// CPP program to find probability of
// first bag to contain N items such
// that M items are distributed among X bags
#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(r) * factorial(n - r));
}
 
// Function to find probability of
// first bag to contain N items such
// that M items are distributed among X bags
float Probability(int M, int N, int X)
{
    return (float)(nCr(M - N - 1, X - 2) /
                    (nCr(M - 1, X - 1) * 1.0));
}
 
// Driver code
int main()
{
    int M = 9, X = 3, N = 4;
 
    // Function call
    cout << Probability(M, N, X);
 
    return 0;
}


Java
// Java program to find probability of
// first bag to contain N items such
// that M items are distributed among X bags
 
class GFG
{
 
    // Function to find factorial of a number
    public static int factorial(int n)
    {
        if (n <= 1)
            return 1;
 
        return n * factorial(n - 1);
    }
 
    // Function to find nCr
    public static int nCr(int n, int r)
    {
        return factorial(n) / (factorial(r) * factorial(n - r));
    }
 
    // Function to find probability of
    // first bag to contain N items such
    // that M items are distributed among X bags
    public static float Probability(int M, int N, int X)
    {
        return (float) (nCr(M - N - 1, X - 2) / (nCr(M - 1, X - 1) * 1.0));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int M = 9, X = 3, N = 4;
 
        // Function call
        System.out.println(Probability(M, N, X));
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 program to find probability of
# first bag to contain N items such
# that M items are distributed among X bags
 
# 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(r) *
                            factorial(n - r)));
 
# Function to find probability of
# first bag to contain N items such
# that M items are distributed among X bags
def Probability(M, N, X) :
 
    return float(nCr(M - N - 1, X - 2) /
                (nCr(M - 1, X - 1) * 1.0));
 
# Driver code
if __name__ == "__main__" :
 
    M = 9; X = 3; N = 4;
 
    # Function call
    print(Probability(M, N, X));
 
# This code is contributed by AnkitRai01


C#
// C# program to find probability of
// first bag to contain N items such
// that M items are distributed among X bags
using System;
 
class GFG
{
  
    // Function to find factorial of a number
    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(r) * factorial(n - r));
    }
  
    // Function to find probability of
    // first bag to contain N items such
    // that M items are distributed among X bags
    static float Probability(int M, int N, int X)
    {
        return (float) (nCr(M - N - 1, X - 2) / (nCr(M - 1, X - 1) * 1.0));
    }
  
    // Driver code
    static void Main()
    {
        int M = 9, X = 3, N = 4;
  
        // Function call
        Console.WriteLine(Probability(M, N, X));
    }
}
  
// This code is contributed by
// mohitkumar 29


Javascript


输出:
0.142857