原文:

给定一个大小为 narr【】,代表每个 n 不同颜色的球的数量,任务是找到将所有球分配到两个盒子中的

示例:

输入: arr[] = {1,1},n = 2 输出: 1.00000 说明:两种可能的分配方式如下: 将第 0 色型的球放入第 1 个盒子,将第 1 个色型的球放入第 2 个盒子。 将 1 st 型的颜色放在 1 st 框中,将 0 th 型的颜色放在第二个框中。 因此,概率等于(2 / 2) = 1.00000

输入: arr[] = {2,1,1},n = 3 t3】输出: 0.66667

方法:思路是用和来解决问题。给定的问题可以基于以下观察来解决:

  • let x be the total number of ways to divide k balls into equal halves.
  • it can be observed that x is the same as k/2 ball selected from k balls, and is equal to k c (k/2).
  • select j ball from arr [i] and put it into a box every 0 ≤ i < n and j ≤ arr [i], which can be calculated by backtracking with
  • therefore, the required probability is y/x.

按照以下步骤解决问题:

  • arr【】在一个变量中,比如 k.
  • 如果 k 是奇数,打印 0
  • 计算选择 k/2 球的方式数并存储在变量中,比如 x
  • 定义一个,说有效排列(pos,usedballs,box1,box2) ,并执行以下步骤:
    • 定义基本情况:如果使用的球等于 k/2 ,那么如果盒 1 =盒 2 ,则返回 1 。否则,返回 0
    • 如果位置≥ n ,则返回 0
    • 现在,初始化一个变量,比如 res ,来存储剩余球的分配方式。
    • 迭代范围 [0,arr[pos]]:
      • 框 1框 2 分配给变量,分别表示新框 1新框 2
      • 如果 j > 0新箱 2 ,如果j,则将新箱 1 增加 1。
      • 现在,更新 resasres = res arr【pos】cj*验证交换(pos 1,usedballs j,newbox1,newbox2)。
    • 返回 res 的值。
  • 调用函数有效排列(0,0,0,0) 并将其存储在变量中,比如 y.
  • 最后,将得到的结果打印为 y/x

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// stores the count of
// distinct colors in box1
static int box1 = 0;
// stores the count of
// distinct colors in box2
static int box2 = 0;
static int fact[11];
// function to calculate ncr
long comb(int n, int r)
{
    long res = fact[n] / fact[r];
    res /= fact[n - r];
    return res;
}
// function to calculate factorial of n
void factorial(int n)
{
    // base case
    fact[0] = 1;
    // iterate over the range [1, n]
    for(int i = 1; i <= n; i  )
        fact[i] = fact[i - 1] * i;
}
// function to calculate total number
// of possible distributions which
// satisfies the given conditions
long validpermutations(int n, int balls[],
                       int usedballs,
                       int i, int m)
{
    // if used balls is equal to k/2
    if (usedballs == n)
    {
        // if box1 is equal to box2
        return box1 == box2 ? 1 : 0;
    }
    // base condition
    if (i >= m)
        return 0;
    // stores the number of ways of
    // distributing remaining balls without
    // including the current balls in box1
    long res = validpermutations(n, balls,
                                 usedballs,
                                 i   1, m);
    // increment box1 by one
    box1  ;
    // iterate over the range [1, balls[i]]
    for(int j = 1; j <= balls[i]; j  )
    {
        // if all the balls goes to box1,
        // then decrease box2 by one
        if (j == balls[i])
            box2--;
        // total number of ways of
        // selecting j balls
        long combinations = comb(balls[i], j);
        // increment res by total number of valid
        // ways of distributing the remaining balls
        res  = combinations * validpermutations(n, balls,
                                                usedballs   j,
                                                i   1, m);
    }
    // decrement box1 by one
    box1--;
    // increment box2 by 1
    box2  ;
    return res;
}
// function to calculate the required probability
double getprobability(int balls[], int m)
{
    // calculate factorial from [1, 10]
    factorial(10);
    // assign all distinct balls to second box
    box2 = m;
    // total number of balls
    int k = 0;
    // calculate total number of balls
    for(int i = 0; i < m; i  )
        k  = balls[i];
    // if k is an odd number
    if (k % 2 == 1)
        return 0;
    // total ways of distributing the balls
    // in two equal halves
    long all = comb(k, k / 2);
    // required number of ways
    long validpermutation = validpermutations(k / 2, balls,
                                              0, 0, m);
    // return the required probability
    return (double)validpermutation / all;
}
// driver code
int main()
{
    int arr[] = { 2, 1, 1 };
    int n = 4;
    int m = sizeof(arr) / sizeof(arr[0]);
    // print the result
    cout << (getprobability(arr, m));
    return 0;
}
// this code is contributed by ukasp

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.io.*;
import java.util.*;
class gfg {
    // stores the count of
    // distinct colors in box1
    static int box1 = 0;
    // stores the count of
    // distinct colors in box2
    static int box2 = 0;
    static int[] fact = new int[11];
    // function to calculate the required probability
    public static double getprobability(int[] balls)
    {
        // calculate factorial from [1, 10]
        factorial(10);
        // assign all distinct balls to second box
        box2 = balls.length;
        // total number of balls
        int k = 0;
        // calculate total number of balls
        for (int i = 0; i < balls.length; i  )
            k  = balls[i];
        // if k is an odd number
        if (k % 2 == 1)
            return 0;
        // total ways of distributing the balls
        // in two equal halves
        long all = comb(k, k / 2);
        // required number of ways
        long validpermutations = validpermutations(k / 2, balls, 0, 0);
        // return the required probability
        return (double)validpermutations / all;
    }
    // function to calculate total number
    // of possible distributions which
    // satisfies the given conditions
    static long validpermutations(int n, int[] balls,
                          int usedballs, int i)
    {
        // if used balls is equal to k/2
        if (usedballs == n) {
            // if box1 is equal to box2
            return box1 == box2 ? 1 : 0;
        }
        // base condition
        if (i >= balls.length)
            return 0;
        // stores the number of ways of
        // distributing remaining balls without
        // including the current balls in box1
        long res = validpermutations(n, balls, usedballs, i   1);
        // increment box1 by one
        box1  ;
        // iterate over the range [1, balls[i]]
        for (int j = 1; j <= balls[i]; j  ) {
            // if all the balls goes to box1,
            // then decrease box2 by one
            if (j == balls[i])
                box2--;
            // total number of ways of
            // selecting j balls
            long combinations = comb(balls[i], j);
            // increment res by total number of valid
            // ways of distributing the remaining balls
            res  = combinations * validpermutations(n, balls,
                                            usedballs   j, i   1);
        }
        // decrement box1 by one
        box1--;
        // increment box2 by 1
        box2  ;
        return res;
    }
    // function to calculate factorial of n
    static void factorial(int n)
    {
        // base case
        fact[0] = 1;
        // iterate over the range [1, n]
        for (int i = 1; i <= n; i  )
            fact[i] = fact[i - 1] * i;
    }
    // function to calculate ncr
    static long comb(int n, int r)
    {
        long res = fact[n] / fact[r];
        res /= fact[n - r];
        return res;
    }
    // driver code
    public static void main(string[] args)
    {
        int[] arr = { 2, 1, 1 };
        int n = 4;
        // print the result
        system.out.println(getprobability(arr));
    }
}

