codeforces #320 div 2A - Raising Bacteria (位运算)

A - Raising Bacteria
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

You are a lover of bacteria. You want to raise some bacteria in a box.

Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment.

What is the minimum number of bacteria you need to put into the box across those days?

Input

The only line containing one integer x (1 ≤ x ≤ 109).

Output

The only line containing one integer: the answer.

Sample Input

Input
5
Output
2
Input
8
Output
1

Hint

For the first sample, we can add one bacterium in the box in the first day morning and at the third morning there will be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so the answer is 2.

For the second sample, we can put one in the first morning and in the 4-th morning there will be 8 in the box. So the answer is 1.a

 

x的二进制表示中1的个数即为答案.

原因是,每天晚上糖果数量翻倍,相当于左移1位,这时候二进制表示中1的数量不变

也就是说,二进制表示中的所有的1,一定都是添加进去的

而且也只有二进制表示中的1是添加进去的

所以二进制表示中1的数量,就是添加的糖果数.

 1 /*************************************************************************
 2     > File Name: code/cf/#320/A.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年09月18日 星期五 23时18分34秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)  
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 int main()
32 {
33   #ifndef  ONLINE_JUDGE 
34   
35   #endif
36     int x;
37     cin>>x;
38     int ans = 0 ;
39     while (x)
40     {
41     if (x%2==1) ans++;
42     x = x / 2;
43     }
44     cout<<ans<<endl;
45   
46  #ifndef ONLINE_JUDGE  
47   fclose(stdin);
48   #endif
49     return 0;
50 }
View Code

 

 

posted @ 2015-09-18 23:28  111qqz  阅读(454)  评论(0编辑  收藏  举报