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: [email protected]
 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 }

时间: 2024-11-16 05:38:35

codeforces #320 div 2A - Raising Bacteria (位运算)的相关文章

Codeforces 558C Amr and Chemistry(数论+位运算)

C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n differ

Codeforces 631 D. Dreamoon Likes Sequences 位运算^ 组合数 递推

https://codeforces.com/contest/1330/problem/D 给出d,m, 找到一个a数组,满足以下要求: a数组的长度为n,n≥1; 1≤a1<a2<?<an≤d: 定义一个数组b:b1=a1, ∀i>1,bi=bi−1⊕ai ,并且b1<b2<?<bn−1<bn: 求满足条件的a数组的个数并模m: 人话:求一个a数组满足递增,并且异或前缀和也递增 ,求出a数组个数mod m. 太菜了,不会,看了很多题解才会的,这里总结一下:

CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)

Codeforces 558C 题意:给n个数字,对每个数字可以进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少需要操作多少次. 分析: 计算每个数的二进制公共前缀. 枚举法亦可. /* *Author : Flint_x *Created Time : 2015-07-22 12:33:11 *File name : whust2_L.cpp */ #include<iostream> #include<sstream> #include<fstrea

[Codeforces] Round #320 (Div.2)

1.前言 虽然这次我依旧没有参加正式比赛,但是事后还是看了看题目的...一般不怎么刷Codeforces. A.Raising Bacteria 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

图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

题目传送门 1 /* 2 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 3 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 4 关键在于更新异或和,精髓:a ^ b = c -> a ^ c = b, b ^ c = a; 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorith

Codeforces Round #613 (Div. 2) D - Dr. Evil Underscores(思维,位运算)

?? ?? ?? 题意:对于一个数组,求一个数字与数组每个元素异或之后的最大值最小,求这个最大值 又是位运算,,题目给出数组元素范围在2^30以内,二进制最多30位,从最高位开始贪心,如果此位置的数组元素有的是1有的是0,最后肯定取1,否则取0,还有就是分组讨论,因为每个bit位只能满足原数组中一部分元素异或后为1 #define int ll vector<int>a; int solve(vector<int>v,int bit) { if(bit<=0||v.size(

codeforces 165E - Compatible Numbers 【位运算】

题目:codeforces 165E - Compatible Numbers 题意:给出n个数,然后每个数对应输出一个当前数组中与 Ai 与运算为 0 的数,没有的话输出-1 分析:简单的位运算题目,技巧性题目 首先,我们知道与运算的性质,就是只有同时为 1的时候才是1,那么假如 x&y=0 ,就是至少 x 的为1的为 y 全为0,其他为自由,假设为 1 ,那么 y = x^((1<<22)-1).但是这样并不是全部的,这些位还可能是0,所以我们可以枚举这些位,然后处理. 具体看代码

位运算/CodeForces 485c Bits

1 /* 2 PROBLEM:CF 485C 3 AUTHER:Nicole 4 MEMO:位运算 二进制 5 */ 6 #include<cstdio> 7 int main() 8 { 9 int n; 10 scanf("%d",&n); 11 for (int i=1;i<=n;i++) 12 { 13 long long l,r; 14 scanf("%lld%lld",&l,&r); 15 long long x

codeforces 579A Raising Bacteria

A. Raising Bacteria 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 bact