python 3

# python3 program for the above approach
# stores the count of
# distinct colors in box1
box1 = 0
# stores the count of
# distinct colors in box2
box2 = 0
fact = [0 for i in range(11)]
# function to calculate the required probability
def getprobability(balls):
    global box1, box2, fact
    # calculate factorial from [1, 10]
    factorial(10)
    # assign all distinct balls to second box
    box2 = len(balls)
    # total number of balls
    k = 0
    # calculate total number of balls
    for i in range(len(balls)):
        k  = balls[i]
    # if k is an odd number   
    if (k % 2 == 1):
        return 0
    # total ways of distributing the balls
    # in two equal halves
    all = comb(k, k // 2)
    # required number of ways
    validpermutation = validpermutations(
        k // 2, balls, 0, 0)
    # return the required probability
    return validpermutation / all
# function to calculate total number
# of possible distributions which
# satisfies the given conditions
def validpermutations(n, balls, usedballs, i):
    global box1, box2, fact
    # if used balls is equal to k/2
    if (usedballs == n):
        # if box1 is equal to box2
        if (box1 == box2):
            return 1
        else:
            return 0
    # base condition
    if (i >= len(balls)):
        return 0
    # stores the number of ways of
    # distributing remaining balls without
    # including the current balls in box1
    res = validpermutations(n, balls,
                            usedballs, i   1)
    # increment box1 by one
    box1  = 1
    # iterate over the range [1, balls[i]]
    for j in range(1, balls[i]   1):
        # if all the balls goes to box1,
        # then decrease box2 by one
        if (j == balls[i]):
            box2 -= 1
        # total number of ways of
        # selecting j balls
        combinations = comb(balls[i], j)
        # increment res by total number of valid
        # ways of distributing the remaining balls
        res  = combinations * validpermutations(n, balls,
                                                usedballs   j,
                                                i   1)
    # decrement box1 by one
    box1 -= 1
    # increment box2 by 1
    box2  = 1
    return res
# function to calculate factorial of n
def factorial(n):
    global box1, box2, fact
    # base case
    fact[0] = 1
    # iterate over the range [1, n]
    for i in range(1, n   1):
        fact[i] = fact[i - 1] * i
# function to calculate ncr   
def comb(n, r):
    global box1, box2, fact
    res = fact[n] // fact[r]
    res //= fact[n - r]
    return res
# driver code   
arr = [ 2, 1, 1 ]
n = 4
print(getprobability(arr))
# this code is contributed by avanitrachhadiya2155

c

// c# program for the above approach
using system;
public class gfg
{
    // stores the count of
    // distinct colors in box1
    static int box1 = 0;
    // stores the count of
    // distinct colors in box2
    static int box2 = 0;
    static int[] fact = new int[11];
    // function to calculate the required probability
    public static double getprobability(int[] balls)
    {
        // calculate factorial from [1, 10]
        factorial(10);
        // assign all distinct balls to second box
        box2 = balls.length;
        // total number of balls
        int k = 0;
        // calculate total number of balls
        for (int i = 0; i < balls.length; i  )
            k  = balls[i];
        // if k is an odd number
        if (k % 2 == 1)
            return 0;
        // total ways of distributing the balls
        // in two equal halves
        long all = comb(k, k / 2);
        // required number of ways
        long validpermutationss = validpermutations((k / 2), balls, 0, 0);
        // return the required probability
        return (double)validpermutationss / all;
    }
    // function to calculate total number
    // of possible distributions which
    // satisfies the given conditions
    static long validpermutations(int n, int[] balls,
                          int usedballs, int i)
    {
        // if used balls is equal to k/2
        if (usedballs == n)
        {
            // if box1 is equal to box2
            return box1 == box2 ? 1 : 0;
        }
        // base condition
        if (i >= balls.length)
            return 0;
        // stores the number of ways of
        // distributing remaining balls without
        // including the current balls in box1
        long res = validpermutations(n, balls, usedballs, i   1);
        // increment box1 by one
        box1  ;
        // iterate over the range [1, balls[i]]
        for (int j = 1; j <= balls[i]; j  )
        {
            // if all the balls goes to box1,
            // then decrease box2 by one
            if (j == balls[i])
                box2--;
            // total number of ways of
            // selecting j balls
            long combinations = comb(balls[i], j);
            // increment res by total number of valid
            // ways of distributing the remaining balls
            res  = combinations * validpermutations(n, balls,
                                            usedballs   j, i   1);
        }
        // decrement box1 by one
        box1--;
        // increment box2 by 1
        box2  ;
        return res;
    }
    // function to calculate factorial of n
    static void factorial(int n)
    {
        // base case
        fact[0] = 1;
        // iterate over the range [1, n]
        for (int i = 1; i <= n; i  )
            fact[i] = fact[i - 1] * i;
    }
    // function to calculate ncr
    static long comb(int n, int r)
    {
        long res = fact[n] / fact[r];
        res /= fact[n - r];
        return res;
    }
    // driver code
    public static void main(string[] args)
    {
        int[] arr = { 2, 1, 1 };
        int n = 4;
        // print the result
        console.writeline(getprobability(arr));
    }
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

0.6666666666666666

时间复杂度: o(n!) 辅助空间: o(1)