ZOJ 4081 Little Sub and Pascal's Triangle 题解

题意

求杨辉三角第n行(从1开始计数)有几个奇数。

考察的其实是杨辉——帕斯卡三角的性质,或者说Gould's sequence的知识。

其实网上很多题解都给出了答案,但大多数都只是给了一个结论或者说找规律(虽然我也是选择打表找规律先做的),但是思考为什么的时候我百度了一下,在wiki看了一些东西。

wiki Pascal's trianglehttps://en.wikipedia.org/wiki/Pascal%27s_triangle

  • Parity: To count odd terms in row n, convert n to binary. Let x be the number of 1s in the binary representation. Then the number of odd terms will be 2x. These numbers are the values in Gould's sequence.[20]

wiki Gould's sequencehttps://en.wikipedia.org/wiki/Gould%27s_sequence#cite_note-oeis-1

证明

严格的证明可以自行搜索相关论文。

我这里给一个基于二项式定理的证明。

最后一步是因为i只有取或者,为奇数,否则为偶数。这个的不懂的可以看后面。

上面的证明已经说明了当指数为的形式时,取奇数的只有首尾两项。

因此对于一个任意的指数n,我们可以分解为若干个的和——

。任意两个指数不相等。

注意我们需要统计杨辉——帕斯卡三角形第n行(从0开始计数,原题是从1开始计数,输入减去1就好)的奇数个数。所以我们应当关注的是二项式定理展开后各项的系数中为奇数的个数。

而我们上面这一步拆解为了m个式子的乘积,而且m个式子都只有2项,且各项的系数都是奇数。由于每一项中的x的指数都是形式,且各不相等,所以从这个m个括号式子中每个选一项(可以选或者),则得到展开后一个x的若干次方的项。容易发现不会有合并同类项的情况出现。而且奇数系数乘以奇数系数,系数仍然是奇数。

所以根据乘法定理,一共有项(并且系数一定为奇数)。

这就是说如果对于输入n,我们先减去1,在用二进制形式表示,统计1的个数,为m,则答案为.

i只有取或者,为奇数,否则为偶数

这个其实根据组合数的定义可以比较容易证明,

首先值为奇数的情况显然

现在考虑

①i是奇数

组合数是个里任意选择i个的方案数。

现在我们把个数排成一排,并且均分为左右两部分。使左右两部分关于中间成轴对称。

emmm……

一图胜千言

若有一种选法A,则将每一个选取的点替换为他的对称点,则得到另一种选法B。因为i是奇数,所以A和B必然是两种不同的选法。并且易得,一种选法的对称选法是唯一的。

所以总的选法的数量是偶数。

②i是偶数

i是偶数时,对于一种选法A我们依然可以通过对称的方法得到选法B。

但是由于i是偶数,所以可能会存在A和B是同一种选法的情况,所以

的奇偶性自对称(A,B相同)选法数的奇偶性

而自对称(A,B相同)选法数即.(左边个中任意选择一半,另一半由对称性在右边确认)

因此如果选取数依旧是偶数,则继续进行除以2的迭代。

这样,根据数论,经过有限步数迭代之后一定会达到选取数为奇数,转化为情况①。

毕。

源代码

import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
for (int i = 0;i < n; ++i) {
OddElementCountOfYanghuiTriangleRow p = new OddElementCountOfYanghuiTriangleRow();
System.out.println(p.count());
}
}
} class OddElementCountOfYanghuiTriangleRow{
long row;
OddElementCountOfYanghuiTriangleRow() {
row = Main.sc.nextLong()-1;
}
long count() {
long t = row;
long cnt = 1;
while (t > 0) {
if ((t & 1) != 0)
cnt <<= 1;
t >>= 1;
}
return cnt;
}
}

ZOJ 4081 Little Sub and Pascal's Triangle 题解的更多相关文章

  1. ZOJ - 4081:Little Sub and Pascal's Triangle (结论)

    Little Sub is about to take a math exam at school. As he is very confident, he believes there is no ...

  2. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  5. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  6. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  7. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

  8. LeetCode - Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  9. 【leetcode】Pascal's Triangle I & II (middle)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

随机推荐

  1. Angular常用命令

    一. Angular常用命令 1. ng new 文件夹名 (新建项目,选择y使用路由) 2. ng serve --open (默认浏览器运行项目) 3. ng serve --port 6060  ...

  2. phpmyadmin配置文件详解

    PHPMyadmin配置文件config.inc.php或config.default.php内容及作用解析大致如下: /** * phpMyAdmin Configuration File * * ...

  3. Vue之Vuex的使用

    重点看懂这张图: 重点记住: 1.Mutation 必须是同步函数,即mutations里只能处理同步操作. 2.如果处理的是同步操作可直接commit提交mutations更改state,如果是异步 ...

  4. 题解【[USACO05NOV]奶牛玩杂技】

    \[ \texttt{Description} \] 有 \(n\) 头牛,每头牛都有自己的体重 \(W_i\) 和力量 \(S_i\) . 将这 \(n\) 头牛摞在一起,每头牛的压扁指数定义为:压 ...

  5. Elasticsearch之文档的增删改查以及ik分词器

    文档的增删改查 增加文档 使用elasticsearch-head查看 修改文档 使用elasticsearch-head查看 删除文档 使用elasticsearch-head查看 查看文档的三种方 ...

  6. 数据库自学笔记(2)--- HAVING和WHERE, ANY 和 ALL,IN和EXIST。

    1.HAVING和WHERE: WHERE 和 HAVING 的作用对象不一样.WHERE作用于基本表或视图,挑出满足条件的元组.HAVING作用于组(group),一般配合GROUP BY 使用. ...

  7. vue及vant框架,多语言配置

    1.安装 vue-i18n,( cnpm install vue-i18n --save ) 2.在入口,main.js 中引入 (import Vuei18n from "vue-i18n ...

  8. Eclipse部署项目,常见几个问题解决方案

    一.java compiler level does not match the version of the inst 解决方案:(一般出现在拷贝项目) 第一步:在Eclipse环境中,鼠标右键选择 ...

  9. 从HTML到node.js以及跨域问题的解决

    废话不多说,直接上代码 网页客户端 <!DOCTYPE html> <html> <head> <meta http-equiv="Content- ...

  10. Umi 小白纪实(一)—— 创建项目&常用配置

    umi 是一个企业级 react 应用框架,也是蚂蚁金服的底层前端框架 <蚂蚁金服的前端框架和工程化实践> 一.安装脚手架 在创建项目之前,需要保证有 node 8.10 以上的环境 可以 ...