In mathematics, any of the positive integers that occurs as a coefficient in the binomial theorem is a binomial coefficient. Commonly, a binomial coefficient is indexed by a pair of integers n ≥ k ≥ 0 and is written {\displaystyle {\tbinom {n}{k}}.} {\displaystyle {\tbinom {n}{k}}.} It is the coefficient of the xk term in the polynomial expansion of the binomial power (1 + x)n, and it is given by the formula.

英文描述

英文描述请参考下面的图。

中文描述

根据给定的公式计算二项式的值。

在这里有一个说明需要注意的是,如果结果超过 1,000,000,000 你的程序应该返回 -1。

如果结果没有定义的话,那么你的程序应该也要返回 -1。

思路和点评

在这里的计算,公式比较简单,就是计算 N,K N-K 的阶乘,在阶乘中,你可以使用递归进行计算。

但是需要注意的是对这个数字的阶乘计算量,程序是很容易溢出的,如果从出题者的意图来看就是要考察大数值的计算和计算中的溢出。

如果你使用的是 Java 的话,你应该使用类 BigDecimal,进行计算。如果你可以使用 Apache Common Math 的话,你就直接使用 CombinatoricsUtils.factorialDouble 进行计算。在计算中允许的最大参数值为 170,超过这个值以后就超过程序能够计算的最大值了。

如果你希望直接计算二项式系数的话,你可以使用 CombinatoricsUtils.binomialCoefficientDouble(40, 20) 直接进行计算。

源代码

源代码和有关代码的更新请访问 GitHub:

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/test/java/com/ossez/codebank/interview/tests/WayfairTest.java

测试类请参考:

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/test/java/com/ossez/codebank/interview/tests/WayfairTest.java

代码思路请参考:

/**
* https://www.cwiki.us/display/ITCLASSIFICATION/Binomial+Coefficient
*
* Binomial Coefficient
*/
@Test
public void testBinomialCoefficient() {
int n = 40;
int k = 20; BigDecimal bc = factorial(n).divide(factorial(k).multiply(factorial(n - k)));
// a.compareTo(new BigDecimal(1000000000))
logger.debug("{}", bc);
logger.debug("Check for Compare To - [{}]", bc.compareTo(new BigDecimal(1000000000)));
logger.debug("Value - [{}]", bc); logger.debug("Apache CombinatoricsUtils Factorial - [{}]", CombinatoricsUtils.factorialDouble(20));
logger.debug("Apache CombinatoricsUtils Binomial Coefficient - [{}]", CombinatoricsUtils.binomialCoefficientDouble(40, 20)); } /**
* for factorial
*
* @param x
* @return
*/
private static BigDecimal factorial(int x) {
if (x == 1 || x == 0) {
return BigDecimal.valueOf(1);
} else {
return BigDecimal.valueOf(x).multiply(factorial(x - 1));
}
}

测试结果

上面程序的测试结果如下:

2018/12/29 19:35:10 DEBUG [com.ossez.codebank.interview.tests.WayfairTest] - 137846528820
2018/12/29 19:35:10 DEBUG [com.ossez.codebank.interview.tests.WayfairTest] - Check for Compare To - [1]
2018/12/29 19:35:10 DEBUG [com.ossez.codebank.interview.tests.WayfairTest] - Value - [137846528820]
2018/12/29 19:35:10 DEBUG [com.ossez.codebank.interview.tests.WayfairTest] - Apache CombinatoricsUtils Factorial - [2.43290200817664E18]
2018/12/29 19:35:10 DEBUG [com.ossez.codebank.interview.tests.WayfairTest] - Apache CombinatoricsUtils Binomial Coefficient - [1.3784652882E11]

Binomial Coefficient(二项式系数)的更多相关文章

  1. 关于 Binomial Coefficient is Fun

    题目传送门 Solution 应该这个做法不是很常见吧. 我们设 \(f_{i,j}\) 表示前面 \(i\) 个数,选出的数和为 \(j\) 的贡献之和.因为我们有以下式子: \[\sum_{i=a ...

  2. Solution -「ARC 110D」Binomial Coefficient is Fun

    \(\mathcal{Description}\)   Link.   给定非负整数序列 \(\{a_n\}\),设 \(\{b_n\}\) 是一个非负整数序列且 \(\sum_{i=1}^nb_i\ ...

  3. Codeforces/TopCoder/ProjectEuler/CodeChef 散题笔记 (持续更新)

    最近做到了一些有趣的散题,于是开个Blog记录一下吧… (如果有人想做这些题的话还是不要看题解吧…) 2017-03-16 PE 202 Laserbeam 题意:有一个正三角形的镜子屋,光线从$C$ ...

  4. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  5. Lucas定理

    Lucas' theorem In number theory, Lucas's theorem expresses the remainder of division of the binomial ...

  6. Conjugate prior relationships

    Conjugate prior relationships The following diagram summarizes conjugate prior relationships for a n ...

  7. java积累

    数组的使用 package javaDemo; import java.util.*; /** * * @author Administrator * @version 1.0 * * */ publ ...

  8. OI不得不知的那些数学定理

    Binomial theorem One can define\[{r \choose k}=\frac{r\,(r-1) \cdots (r-k+1)}{k!} =\frac{(r)_k}{k!}\ ...

  9. UVA10375 Choose and divide 质因数分解

    质因数分解: Choose and divide Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

随机推荐

  1. sublime使用技巧总结

    软件右下角可以选择文档语法模式 Ctrl + Shift + P ------------------命令模式sshtml模糊匹配-----语法切换到html模式,同理所得,ss+相应文件名匹配相应模 ...

  2. ES6中新增的数组知识

    JSON数组格式转换 JSON的数组格式就是为了前端快速的把JSON转换成数组的一种格式,我们先来看一下JSON的数组格式怎么写. let  json = {     '0': 'xzblogs', ...

  3. Facebook ads_Business Manager

    xmind 下载链接

  4. 1、iptables-netfilter基础

    .iptables: 包过滤型防火墙功能.四表五链 .iptables规则.规则管理工具.iptables命令 .iptables链管理.规则管理.查看等 .iptables匹配条件.目标.显式扩展. ...

  5. SAP月结操作讲解

    SAP月结操作讲解 https://wenku.baidu.com/view/ac6fe45d312b3169a451a4b9.html   步聚 操作内容 事务码 是否必须 操作时间 月/年结 1 ...

  6. _event

      EventId  事件ID 请使用大于100的ID EventName 事件的名称,用于游戏中各种提示 NoticeText  事件开始时的弹窗内容 GossipText  功能宝石等菜单内容 Z ...

  7. hdu 2034 改革春风吹满地 多边形面积

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem ...

  8. P1262 间谍网络

    传送门 思路: ①在 Tarjan 的基础上加一个 belong 记录每个点属于哪个强连通分量. ②存图完成后,暴力地遍历全图,查找是否要间谍不愿受贿. inline void dfs(int u) ...

  9. Virtualbox-CentOS网络配置

    1.记下虚拟网卡IP 2. 配置网卡1.网卡2 网卡1 ---对应---eth0----NAT(网络地址转换)用来与外网通信 网卡2 ---对应---eth1----Host-only用来与主机通信 ...

  10. CentOS6.5下安装配置MySQL数据库

    一.MySQL简介 说到数据库,我们大多想到的是关系型数据库,比如MySQL.Oracle.SQLServer等等,这些数据库软件在Windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...