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. python-nmap 使用

    安装 [root@localhost ~]# yum -y install nmap [root@localhost ~]# pip install python-nmap 使用 import nma ...

  2. chkcongfig 命令

    chkconfig   命令主要用来  更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig ...

  3. Apache httpd.conf配置文件 2(Main server configuration)

    ### Section 2: 'Main' server configuration # # The directives in this section set up the values used ...

  4. Linux 报错:syntax error "C" 解决办法(此处选择bash系统)

    出现此问题的原因,是由系统的兼容性引起的,linux下默认了指向dash而非bash. linux下Dash改Bash: 1.先查看是使用哪个shell ls -al /bin/sh 2.#如果是Da ...

  5. java连接Oracle数据库,从ResultSet中提取数据出现java.sql.sqlException结果集已耗尽

    出现错误的原因是ResultSet中并没有任何东西,再调用next()方法就会出错,原因可能是oracle创建用户,表没有提交,commit即可

  6. Django使用 djcelery时报ImportError: No module named south.db错误

    这时候可能是安装的Django-celery.celery的版本过低引起的,可以到pycharm查看推荐的版本,把版本更换到的推荐的版本就解决了

  7. Javase-坦克大战小游戏,为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着

    //为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着 package TanKe.lbl;import java.awt.*;import java.awt.ev ...

  8. Happycorp:1 Vulnhub Walkthrough

    靶机链接: https://www.vulnhub.com/entry/happycorp-1,296/ 网络主机扫描::: 主机端口扫描: NFS文件系统,尝试挂载试试 mount -t nfs 1 ...

  9. 【读书笔记】https://source.android.google.cn/devices/bootloader

    https://source.android.google.cn/devices/bootloader 本文主要记录aosp官网关于bootloader的相关资料 Bootloader A bootl ...

  10. opencv —— inpaint 图像修补、去除指定区域物体

    实现图像修补.物体去除:inpaint 函数 void inpaint(InputArray src, InputArray inpaintMask, OutputArray dst, double ...