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) (1≤n≤108).
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=0∑n[Cni≡1(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=nknk−1⋯n1,
i
=
i
k
i
k
−
1
⋯
i
1
‾
i=\overline{i_ki_{k-1}\cdots i_1}
i=ikik−1⋯i1。根据卢卡斯定理,可将
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)×(⌊2n⌋⌊2i⌋)=(nmod2imod2)×(⌊2n⌋mod2⌊2i⌋mod2)×(⌊4n⌋⌊4i⌋)⋮=(n1i1)×(n2i2)×⋯×(nkik)=j=1∏k(njij)
当且仅当
∀
1
≤
j
≤
k
\forall\ 1\le j\le k
∀ 1≤j≤k,有
(
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;
}