📜  二项式系数的平方和

📅  最后修改于: 2021-06-25 16:18:49             🧑  作者: Mango

给定正整数n 。任务是找到二项式系数的平方和,即
n C 0 2 + n C 1 2 + n C 2 2 + n C 3 2 +………+ n C n-2 2 + n C n-1 2 + n C n 2
例子:

Input : n = 4
Output : 70

Input : n = 5
Output : 252

方法1 :(强力)
想法是生成二项式系数的所有项,并找到每个二项式系数的平方和。
下面是此方法的实现:

C++
// CPP Program to find the sum of square of
// binomial coefficient.
#include
using namespace std;
 
// Return the sum of square of binomial coefficient
int sumofsquare(int n)
{
    int C[n+1][n+1];
    int i, j;
 
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, n); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
 
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }   
     
    // Finding the sum of square of binomial
    // coefficient.
    int sum = 0;
    for (int i = 0; i <= n; i++)   
        sum += (C[n][i] * C[n][i]);   
     
    return sum;
}
 
// Driven Program
int main()
{
    int n = 4;   
    cout << sumofsquare(n) << endl;
    return 0;
}


Java
// Java Program to find the sum of
// square of binomial coefficient.
import static java.lang.Math.*;
 
class GFG{
         
    // Return the sum of square of
    // binomial coefficient
    static int sumofsquare(int n)
    {
        int[][] C = new int [n+1][n+1] ;
        int i, j;
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (i = 0; i <= n; i++)
        {
            for (j = 0; j <= min(i, n); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
     
                // Calculate value using
                //previously stored values
                else
                    C[i][j] = C[i-1][j-1]
                             + C[i-1][j];
            }
        }
         
        // Finding the sum of square of
        // binomial coefficient.
        int sum = 0;
        for (i = 0; i <= n; i++)
            sum += (C[n][i] * C[n][i]);
         
        return sum;
    }
     
