原文:

给定一个包含 n 元素的集合。如果两个子集 xy 被选中,那么找到它们都包含相同数量元素的概率。 举例:

输入:4 t3】输出: 35/128 输入: 2 输出: 3/8

方法: 我们选择一个子集 xr 个元素,那么 y 必须包含 r 个元素。一个子集可以有最小 0 元素和最大 n 元素。 一个集合包含的子集总数 n 元素个数为

*** quicklatex cannot compile formula:
*** error message:
error: nothing to show, formula is empty

,同时选择 xy 的总可能方式将是

*** quicklatex cannot compile formula:
*** error message:
error: nothing to show, formula is empty

=

*** quicklatex cannot compile formula:
*** error message:
error: nothing to show, formula is empty

=

*** quicklatex cannot compile formula:
*** error message:
error: nothing to show, formula is empty

。 let, p =选择 xy 的总可能方式,使两者具有相同的元素数量。 那么 p = = = 那么要求的概率就是。 以下是上述方法的实施:

c

// c   implementation of
// the above approach
#include 
using namespace std;
// returns value of binomial
// coefficient c(n, k)
int binomialcoeff(int n, int k)
{
    int res = 1;
    // since c(n, k) = c(n, n-k)
    if (k > n - k)
        k = n - k;
    // calculate value of
    for (int i = 0; i < k;   i) {
        res *= (n - i);
        res /= (i   1);
    }
    return res;
}
// iterative function to
// calculate (x^y) in o(log y)
int power(int x, unsigned int y)
{
    // initialize result
    int res = 1;
    while (y > 0) {
        // if y is odd, multiply
        // x with result
        if (y & 1)
            res = res * x;
        // y must be even now
        // y = y/2
        y = y >> 1;
        // change x to x^2
        x = x * x;
    }
    return res;
}
// function to find probability
void findprobability(int n)
{
    // calculate total possible
    // ways and favourable ways.
    int up = binomialcoeff(2 * n, n);
    int down = power(2, 2 * n);
    // divide by gcd such that
    // they become relatively coprime
    int g = __gcd(up, down);
    up /= g, down /= g;
    cout << up << "/" << down << endl;
}
// driver code
int main()
{
    int n = 8;
    findprobability(n);
    return 0;
}

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

// java implementation of
// the above approach
class gfg
{
    // returns value of binomial
    // coefficient c(n, k)
    static int binomialcoeff(int n, int k)
    {
        int res = 1;
        // since c(n, k) = c(n, n-k)
        if (k > n - k)
            k = n - k;
        // calculate value of
        for (int i = 0; i < k;   i)
        {
            res *= (n - i);
            res /= (i   1);
        }
        return res;
    }
    // iterative function to
    // calculate (x^y) in o(log y)
    static int power(int x, int y)
    {
        // initialize result
        int res = 1;
        while (y > 0)
        {
            // if y is odd, multiply
            // x with result
            if ((y & 1) == 1)
                res = res * x;
            // y must be even now
            // y = y/2
            y = y >> 1;
            // change x to x^2
            x = x * x;
        }
        return res;
    }
    // recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    // function to find probability
    static void findprobability(int n)
    {
        // calculate total possible
        // ways and favourable ways.
        int up = binomialcoeff(2 * n, n);
        int down = power(2, 2 * n);
        // divide by gcd such that
        // they become relatively coprime
        int g = gcd(up, down);
        up /= g;
        down /= g;
        system.out.println(up   "/"   down);
    }
    // driver code
    public static void main (string[] args)
    {
        int n = 8;
        findprobability(n);
    }
}
// this code is contributed by ankitrai01

python 3

# python3 implementation of
# the above approach
import math
# returns value of binomial
# coefficient c(n, k)
def binomialcoeff(n, k):
    res = 1
    # since c(n, k) = c(n, n-k)
    if (k > n - k):
        k = n - k
    # calculate value of
    for i in range(0, k):
        res = res * (n - i)
        res = res // (i   1)
    return res
# iterative function to
# calculate (x^y) in o(log y)
def power(x, y):
    # initialize result
    res = 1
    while (y > 0):
        # if y is odd, multiply
        # x with result
        if (y & 1):
            res = res * x
        # y must be even now
        # y = y/2
        y = y // 2
        # change x to x^2
        x = x * x
    return res
# function to find probability
def findprobability(n):
    # calculate total possible
    # ways and favourable ways.
    up = binomialcoeff(2 * n, n)
    down = power(2, 2 * n)
    # divide by gcd such that
    # they become relatively coprime
    g = math.gcd(up,down)
    up = up // g
    down = down // g
    print(up, "/", down)
# driver code
n = 8
findprobability(n)
# this code is contributed by sanjit_prasad

c

// c# implementation of
// the above approach
using system;
using system.collections.generic;
class gfg
{
    // returns value of binomial
    // coefficient c(n, k)
    static int binomialcoeff(int n, int k)
    {
        int res = 1;
        // since c(n, k) = c(n, n-k)
        if (k > n - k)
            k = n - k;
        // calculate value of
        for (int i = 0; i < k;   i)
        {
            res *= (n - i);
            res /= (i   1);
        }
        return res;
    }
    // iterative function to
    // calculate (x^y) in o(log y)
    static int power(int x, int y)
    {
        // initialize result
        int res = 1;
        while (y > 0)
        {
            // if y is odd, multiply
            // x with result
            if ((y & 1) == 1)
                res = res * x;
            // y must be even now
            // y = y/2
            y = y >> 1;
            // change x to x^2
            x = x * x;
        }
        return res;
    }
    // recursive function to
    // return gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    // function to find probability
    static void findprobability(int n)
    {
        // calculate total possible
        // ways and favourable ways.
        int up = binomialcoeff(2 * n, n);
        int down = power(2, 2 * n);
        // divide by gcd such that
        // they become relatively coprime
        int g = gcd(up, down);
        up /= g;
        down /= g;
        console.writeline(up   "/"   down);
    }
    // driver code
    public static void main (string[] args)
    {
        int n = 8;
        findprobability(n);
    }
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

6435/32768