hdu 4349Xiao Ming's Hope

http://acm.hdu.edu.cn/showproblem.php?pid=4349

Xiao Ming's Hope

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 279    Accepted Submission(s): 198


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 girl friends, he coundn't help running into his classroom, and then opened his maths book preparing to count odd numbers. He looked at his book, then he found a question "C(n,0)+C(n,1)+C(n,2)+...+C(n,n)=?". 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 is equal to 1, C(1,0)=C(1,1)=1, there are 2 odd numbers. When n is equal to 2, C(2,0)=C(2,2)=1, there are 2 odd numbers...... Suddenly, he found a girl was watching him counting odd numbers. In order to show his gifts on maths, he wrote several big numbers what 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(1<=n<=108)
 
Output
A single line with the number of odd numbers of C(n,0),C(n,1),C(n,2)...C(n,n).
 
Sample Input
1 2 11
 
Sample Output
2 2 8
 
Source
超时代码:
思路更简单,分子含2的个数比分子多,则相当于一个数乘以2,必然为偶数,转化为求分母分子2的个数
View Code
 1 #include<stdio.h>
 2 int f(int x)
 3 {
 4     int sum=0;
 5     while(x)
 6     {
 7         sum+=x/2;
 8         x=x/2;
 9     }
10     return sum;
11 }
12 int main()
13 {
14     int n;
15     int i;
16     int sum;
17     while(~scanf("%d",&n))
18     {
19         sum=0;
20         for(i=0;i<=n/2;i++)
21         {
22             if(f(n)-f(i)-f(n-i)<=0) sum++;
23         }
24         sum*=2;
25         if(n%2==0&&f(n)-2*f(n/2)<=0) sum--;
26         printf("%d\n",sum);
27     }
28 }

运用二项式系数奇偶性+n分为前后两部分,水过,定理比较难证明

View Code
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,i;
 5     int sum;
 6     while(~scanf("%d",&n))
 7     {
 8         sum=0;
 9         for(i=0;i<=n/2;i++)
10         {
11             if(((n-i)&i)==0) sum++;
12         }
13         printf("%d\n",sum*2);
14     }
15 }

 Lucas定理,听都没听过。。。。弱到爆!此题,求n转换为二进制1的个数,超快

View Code
 1 #include<stdio.h>
 2 int main()
 3 {int s,n,i,g;
 4 while(~scanf("%d",&n))
 5 {g=1,s=0;
 6 while(n)
 7 {s+=(n&1);
 8 n/=2;}
 9 for(i=1;i<=s;i++)
10 {g*=2;
11 }
12 printf("%d\n",g);}}
原文地址:https://www.cnblogs.com/1114250779boke/p/2627924.html