📜  时空有效的二项式系数

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

编写一个函数,该函数接受两个参数n和k并返回二项式系数C(n,k)的值。
例子:

Input: n = 4 and k = 2
Output: 6
Explanation: 4 C 2 is 4!/(2!*2!) = 6

Input: n = 5 and k = 2
Output: 10
Explanation: 5 C 2 is 5!/(3!*2!) = 20

在本文中,我们讨论了O(n * k)时间和O(k)额外空间算法。可以在O(k)时间和O(1)额外空间中计算C(n,k)的值。
解决方案:

C(n, k) 
= n! / (n-k)! * k!
= [n * (n-1) *....* 1]  / [ ( (n-k) * (n-k-1) * .... * 1) * 
                            ( k * (k-1) * .... * 1 ) ]
After simplifying, we get
C(n, k) 
= [n * (n-1) * .... * (n-k+1)] / [k * (k-1) * .... * 1]

Also, C(n, k) = C(n, n-k)  
// r can be changed to n-r if r > n-r 
  1. 如果r大于nr,则将r更改为nr。并创建一个变量来存储答案。
  2. 运行从0到r-1的循环
  3. 在每次迭代中,将ans更新为(ans *(ni))/(i + 1),其中i是循环计数器。
  4. 因此,答案将等于((n / 1)*((n-1)/ 2)* … *((n-r + 1)/ r!),它等于nCr。

下面的实现使用上面的公式来计算C(n,k)。

C++
// Program to calculate C(n, k)
#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
    // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 8, k = 2;
    cout << "Value of C(" << n << ", "
         << k << ") is " << binomialCoeff(n, k);
    return 0;
}
 
// This is code is contributed by rathbhupendra


C
// Program to calculate C(n, k)
#include 
 
// 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
    // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
/* Driver program to test above function*/
int main()
{
    int n = 8, k = 2;
    printf(
        "Value of C(%d, %d) is %d ",
        n, k, binomialCoeff(n, k));
    return 0;
}


Java
// Program to calculate C(n, k) in java
class BinomialCoefficient {
    // 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
        // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
        for (int i = 0; i < k; ++i) {
            res *= (n - i);
            res /= (i + 1);
        }
 
        return res;
    }
 
    /* Driver program to test above function*/
    public static void main(String[] args)
    {
        int n = 8;
        int k = 2;
        System.out.println("Value of C(" + n + ", " + k + ") "
                           + "is"
                           + " " + binomialCoeff(n, k));
    }
}
// This Code is Contributed by Saket Kumar


Python
# Python program to calculate C(n, k)
 
# Returns value of Binomial Coefficient
# C(n, k)
def binomialCoefficient(n, k):
    # since C(n, k) = C(n, n - k)
    if(k > n - k):
        k = n - k
    # initialize result
    res = 1
    # Calculate value of
    # [n * (n-1) *---* (n-k + 1)] / [k * (k-1) *----* 1]
    for i in range(k):
        res = res * (n - i)
        res = res / (i + 1)
    return res
 
# Driver program to test above function
n = 8
k = 2
res = binomialCoefficient(n, k)
print("Value of C(% d, % d) is % d" %(n, k, res))
 
# This code is contributed by Aditi Sharma


C#
// C# Program to calculate C(n, k)
using System;
 
class BinomialCoefficient {
 
    // 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 [n * ( n - 1) *---* (
        // n - k + 1)] / [k * (k - 1) *----* 1]
        for (int i = 0; i < k; ++i) {
            res *= (n - i);
            res /= (i + 1);
        }
 
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 8;
        int k = 2;
        Console.Write("Value of C(" + n + ", " + k + ") "
                      + "is"
                      + " " + binomialCoeff(n, k));
    }
}
 
// This Code is Contributed by
// Smitha Dinesh Semwal.


PHP
 $n - $k )
        $k = $n - $k;
 
    // Calculate value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for ($i = 0; $i < $k; ++$i)
    {
        $res *= ($n - $i);
        $res /= ($i + 1);
    }
 
    return $res;
}
 
    // Driver Code
    $n = 8;
    $k = 2;
    echo " Value of C ($n, $k) is ",
             binomialCoeff($n, $k);
 
// This code is contributed by ajit.
?>


Javascript


输出:

Value of C(8, 2) is 28

复杂度分析:

  • 时间复杂度: O(r)。
    循环必须从0运行到r。因此,时间复杂度为O(r)。
  • 辅助空间: O(1)。
    由于不需要额外的空间。