    // Driver function
    public static void main(String[] args)
    {
        int n = 4;
         
        System.out.println(sumofsquare(n));
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


Python3
# Python Program to find
# the sum of square of
# binomial coefficient.
 
# Return the sum of
# square of binomial
# coefficient
def sumofsquare(n) :
     
    C = [[0 for i in range(n + 1)]
            for j in range(n + 1)]
             
    # Calculate value of
    # Binomial Coefficient
    # in bottom up manner
    for i in range(0, n + 1) :
     
        for j in range(0, min(i, n) + 1) :
                     
            # Base Cases
            if (j == 0 or j == i) :
                C[i][j] = 1
 
            # Calculate value
            # using previously
            # stored values
            else :
                C[i][j] = (C[i - 1][j - 1] +
                           C[i - 1][j])
 
     
    # Finding the sum of
    # square of binomial
    # coefficient.
    sum = 0
    for i in range(0, n + 1) :
        sum = sum + (C[n][i] *
                     C[n][i])
     
    return sum
 
 
# Driver Code
n = 4
print (sumofsquare(n), end="\n")
     
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# Program to find the sum of
// square of binomial coefficient.
using System;
 
class GFG {
         
    // Return the sum of square of
    // binomial coefficient
    static int sumofsquare(int n)
    {
        int[,] C = new int [n+1,n+1] ;
        int i, j;
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (i = 0; i <= n; i++)
        {
            for (j = 0; j <= Math.Min(i, n); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i,j] = 1;
     
                // Calculate value using
                //previously stored values
                else
                    C[i,j] = C[i-1,j-1]
                            + C[i-1,j];
            }
        }
         
        // Finding the sum of square of
        // binomial coefficient.
        int sum = 0;
        for (i = 0; i <= n; i++)
            sum += (C[n,i] * C[n,i]);
         
        return sum;
    }
     
    // Driver function
    public static void Main()
    {
        int n = 4;
         
        Console.WriteLine(sumofsquare(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP Program to find the sum of square of
// binomial coefficient.
#include
using namespace std;
 
// function to return product of number
// from start to end.
int factorial(int start, int end)
{
    int res = 1;
    for (int i = start; i <= end; i++)
        res *= i;
         
    return res;
}
 
// Return the sum of square of binomial
// coefficient
int sumofsquare(int n)
{
    return factorial(n+1, 2*n)/factorial(1, n);
}
 
// Driven Program
int main()
{
    int n = 4;   
    cout << sumofsquare(n) << endl;
    return 0;
}


Java
// Java Program to find the sum of square of
// binomial coefficient.
class GFG{
     
    // function to return product of number
    // from start to end.
    static int factorial(int start, int end)
    {
        int res = 1;
        for (int i = start; i <= end; i++)
            res *= i;
             
        return res;
    }
     
    // Return the sum of square of binomial
    // coefficient
    static int sumofsquare(int n)
    {
        return factorial(n+1, 2*n)/factorial(1, n);
    }
     
    // Driven Program
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(sumofsquare(n));
    }
}
 
// This code is contributed by
// Smitha DInesh Semwal


Python
# Python 3 Program to find the sum of
# square of binomial coefficient.
 
# function to return product of number
# from start to end.
def factorial(start, end):
 
    res = 1
     
    for i in range(start, end + 1):
        res *= i
         
    return res
 
# Return the sum of square of binomial
# coefficient
def sumofsquare(n):
 
    return int(factorial(n + 1, 2 * n)
                     /factorial(1, n))
 
# Driven Program
 
n = 4
print(sumofsquare(n))
 
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# Program to find the sum of square of
// binomial coefficient.
using System;
 
class GFG {
     
    // function to return product of number
    // from start to end.
    static int factorial(int start, int end)
    {
        int res = 1;
         
        for (int i = start; i <= end; i++)
            res *= i;
             
        return res;
    }
     
    // Return the sum of square of binomial
    // coefficient
    static int sumofsquare(int n)
    {
        return factorial(n+1, 2*n)/factorial(1, n);
    }
     
    // Driven Program
    public static void Main()
    {
        int n = 4;
         
        Console.WriteLine(sumofsquare(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

70

方法2 :(使用公式)
^nC^2_0 + ^nC^2_1 + ^nC^2_2 + .... + ^nC^2_n-1 + ^nC^2_n
= ^2nC_n
= \frac{(2n)!}{(n!)^2}
证明,

We know,
(1 + x)n = nC0 + nC1 x + nC2 x2 + ......... + nCn-1 xn-1 + nCn-1 xn
Also,
(x + 1)n = nC0 xn + nC1 xn-1 + nC2 xn-2 + ......... + nCn-1 x + nCn

Multiplying above two equations,
(1 + x)2n = [nC0 + nC1 x + nC2 x2 + ......... + nCn-1 xn-1 + nCn-1 xn] X 
            [nC0 xn + nC1 xn-1 + nC2 xn-2 + ......... + nCn-1 x + nCn]

Equating coefficients of xn on both sides, we get
2nCn = nC02 + nC12 + nC22 + nC32 + ......... + nCn-22 + nCn-12 + nCn2

Hence, sum of the squares of coefficients = 2nCn = (2n)!/(n!)2.

此外,(2n)!/(n!) 2 =(2n *(2n – 1)*(2n – 2)*….. *(n + 1))/(n *(n – 1)*(n – 2)*….. * 1)。
下面是此方法的实现:

C++

// CPP Program to find the sum of square of
// binomial coefficient.
#include
using namespace std;
 
// function to return product of number
// from start to end.
int factorial(int start, int end)
{
    int res = 1;
    for (int i = start; i <= end; i++)
        res *= i;
         
    return res;
}
 
// Return the sum of square of binomial
// coefficient
int sumofsquare(int n)
{
    return factorial(n+1, 2*n)/factorial(1, n);
}
 
// Driven Program
int main()
{
    int n = 4;   
    cout << sumofsquare(n) << endl;
    return 0;
}

Java

// Java Program to find the sum of square of
// binomial coefficient.
class GFG{
     
    // function to return product of number
    // from start to end.
    static int factorial(int start, int end)
    {
        int res = 1;
        for (int i = start; i <= end; i++)
            res *= i;
             
        return res;
    }
     
    // Return the sum of square of binomial
    // coefficient
    static int sumofsquare(int n)
    {
        return factorial(n+1, 2*n)/factorial(1, n);
    }
     
    // Driven Program
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(sumofsquare(n));
    }
}
 
// This code is contributed by
// Smitha DInesh Semwal

Python

# Python 3 Program to find the sum of
# square of binomial coefficient.
 
# function to return product of number
# from start to end.
def factorial(start, end):
 
    res = 1
     
    for i in range(start, end + 1):
        res *= i
         
    return res
 
# Return the sum of square of binomial
# coefficient
def sumofsquare(n):
 
    return int(factorial(n + 1, 2 * n)
                     /factorial(1, n))
 
# Driven Program
 
n = 4
print(sumofsquare(n))
 
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# Program to find the sum of square of
// binomial coefficient.
using System;
 
class GFG {
     
    // function to return product of number
    // from start to end.
    static int factorial(int start, int end)
    {
        int res = 1;
         
        for (int i = start; i <= end; i++)
            res *= i;
             
        return res;
    }
     
    // Return the sum of square of binomial
    // coefficient
    static int sumofsquare(int n)
    {
        return factorial(n+1, 2*n)/factorial(1, n);
    }
     
    // Driven Program
    public static void Main()
    {
        int n = 4;
         
        Console.WriteLine(sumofsquare(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

70