热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

探索偶数次幂二项式系数的求和方法及其数学意义

求偶数指数二项式系数之和原文:https://www . geesforgeks . org/find-sum-偶数-指数-二项

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

原文:https://www . geesforgeks . org/find-sum-偶数-指数-二项式-系数/

给定正整数 n 。任务是找到偶数索引二项式系数的和。即
nC0+nC2+nC4+nC6+nC8+……..
例:

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

方法一:(蛮力)
思路是找出所有的二项式系数,只找出偶数索引值的和。

卡片打印处理机(Card Print Processor 的缩写)


// CPP Program to find sum
// of even index term
#include <bits/stdc++.h>
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 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.*/


服务器端编程语言(Professional Hypertext Preprocessor 的缩写)



// PHP Program to find sum
// of even index term
// Return the sum of
// even index term
function evenSum($n)
{
    $C = array(array());
    $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 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 = 0; $i <= $n; $i += 2)
        $sum += $C[$n][$i];
    return $sum;
}
// Driver Code
$n = 4;
echo evenSum($n) ;
// This code is contributed by anuj_67.
?>


java 描述语言


<script>
// Javascript Program to find sum
// of even index term
// Return the sum of
// even index term
function evenSum(n)
{
    var C =  Array.from(Array(n+1),
    ()=> Array(n+1).fill(0));
    var 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 sum of even index term.
    var sum = 0;
    for (var i = 0; i <= n; i += 2)
        sum += C[n][i];
    return sum;
}
// Driver Program
var n = 4;
document.write( evenSum(n) );  
script>   

输出:

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 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'.


服务器端编程语言(Professional Hypertext Preprocessor 的缩写)



// PHP Program to find sum
// even indexed Binomial
// Coefficient.
// Returns value of even indexed
// Binomial Coefficient Sum which
// is 2 raised to power n-1.
function evenbinomialCoeffSum( $n)
{
    return (1 << ($n - 1));
}
    // Driver Code
    $n = 4;
    echo evenbinomialCoeffSum($n);
// This code is contributed by anuj_67.
?>


java 描述语言



输出:

8

时间复杂度: O(1)
奇指标二项式系数之和
利用上面的结果我们很容易证明奇指标二项式系数之和也是 2 n-1


推荐阅读
  • 反向投影技术主要用于在大型输入图像中定位特定的小型模板图像。通过直方图对比,它能够识别出最匹配的区域或点,从而确定模板图像在输入图像中的位置。 ... [详细]
  • 哈密顿回路问题旨在寻找一个简单回路,该回路包含图中的每个顶点。本文将介绍如何判断给定的路径是否构成哈密顿回路。 ... [详细]
  • 对象自省自省在计算机编程领域里,是指在运行时判断一个对象的类型和能力。dir能够返回一个列表,列举了一个对象所拥有的属性和方法。my_list[ ... [详细]
  • Python处理Word文档的高效技巧
    本文详细介绍了如何使用Python处理Word文档,涵盖从基础操作到高级功能的各种技巧。我们将探讨如何生成文档、定义样式、提取表格数据以及处理超链接和图片等内容。 ... [详细]
  • 采用IKE方式建立IPsec安全隧道
    一、【组网和实验环境】按如上的接口ip先作配置,再作ipsec的相关配置,配置文本见文章最后本文实验采用的交换机是H3C模拟器,下载地址如 ... [详细]
  • 深入解析SpringMVC核心组件:DispatcherServlet的工作原理
    本文详细探讨了SpringMVC的核心组件——DispatcherServlet的运作机制,旨在帮助有一定Java和Spring基础的开发人员理解HTTP请求是如何被映射到Controller并执行的。文章将解答以下问题:1. HTTP请求如何映射到Controller;2. Controller是如何被执行的。 ... [详细]
  • 本文将探讨2015年RCTF竞赛中的一道PWN题目——shaxian,重点分析其利用Fastbin和堆溢出的技巧。通过详细解析代码流程和漏洞利用过程,帮助读者理解此类题目的破解方法。 ... [详细]
  • 开发笔记:9.八大排序
    开发笔记:9.八大排序 ... [详细]
  • 本文详细介绍了Python中列表的创建、访问、修改、排序及遍历等基本操作,帮助初学者快速掌握列表这一重要数据结构。 ... [详细]
  • 本文将详细探讨Linux pinctrl子系统的各个关键数据结构,帮助读者深入了解其内部机制。通过分析这些数据结构及其相互关系,我们将进一步理解pinctrl子系统的工作原理和设计思路。 ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 本文探讨了如何通过预处理器开关选择不同的类实现,并解决在特定情况下遇到的链接器错误。 ... [详细]
  • 本文介绍了如何在 Node.js 中使用 `setDefaultEncoding` 方法为可写流设置默认编码,并提供了详细的语法说明和示例代码。 ... [详细]
  • 由二叉树到贪心算法
    二叉树很重要树是数据结构中的重中之重,尤其以各类二叉树为学习的难点。单就面试而言,在 ... [详细]
  • 并发编程 12—— 任务取消与关闭 之 shutdownNow 的局限性
    Java并发编程实践目录并发编程01——ThreadLocal并发编程02——ConcurrentHashMap并发编程03——阻塞队列和生产者-消费者模式并发编程04——闭锁Co ... [详细]
author-avatar
沛妤林_654
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有