HDU 4349 Xiao Ming‘s Hope

Problem Description

  Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2011.11.11 comes. Seeing classmates walking with their girlfriends, he couldn’t help running into his classroom, and then opened his math book preparing to count odd numbers. He looked at his book, then he found a question : C n 0 C_n^0 Cn0 + + + C n 1 C_n^1 Cn1 + + + C n 2 C_n^2 Cn2 + + + ⋯ + \cdots+ + C n n =   ? C_n^n=\ ? Cnn= ? Of course, Xiao Ming knew the answer, but he didn’t care about that , What he wanted to know was that how many odd numbers there were? Then he began to count odd numbers. When n n n is equal to 1 1 1, C 1 0 = C 1 1 = 1 C_1^0=C_1^1=1 C10=C11=1, there are 2 2 2 odd numbers. When n n n is equal to 2 2 2, C 2 0 = C 2 2 C_2^0=C_2^2 C20=C22 = = = 1 1 1, there are 2 2 2 odd numbers… Suddenly, he found a girl was watching him counting odd numbers. In order to show his gifts on math, he wrote several big numbers what n n n would be equal to, but he found it was impossible to finished his tasks, then he sent a piece of information to you, and wanted you a excellent programmer to help him, he really didn’t want to let her down. Can you help him?

Input

  Each line contains a integer n n n ( 1 ≤ n ≤ 1 0 8 ) (1\le n\le 10^8) (1n108).

Output

  A single line with the number of odd numbers of C n 0 C_n^0 Cn0 , , , C n 1 C_n^1 Cn1 , , , C n 2 C_n^2 Cn2 , , , ⋯   , \cdots, , C n n C_n^n Cnn.

Sample Input

1
2
11

Sample Output

2
2
8

Translation

  给定正整数 n n n,输出 ∑ i = 0 n [ C n i ≡ 1 ( m o d 2 ) ] \sum\limits_{i=0}^n \left[C_{n}^i\equiv1\pmod 2\right] i=0n[Cni1(mod2)]

Idea

  对于和式的一项 C n i C_n^i Cni,设 i , n i,n i,n k k k 位二进制数(不会产生溢出), n = n k n k − 1 ⋯ n 1 ‾ n=\overline{n_kn_{k-1}\cdots n_1} n=nknk1n1 i = i k i k − 1 ⋯ i 1 ‾ i=\overline{i_ki_{k-1}\cdots i_1} i=ikik1i1。根据卢卡斯定理,可将 C n i C_n^i Cni 拆解。
( n i ) = ( n   m o d   2 i   m o d   2 ) × ( ⌊ n 2 ⌋ ⌊ i 2 ⌋ ) = ( n   m o d   2 i   m o d   2 ) × ( ⌊ n 2 ⌋   m o d   2 ⌊ i 2 ⌋   m o d   2 ) × ( ⌊ n 4 ⌋ ⌊ i 4 ⌋ ) ⋮ = ( n 1 i 1 ) × ( n 2 i 2 ) × ⋯ × ( n k i k ) = ∏ j = 1 k ( n j i j ) \begin{aligned}\begin{pmatrix}n\\i\end{pmatrix}&=\begin{pmatrix}n\bmod 2\\i\bmod 2\end{pmatrix}\times\begin{pmatrix}\left\lfloor\frac{n}{2}\right\rfloor\\\left\lfloor\frac{i}{2}\right\rfloor\end{pmatrix}\\&=\begin{pmatrix}n\bmod 2\\i\bmod 2\end{pmatrix}\times\begin{pmatrix}\left\lfloor\frac{n}{2}\right\rfloor\bmod 2\\\left\lfloor\frac{i}{2}\right\rfloor\bmod 2\end{pmatrix}\times\begin{pmatrix}\left\lfloor\frac{n}{4}\right\rfloor\\\left\lfloor\frac{i}{4}\right\rfloor\end{pmatrix}\\&\quad\quad\quad\quad\quad\quad\quad\quad\quad\vdots\\&=\begin{pmatrix}n_1\\i_1\end{pmatrix}\times\begin{pmatrix}n_2\\i_2\end{pmatrix}\times\cdots\times\begin{pmatrix}n_k\\i_k\end{pmatrix}\\&=\prod\limits_{j=1}^{k}\begin{pmatrix}n_j\\i_j\end{pmatrix}\end{aligned} (ni)=(nmod2imod2)×(2n2i)=(nmod2imod2)×(2nmod22imod2)×(4n4i)=(n1i1)×(n2i2)××(nkik)=j=1k(njij)
  当且仅当 ∀   1 ≤ j ≤ k \forall\ 1\le j\le k  1jk,有 ( n j i j ) = 1 \begin{pmatrix}n_j\\i_j\end{pmatrix}=1 (njij)=1 C n i C_n^i Cni 为奇数。遍历 n n n k k k 个二进制位,若 n j = 1 n_j=1 nj=1,那么 i j = 0 i_j=0 ij=0 i j = 1 i_j=1 ij=1,两种选择都满足 ( n j i j ) = 1 \begin{pmatrix}n_j\\i_j\end{pmatrix}=1 (njij)=1(产生可行的两个分支,数量 × 2 \times 2 ×2);若 n j = 0 n_j=0 nj=0,只有 i j = 0 i_j=0 ij=0 满足 ( n j i j ) = 1 \begin{pmatrix}n_j\\i_j\end{pmatrix}=1 (njij)=1(不产生可行的分支,数量 × 1 \times 1 ×1)。设 c n t cnt cnt n n n 的二进制中 1 1 1 的个数,那么答案为 2 c n t 2^{cnt} 2cnt

代码

#include<bitset>
#include<cstdio>
using namespace std;
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		//装入bitset
		//调用函数
		//bs.count()表示1的数量
		bitset<64>bs(n);
		printf("%d\n",1<<bs.count());
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值