Suma del producto de Coeficientes Binomiales consecutivos

Dado un entero positivo n . La tarea es encontrar la suma del producto del coeficiente binomial consecutivo, es decir, 
n C 0 * n C 1 + n C 1 * n C 2 + ….. + n C n-1 * n C n 

Ejemplos:  

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

Método 1: La idea es encontrar todos los coeficientes binomiales hasta el término n y encontrar la suma del producto de los coeficientes consecutivos. 

A continuación se muestra la implementación de este enfoque: 

C++

// CPP Program to find sum of product of
// consecutive Binomial Coefficient.
#include <bits/stdc++.h>
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

<?php
// PHP Program to find sum
// of product of consecutive
// Binomial Coefficient.
$MAX = 100;
 
// Find the binomial
// coefficient upto
// nth term
function binomialCoeff($C, $n)
{
    $C[0] = 1; // nC0 is 1
 
    for ($i = 1;
         $i <= $n; $i++)
    {
 
        // Compute next row of
        // pascal triangle using
        // the previous row
        for ($j = min($i, $n);
             $j > 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

<script>
 
// Javascript Program to find sum of product of
// consecutive Binomial Coefficient.
var MAX = 100;
 
// Find the binomial coefficient upto nth term
function binomialCoeff(C, n)
{
    C[0] = 1; // nC0 is 1
 
    for (var i = 1; i <= n; i++) {
 
        // Compute next row of pascal triangle using
        // the previous row
        for (var 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.
function sumOfproduct(n)
{
    var sum = 0;
    var C = Array(MAX).fill(0);
 
    binomialCoeff(C, n);
 
    // finding the sum of product of
    // consecutive coefficient.
    for (var i = 0; i <= n; i++)
        sum += C[i] * C[i + 1];   
 
    return sum;
}
 
// Driven Program
var n = 3;
document.write( sumOfproduct(n));
 
</script>

Producción  

15

Método 2: 
Sabemos, 
(1 + x) n = n C 0 + n C 1 *x + n C 2 *x 2 + …. + norte C norte *x norte … (1)
(1 + 1/x) norte = norte C 0 + norte C 1 /x + norte C 2 /x 2 + …. + n C n /x n … (2)
Multiplicando (1) y (2), obtenemos 
(1 + x) 2n /x n= ( norte C 0 + norte C 1 *x + norte C 2 *x 2 + …. + norte C norte *x norte ) * ( norte C 0 + norte C 1 /x + norte C 2 /x 2 + …. + n C n /x n )
( 2n C 0 + 2n C 1 *x + 2n C 2 *x 2 + …. + 2nC norte *x norte )/x norte = ( norte C 0 + norte C 1 *x + norte C 2 *x 2 + …. + norte C norte *x norte ) * ( norte C 0 + norte C 1 /x + n C 2 /x 2 + …. + n C n /x n )
Ahora, encuentre el coeficiente de x en LHS, 
observe que el r-ésimo término de expansión en el numerador es 2n C rxr _
Para encontrar el coeficiente de x en (1 + x) 2n /x n , r debe ser n + 1, porque la potencia de x en el denominador lo reducirá. 
Entonces, coeficiente de x en LHS = 2n C n + 1 o 2n C n – 1
Ahora, encuentra el coeficiente de x en RHS, el 
r-ésimo término de la primera expansión de la multiplicación es n C r * x r 
t-ésimo término de la segunda expansión de la multiplicación es n C t / x t 
Así que el término después de la multiplicación será n C r * x r * nC t / x t
n C r * n C t * x r / x t 
Ponga r = t + 1, obtenemos, 
n C t+1 * n C t * x 
Observe que habrá n tal término en la expansión de multiplicar, por lo que t oscila entre 0 y n – 1. 
Por lo tanto, el coeficiente de x en RHS = n C 0 * n C 1 + n C 1 * n C 2 + ….. + n C n-1 * nC n
Comparando el coeficiente de x en LHS y RHS, podemos decir, 
n C 0 * n C 1 + n C 1 * n C 2 + ….. + n C n-1 * n C n = 2n C n – 1

A continuación se muestra la implementación de este enfoque:  

C++

// CPP Program to find sum of product of
// consecutive Binomial Coefficient.
#include <bits/stdc++.h>
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

<?php
// PHP Program to find sum of product of
// consecutive Binomial Coefficient.
$MAX = 100;
 
// Find the binomial coefficient
// up to nth term
function binomialCoeff($n, $k)
{
    $C = array_fill(0, ($k + 1), 0);
 
    $C[0] = 1; // nC0 is 1
 
    for ($i = 1; $i <= $n; $i++)
    {
 
        // Compute next row of pascal triangle
        // using the previous row
        for ($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.
function sumOfproduct($n)
{
    return binomialCoeff(2 * $n, $n - 1);
}
 
// Driver Code
$n = 3;
echo sumOfproduct($n);
 
// This code is contributed by mits
?>

Javascript

<script>
    // Javascript Program to find sum of
    // product of consecutive
    // Binomial Coefficient.
     
    let MAX = 100;
      
    // Find the binomial coefficient
    // up to nth term
    function binomialCoeff(n, k)
    {
        let C = new Array(k + 1);
        C.fill(0);
          
        // memset(C, 0, sizeof(C));
        C[0] = 1; // nC0 is 1
  
        for (let i = 1; i <= n; i++)
        {
  
            // Compute next row of
            // pascal triangle
            // using the previous row
            for (let 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.
  function sumOfproduct(n)
  {
      return binomialCoeff(2 * n, n - 1);
  }
   
  let n = 3;
  document.write(sumOfproduct(n));
 
// This code is contributed by suresh07.
</script>

Producción:  

15

Complejidad de tiempo: O(n*k)

Espacio Auxiliar : O(k)
 

Publicación traducida automáticamente

Artículo escrito por anuj0503 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *