ZOJ 4081 Little Sub and Pascal's Triangle 题解
ZOJ 4081 Little Sub and Pascal's Triangle 题解
题意
求杨辉三角第n行(从1开始计数)有几个奇数。
考察的其实是杨辉——帕斯卡三角的性质,或者说Gould's sequence的知识。
其实网上很多题解都给出了答案,但大多数都只是给了一个结论或者说找规律(虽然我也是选择打表找规律先做的),但是思考为什么的时候我百度了一下,在wiki看了一些东西。
wiki Pascal's triangle(https://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 sequence(https://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 题解的更多相关文章
- 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 ...
- [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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 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 ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- session和cookie的最深刻理解
先说session 对SESSION的争论好象一直没有停止过,不过幺麽能理解SESSION的人应该占90以上.但还是讲讲,别嫌老~ 有一些人赞成用SESSION,有一些人不赞成.但这个问题到底要怎么说 ...
- Leetcode:96. 不同的二叉搜索树
Leetcode:96. 不同的二叉搜索树 Leetcode:96. 不同的二叉搜索树 题目在链接中,点进去看看吧! 先介绍一个名词:卡特兰数 卡特兰数 卡特兰数Cn满足以下递推关系: \[ C_{n ...
- 用bootstrap来放置天气和图标的位置 自适应
今天写了个关于天气的页面,他的摆放位置有点难,花了一两个小时用bootstrap来摆放,但是感觉bug很多 所以今天写下自己的心得,放上代码,以后这种就知道怎么写了 <div class=&qu ...
- Android Studio安装虚拟机步骤
1. 新建 或者 2. 按图中的步骤,选择一款设备,建议选择分辨率小的,不占内存 3. 按图 4. 按图 5. 点击finish后,选择之前下载的虚拟机,点next 6. 虚拟机重命名 7. 到这里, ...
- pikachu-跨站脚本漏洞(XSS)
一.跨站脚本漏洞概述 1.1 什么是XSS漏洞? XSS是一种发生在Web前端的漏洞,其危害的对象也主要是前端用户. 1.2 XSS如何攻击? 二.跨站脚本漏洞类型及测试流程 2.1 跨站脚本 ...
- Blend 修改TreeViewItem样式
Blend 修改TreeViewItem样式 1.用Blend for Visual Studio 2019 新建Wpf项目,拖动一个TreeView控件到Grid上 <Grid> < ...
- npm安装Vue.js
我之前是有安装过npm的 使用淘宝 NPM 镜像 $ npm install -g cnpm --registry=https://registry.npm.taobao.org 查看nmp版本 $ ...
- vue 路由过渡动效
<router-view> 是基本的动态组件,所以我们可以用 <transition> 组件给它添加一些过渡效果: <transition name="slid ...
- 使用 linux kernel +busybox 定制linux系统
目的: 了解linux的启动过程 主要内容: 1.grub 是启动程序的bootloader 2.linux-kernel 是linux的开源内核 3.busybox 是linux的工具集合 启动顺序 ...
- nCompass-产品配置基础
nCompass-产品配置基础 设备上架后,浏览器登陆设备的管理IP,输入用户名和密码, 登入进入视图展示页面 1. 添加许可 新设备上架之后,要添加许可方能使用. 步骤: 系统设置 --- 许可-- ...