A-raising bacteriaTime
limit:1000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u SubmitStatus
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 would split into the bacteria. You hope to see exactly x bacteria in the box at some moment.
What's the minimum number of bacteria you need-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 "The first day Morning" and at the third morning there would be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so that the answer is 2.
For the second sample, we can put a one in the first morning and in the 4-th morning there would be a 8 in the box. So the answer is 1.a
The number of 1 in the binary representation of x is the answer.
The reason is that the number of candies doubled every night, equivalent to moving left 1 bits, when the binary representation of the number of 1 is unchanged
In other words, all 1 in the binary representation must be added.
And only 1 of binary representations are added.
So the number of binary representations in 1 is the number of candies added.
1 /*************************************************************************2 > File name:code/cf/#320/a.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 September 18 Friday 23:18 34 seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> + #defineY1 HUST111QQZ A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - #defineLR DYING111QQZ - using namespacestd; - #definefor (i, n) for (int i=0;i<int (n); ++i) -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to intMain () + { - #ifndef Online_judge the * #endif $ intx;Panax NotoginsengCin>>x; - intAns =0 ; the while(x) + { A if(%2==1) ans++; thex = x/2; + } -cout<<ans<<Endl; $ $ #ifndef Online_judge - fclose (stdin); - #endif the return 0; -}
View Code
Codeforces #320 Div 2a-raising bacteria (bitwise operation)