📜  求偶指数二项式系数之和

📅  最后修改于: 2021-06-25 10:31:43             🧑  作者: Mango

给定正整数n 。任务是找到偶数索引的二项式系数之和。那是,
n C 0 + n C 2 + n C 4 + n C 6 + n C 8 +……..
例子 :

Input : n = 4
Output : 8
4C0 + 4C2 + 4C4
= 1 + 6 + 1
= 8

Input : n = 6
Output : 32

方法1 :(强力)
这个想法是找到所有二项式系数,并且只找到偶数索引值的总和。

CPP
// CPP Program to find sum
// of even index term
#include 
using namespace std;
 
// Return the sum of
// even index term
int evenSum(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 sum of even index term.
    int sum = 0;
    for (int i = 0; i <= n; i += 2)
        sum += C[n][i];
 
    return sum;
}
 
// Driver Program
int main()
{
    int n = 4;
    cout << evenSum(n) << endl;
    return 0;
}


Java
// Java Program to find sum
// of even index term
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Return the sum of
    // even index term
    static int evenSum(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;
      
                // else Calculate value using
                // previously stored values
                else
                    C[i][j] = C[i - 1][j - 1]
                                + C[i - 1][j];
            }
        }   
      
        // finding sum of even index term.
        int sum = 0;
        for (i = 0; i <= n; i += 2)
            sum += C[n][i];
      
        return sum;
    }
      
    // Driver Program
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(evenSum(n));
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python
# Python Program to find sum of even index term
import math
 
# Return the sum of even index term
def evenSum(n) :
    # Creates a list containing n+1 lists,
    # each of n+1 items, all set to 0
    C = [[0 for x in range(n + 1)] for y 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 sum of even index term
    sum = 0;
    for i in range(0, n + 1):
        if n % 2 == 0:
            sum = sum + C[n][i]
             
    return sum
     
# Driver method
n = 4
print evenSum(n)
 
 
# This code is contributed by 'Gitanjali'.


C#
// C# Program to find sum
// of even index term
using System;
 
class GFG {
     
    // Return the sum of
    // even index term
    static int evenSum(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;
     
                // else Calculate value using
                // previously stored values
                else
                    C[i,j] = C[i - 1,j - 1]
                            + C[i - 1,j];
            }
        }
     
        // finding sum of even index term.
        int sum = 0;
        for (i = 0; i <= n; i += 2)
            sum += C[n,i];
     
        return sum;
    }
     
    // Driver Program
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(evenSum(n));
    }
}
 
/*This code is contributed by vt_m.*/


PHP


Javascript


C++
// CPP Program to find sum even indexed Binomial
// Coefficient.
#include 
using namespace std;
 
// Returns value of even indexed Binomial Coefficient
// Sum which is 2 raised to power n-1.
int evenbinomialCoeffSum(int n)
{
    return (1 << (n - 1));
}
 
/* Driver program to test above function*/
int main()
{
    int n = 4;
    printf("%d", evenbinomialCoeffSum(n));
    return 0;
}


Java
// Java Program to find sum even indexed
// Binomial Coefficient.
import java.io.*;
 
class GFG {
// Returns value of even indexed Binomial Coefficient
// Sum which is 2 raised to power n-1.
static int evenbinomialCoeffSum(int n)
{
    return (1 << (n - 1));
}
 
// Driver Code
public static void main(String[] args)
{
int n = 4;
    System.out.println(evenbinomialCoeffSum(n));
}
    }
 
// This code is contributed by 'Gitanjali'.


Python
# Python program to find sum even indexed
# Binomial Coefficient
import math
 
# Returns value of even indexed Binomial Coefficient
# Sum which is 2 raised to power n-1.
def evenbinomialCoeffSum( n):
 
    return (1 << (n - 1))
 
# Driver method
if __name__ == '__main__':
    n = 4
    print evenbinomialCoeffSum(n)
 
# This code is contributed by 'Gitanjali'.


C#
// C# Program to find sum even indexed
// Binomial Coefficient.
using System;
 
class GFG
{
    // Returns value of even indexed
    // Binomial Coefficient Sum which
    // is 2 raised to power n-1.
    static int evenbinomialCoeffSum(int n)
    {
        return (1 << (n - 1));
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(evenbinomialCoeffSum(n));
    }
}
 
// This code is contributed by 'Vt_m'.


PHP


Javascript


输出 :

8

时间复杂度: O(n 2 )
方法2 :(使用公式)
指数二项式系数的和:
^nC_0 + ^nC_2 + ^nC_4 + ^nC_6 + .... = 2^{n-1}
证明 :

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

Now put x = -x, we get
(1 - x)n = nC0 - nC1 x + nC2 x2 + ..... + (-1)n nCn xn

Now, adding both the above equation, we get,
(1 + x)n + (1 - x)n = 2 * [nC0 + nC2 x2 + nC4 x4 + .......]

Put x = 1
(1 + 1)n + (1 - 1)n = 2 * [nC0 + nC2 + nC4 + .......]
2n/2 = nC0 + nC2 + nC4 + .......
2n-1 = nC0 + nC2 + nC4 + .......

以下是此方法的实现:

C++

// CPP Program to find sum even indexed Binomial
// Coefficient.
#include 
using namespace std;
 
// Returns value of even indexed Binomial Coefficient
// Sum which is 2 raised to power n-1.
int evenbinomialCoeffSum(int n)
{
    return (1 << (n - 1));
}
 
/* Driver program to test above function*/
int main()
{
    int n = 4;
    printf("%d", evenbinomialCoeffSum(n));
    return 0;
}

Java

// Java Program to find sum even indexed
// Binomial Coefficient.
import java.io.*;
 
class GFG {
// Returns value of even indexed Binomial Coefficient
// Sum which is 2 raised to power n-1.
static int evenbinomialCoeffSum(int n)
{
    return (1 << (n - 1));
}
 
// Driver Code
public static void main(String[] args)
{
int n = 4;
    System.out.println(evenbinomialCoeffSum(n));
}
    }
 
// This code is contributed by 'Gitanjali'.

Python

# Python program to find sum even indexed
# Binomial Coefficient
import math
 
# Returns value of even indexed Binomial Coefficient
# Sum which is 2 raised to power n-1.
def evenbinomialCoeffSum( n):
 
    return (1 << (n - 1))
 
# Driver method
if __name__ == '__main__':
    n = 4
    print evenbinomialCoeffSum(n)
 
# This code is contributed by 'Gitanjali'.

C#

// C# Program to find sum even indexed
// Binomial Coefficient.
using System;
 
class GFG
{
    // Returns value of even indexed
    // Binomial Coefficient Sum which
    // is 2 raised to power n-1.
    static int evenbinomialCoeffSum(int n)
    {
        return (1 << (n - 1));
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(evenbinomialCoeffSum(n));
    }
}
 
// This code is contributed by 'Vt_m'.

的PHP


Java脚本


输出:

8

时间复杂度: O(1)
奇指数二项式系数之和
使用以上结果,我们可以轻松证明奇数指数二项式系数的总和也为2 n-1