📜  连续二项式系数的乘积之和

📅  最后修改于: 2021-09-17 16:01:09             🧑  作者: Mango

给定一个正整数n 。任务是找到连续二项式系数的乘积之和,即
n C 0 * n C 1 + n C 1 * n C 2 + ….. + n C n-1 * n C n
例子:

Input : n = 3
Output : 15
3C0*3C1 + 3C1*3C2 +3C2*3C3
= 1*3 + 3*3 + 3*1
= 3 + 9 + 3
= 15

Input : n = 4
Output : 56

方法一:思想是找到所有直到第n项的二项式系数,并找到连续系数的乘积之和。
下面是这个方法的实现:

C++
// CPP Program to find sum of product of
// consecutive Binomial Coefficient.
#include 
using namespace std;
#define MAX 100
 
// Find the binomial coefficient upto nth term
void binomialCoeff(int C[], int n)
{
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle using
        // the previous row
        for (int j = min(i, n); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
int sumOfproduct(int n)
{
    int sum = 0;
    int C[MAX] = { 0 };
 
    binomialCoeff(C, n);
 
    // finding the sum of product of
    // consecutive coefficient.
    for (int i = 0; i <= n; i++)
        sum += C[i] * C[i + 1];   
 
    return sum;
}
 
// Driven Program
int main()
{
    int n = 3;
    cout << sumOfproduct(n) << endl;
    return 0;
}


Java
// Java Program to find sum of product of
// consecutive Binomial Coefficient.
 
import java.io.*;
 
class GFG {
    
static int  MAX = 100;
 
// Find the binomial coefficient upto nth term
static void binomialCoeff(int C[], int n)
{
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle using
        // the previous row
        for (int j = Math.min(i, n); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
static int sumOfproduct(int n)
{
    int sum = 0;
    int C[] = new int[MAX];
 
    binomialCoeff(C, n);
 
    // finding the sum of product of
    // consecutive coefficient.
    for (int i = 0; i <= n; i++)
        sum += C[i] * C[i + 1];
 
    return sum;
}
 
// Driven Program
 
    public static void main (String[] args) {
    int n = 3;
    System.out.println( sumOfproduct(n));
    }
}
  
// This code is contributed by inder_verma..


Python3
# Python3 Program to find sum of product
# of consecutive Binomial Coefficient.
MAX = 100;
 
# Find the binomial coefficient upto
# nth term
def binomialCoeff(C, n):
 
    C[0] = 1; # nC0 is 1
 
    for i in range(1, n + 1):
 
        # Compute next row of
        # pascal triangle using
        # the previous row
        for j in range(min(i, n), 0, -1):
            C[j] = C[j] + C[j - 1];
     
    return C;
 
# Return the sum of the product of
# consecutive binomial coefficient.
def sumOfproduct(n):
 
    sum = 0;
    C = [0] * MAX;
 
    C = binomialCoeff(C, n);
 
    # finding the sum of
    # product of consecutive
    # coefficient.
    for i in range(n + 1):
        sum += C[i] * C[i + 1];
 
    return sum;
 
# Driver Code
n = 3;
print(sumOfproduct(n));
 
# This code is contributed by mits


C#
// C# Program to find sum of
// product of consecutive
// Binomial Coefficient.
using System;
 
class GFG
{
static int MAX = 100;
 
// Find the binomial coefficient
// upto nth term
static void binomialCoeff(int []C, int n)
{
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++)
    {
 
        // Compute next row of pascal
        // triangle using the previous row
        for (int j = Math.Min(i, n);
                 j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
static int sumOfproduct(int n)
{
    int sum = 0;
    int []C = new int[MAX];
 
    binomialCoeff(C, n);
 
    // finding the sum of product of
    // consecutive coefficient.
    for (int i = 0; i <= n; i++)
        sum += C[i] * C[i + 1];
 
    return sum;
}
 
// Driven Code
public static void Main ()
{
    int n = 3;
    Console.WriteLine(sumOfproduct(n));
}
}
 
// This code is contributed by anuj_67


PHP
 0; $j--)
            $C[$j] = $C[$j] +
                     $C[$j - 1];
    }
    return $C;
}
 
// Return the sum of the
// product of consecutive
// binomial coefficient.
function sumOfproduct($n)
{
    global $MAX;
    $sum = 0;
    $C = array_fill(0, $MAX, 0);
 
    $C = binomialCoeff($C, $n);
 
    // finding the sum of
    // product of consecutive
    // coefficient.
    for ($i = 0; $i <= $n; $i++)
        $sum += $C[$i] * $C[$i + 1];
 
    return $sum;
}
 
// Driver Code
$n = 3;
echo sumOfproduct($n);
 
// This code is contributed by mits
?>


Javascript


C++
// CPP Program to find sum of product of
// consecutive Binomial Coefficient.
#include 
using namespace std;
#define MAX 100
 
// Find the binomial coefficient up to nth
// term
int binomialCoeff(int n, int k)
{
    int C[k + 1];
    memset(C, 0, sizeof(C));
 
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle
        // using the previous row
        for (int j = min(i, k); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
    return C[k];
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
int sumOfproduct(int n)
{
    return binomialCoeff(2 * n, n - 1);
}
 
// Driven Program
int main()
{
    int n = 3;
 
    cout << sumOfproduct(n) << endl;
    return 0;
}


Java
// Java Program to find sum of
// product of consecutive
// Binomial Coefficient.
import java.io.*;
 
class GFG
{
    static int MAX = 100;
     
    // Find the binomial coefficient
    // up to nth term
    static int binomialCoeff(int n,
                             int k)
    {
        int C[] = new int[k + 1];
         
        // memset(C, 0, sizeof(C));
        C[0] = 1; // nC0 is 1
 
        for (int i = 1; i <= n; i++)
        {
 
            // Compute next row of
            // pascal triangle
            // using the previous row
            for (int j = Math.min(i, k); j > 0; j--)
                C[j] = C[j] + C[j - 1];
    }
     
    return C[k];
}
 
// Return the sum of the
// product of consecutive
// binomial coefficient.
static int sumOfproduct(int n)
{
    return binomialCoeff(2 * n,
                         n - 1);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 3;
    System.out.println(sumOfproduct(n));
}
}
 
// This code is contributed
// by shiv_bhakt.


Python3
# Python3 Program to find sum of product
# of consecutive Binomial Coefficient.
MAX = 100;
 
# Find the binomial coefficient
# up to nth term
def binomialCoeff(n, k):
 
    C = [0] * (k + 1);
 
    C[0] = 1; # nC0 is 1
 
    for i in range(1, n + 1):
 
        # Compute next row of pascal triangle
        # using the previous row
        for j in range(min(i, k), 0, -1):
            C[j] = C[j] + C[j - 1];
    return C[k];
 
# Return the sum of the product of
# consecutive binomial coefficient.
def sumOfproduct(n):
    return binomialCoeff(2 * n, n - 1);
 
# Driver Code
n = 3;
print(sumOfproduct(n));
 
# This code is contributed by mits


C#
// C# Program to find sum of
// product of consecutive
// Binomial Coefficient.
using System;
 
class GFG
{
     
    // Find the binomial
    // coefficient up to
    // nth term
    static int binomialCoeff(int n,
                             int k)
    {
        int []C = new int[k + 1];
         
        // memset(C, 0, sizeof(C));
        C[0] = 1; // nC0 is 1
 
        for (int i = 1; i <= n; i++)
        {
 
            // Compute next row of
            // pascal triangle
            // using the previous row
            for (int j = Math.Min(i, k);
                             j > 0; j--)
                C[j] = C[j] + C[j - 1];
    }
     
    return C[k];
}
 
// Return the sum of the
// product of consecutive
// binomial coefficient.
static int sumOfproduct(int n)
{
    return binomialCoeff(2 * n,
                         n - 1);
}
 
// Driver Code
static public void Main ()
{
    int n = 3;
    Console.WriteLine(sumOfproduct(n));
}
}
 
// This code is contributed
// by @ajit.


PHP
 0; $j--)
            $C[$j] = $C[$j] + $C[$j - 1];
    }
    return $C[$k];
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
function sumOfproduct($n)
{
    return binomialCoeff(2 * $n, $n - 1);
}
 
// Driver Code
$n = 3;
echo sumOfproduct($n);
 
// This code is contributed by mits
?>


Javascript


输出

15

方法二:
我们知道,
(1 + x) n = n C 0 + n C 1 *x + n C 2 *x 2 + …。 + n C n *x n … (1)
(1 + 1/x) n = n C 0 + n C 1 /x + n C 2 /x 2 + …。 + n C n /x n … (2)
将(1)和(2)相乘,我们得到
(1 + x) 2n /x n = ( n C 0 + n C 1 *x + n C 2 *x 2 + …. + n C n *x n ) * ( n C 0 + n C 1 /x + n C 2 /x 2 + …. + n C n /x n )
( 2n C 0 + 2n C 1 *x + 2n C 2 *x 2 + …. + 2n C n *x n )/x n = ( n C 0 + n C 1 *x + n C 2 *x 2 + …. + n C n *x n ) * ( n C 0 + n C 1 /x + n C 2 /x 2 + …. + n C n /x n )
现在,在 LHS 中找到 x 的系数,
观察分子的第 r 个展开项是2n C r x r
要在 (1 + x) 2n /x n 中找到 x 的系数,r 应该是 n + 1,因为 x 在分母中的幂会减少它。
因此,LHS 中 x 的系数 = 2n C n + 12n C n – 1
现在,在 RHS 中找到 x 的系数,
乘法的第一次展开的第 r 项是n C r * x r
乘法的第二次展开的第 t 项是n C t / x t
所以乘法之后的项将是n C r * x r * n C t / x t
n C r * n C t * x r / x t
设 r = t + 1,我们得到,
n C t+1 * n C t * x
观察乘法的展开式中会有 n 个这样的项,所以 t 的范围是从 0 到 n – 1。
因此,RHS 中 x 的系数 = n C 0 * n C 1 + n C 1 * n C 2 + ….. + n C n-1 * n C n
比较 LHS 和 RHS 中 x 的系数,我们可以说,
n C 0 * n C 1 + n C 1 * n C 2 + ….. + n C n-1 * n C n = 2n C n – 1
下面是这种方法的实现:

C++

// CPP Program to find sum of product of
// consecutive Binomial Coefficient.
#include 
using namespace std;
#define MAX 100
 
// Find the binomial coefficient up to nth
// term
int binomialCoeff(int n, int k)
{
    int C[k + 1];
    memset(C, 0, sizeof(C));
 
    C[0] = 1; // nC0 is 1
 
    for (int i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle
        // using the previous row
        for (int j = min(i, k); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
    return C[k];
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
int sumOfproduct(int n)
{
    return binomialCoeff(2 * n, n - 1);
}
 
// Driven Program
int main()
{
    int n = 3;
 
    cout << sumOfproduct(n) << endl;
    return 0;
}

Java

// Java Program to find sum of
// product of consecutive
// Binomial Coefficient.
import java.io.*;
 
class GFG
{
    static int MAX = 100;
     
    // Find the binomial coefficient
    // up to nth term
    static int binomialCoeff(int n,
                             int k)
    {
        int C[] = new int[k + 1];
         
        // memset(C, 0, sizeof(C));
        C[0] = 1; // nC0 is 1
 
        for (int i = 1; i <= n; i++)
        {
 
            // Compute next row of
            // pascal triangle
            // using the previous row
            for (int j = Math.min(i, k); j > 0; j--)
                C[j] = C[j] + C[j - 1];
    }
     
    return C[k];
}
 
// Return the sum of the
// product of consecutive
// binomial coefficient.
static int sumOfproduct(int n)
{
    return binomialCoeff(2 * n,
                         n - 1);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 3;
    System.out.println(sumOfproduct(n));
}
}
 
// This code is contributed
// by shiv_bhakt.

蟒蛇3

# Python3 Program to find sum of product
# of consecutive Binomial Coefficient.
MAX = 100;
 
# Find the binomial coefficient
# up to nth term
def binomialCoeff(n, k):
 
    C = [0] * (k + 1);
 
    C[0] = 1; # nC0 is 1
 
    for i in range(1, n + 1):
 
        # Compute next row of pascal triangle
        # using the previous row
        for j in range(min(i, k), 0, -1):
            C[j] = C[j] + C[j - 1];
    return C[k];
 
# Return the sum of the product of
# consecutive binomial coefficient.
def sumOfproduct(n):
    return binomialCoeff(2 * n, n - 1);
 
# Driver Code
n = 3;
print(sumOfproduct(n));
 
# This code is contributed by mits

C#

// C# Program to find sum of
// product of consecutive
// Binomial Coefficient.
using System;
 
class GFG
{
     
    // Find the binomial
    // coefficient up to
    // nth term
    static int binomialCoeff(int n,
                             int k)
    {
        int []C = new int[k + 1];
         
        // memset(C, 0, sizeof(C));
        C[0] = 1; // nC0 is 1
 
        for (int i = 1; i <= n; i++)
        {
 
            // Compute next row of
            // pascal triangle
            // using the previous row
            for (int j = Math.Min(i, k);
                             j > 0; j--)
                C[j] = C[j] + C[j - 1];
    }
     
    return C[k];
}
 
// Return the sum of the
// product of consecutive
// binomial coefficient.
static int sumOfproduct(int n)
{
    return binomialCoeff(2 * n,
                         n - 1);
}
 
// Driver Code
static public void Main ()
{
    int n = 3;
    Console.WriteLine(sumOfproduct(n));
}
}
 
// This code is contributed
// by @ajit.

PHP

 0; $j--)
            $C[$j] = $C[$j] + $C[$j - 1];
    }
    return $C[$k];
}
 
// Return the sum of the product of
// consecutive binomial coefficient.
function sumOfproduct($n)
{
    return binomialCoeff(2 * $n, $n - 1);
}
 
// Driver Code
$n = 3;
echo sumOfproduct($n);
 
// This code is contributed by mits
?>

Javascript


输出:

15

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程