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 : 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 is equal to , , there are odd numbers. When is equal to , , there are 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 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 .
Output
A single line with the number of odd numbers of .
Sample Input
1
2
11
Sample Output
2
2
8
Translation
给定正整数 ,输出 。
Idea
对于和式的一项 ,设 为 位二进制数(不会产生溢出),,。根据卢卡斯定理,可将 拆解。
$$ \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} $$当且仅当 ,有 $\begin{pmatrix}n_j\\i_j\end{pmatrix}=1$, 为奇数。遍历 的 个二进制位,若 ,那么 或 ,两种选择都满足 $\begin{pmatrix}n_j\\i_j\end{pmatrix}=1$(产生可行的两个分支,数量 );若 ,只有 满足 $\begin{pmatrix}n_j\\i_j\end{pmatrix}=1$(不产生可行的分支,数量 )。设 为 的二进制中 的个数,那么答案为 。
代码
1 |
|