📜  两个数的二项式系数的所有乘积之和,最高为K

📅  最后修改于: 2021-04-27 19:01:44             🧑  作者: Mango

给定三个整数NMK ,任务是计算二项式系数C(N,i)和C(M,K – i)的乘积之和,其中i介于[0,K]之间

例子:

天真的方法:解决此问题的最简单方法是简单地在[0,K]范围内进行迭代并为每个i计算C(N,i)C(M,K – 1) ,并通过相加它们的乘积来更新总和
下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function returns nCr
// i.e. Binomial Coefficient
int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i) {
 
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to calculate and
// return the sum of the products
int solve(int n, int m, int k)
{
 
    // Initialize sum to 0
    int sum = 0;
 
    // Traverse from 0 to k
    for (int i = 0; i <= k; i++)
        sum += nCr(n, i)
               * nCr(m, k - i);
 
    return sum;
}
 
// Driver Code
int main()
{
    int n = 3, m = 2, k = 2;
 
    cout << solve(n, m, k);
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to calculate and
// return the sum of the products
static int solve(int n, int m, int k)
{
 
    // Initialize sum to 0
    int sum = 0;
 
    // Traverse from 0 to k
    for (int i = 0; i <= k; i++)
        sum += nCr(n, i)
               * nCr(m, k - i);
 
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    System.out.print(solve(n, m, k));
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 implementation of
# the above approach
 
# Function returns nCr
# i.e. Binomial Coefficient
def nCr(n, r):
     
    # Initialize res with 1
    res = 1
     
    # Since C(n, r) = C(n, n-r)
    if r > n - r:
        r = n - r
     
    # Evaluating expression
    for i in range(r):
        res *= (n - i)
        res /= (i + 1)
     
    return res;
     
# Function to calculate and
# return the sum of the products
def solve(n, m, k):
     
    # Initialize sum to 0
    sum = 0;
     
    # Traverse from 0 to k
    for i in range(k + 1):
        sum += nCr(n, i) * nCr(m, k - i)
     
    return int(sum)
     
# Driver code
if __name__ == '__main__':
     
    n = 3
    m = 2
    k = 2;
     
    print(solve(n, m, k))
 
# This code is contributed by jana_sayantan


C#
// C# implementation of
// the above approach
using System;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to calculate and
// return the sum of the products
static int solve(int n, int m, int k)
{
 
    // Initialize sum to 0
    int sum = 0;
 
    // Traverse from 0 to k
    for (int i = 0; i <= k; i++)
        sum += nCr(n, i)
            * nCr(m, k - i);
 
    return sum;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    Console.Write(solve(n, m, k));
}
}
 
// This code is contributed by Rajput-Ji


C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function returns nCr
// i.e. Binomial Coefficient
int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i) {
 
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 3, m = 2, k = 2;
 
    cout << nCr(n + m, k);
    return 0;
}


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    System.out.print(nCr(n + m, k));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation of
# the above approach
 
# Function returns nCr
# i.e. Binomial Coefficient
def nCr(n, r):
 
    # Initialize res with 1
    res = 1
 
    # Since C(n, r) = C(n, n-r)
    if(r > n - r):
        r = n - r
 
    # Evaluating expression
    for i in range(r):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
# Driver Code
if __name__ == '__main__':
 
    n = 3
    m = 2
    k = 2
 
    # Function call
    print(nCr(n + m, k))
 
# This code is contributed by Shivam Singh


C#
// C# implementation of
// the above approach
using System;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
public static void Main()
{
    int n = 3, m = 2, k = 2;
    Console.Write(nCr(n + m, k));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
10

时间复杂度: O(K 2 )
辅助空间: O(1)
高效方法:
可以使用Vandermonde的Identity对上述方法进行优化。

因此,给定的表达式简化为以下形式:

下面是上述方法的实现:

C++

// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
// Function returns nCr
// i.e. Binomial Coefficient
int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i) {
 
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 3, m = 2, k = 2;
 
    cout << nCr(n + m, k);
    return 0;
}

Java

// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    System.out.print(nCr(n + m, k));
}
}
 
// This code is contributed by sapnasingh4991

Python3

# Python3 implementation of
# the above approach
 
# Function returns nCr
# i.e. Binomial Coefficient
def nCr(n, r):
 
    # Initialize res with 1
    res = 1
 
    # Since C(n, r) = C(n, n-r)
    if(r > n - r):
        r = n - r
 
    # Evaluating expression
    for i in range(r):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
# Driver Code
if __name__ == '__main__':
 
    n = 3
    m = 2
    k = 2
 
    # Function call
    print(nCr(n + m, k))
 
# This code is contributed by Shivam Singh

C#

// C# implementation of
// the above approach
using System;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
public static void Main()
{
    int n = 3, m = 2, k = 2;
    Console.Write(nCr(n + m, k));
}
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
10

时间复杂度: O(K)
辅助空间: O(1)