CSU-ACM2017暑假集训比赛7 B
来源:互联网 发布:西安行知中学中考喜报 编辑:程序博客网 时间:2024/06/05 16:27
B - 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 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.
Example
Input
5Output2Input8Output1
Note
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.
为使放入盒中的细菌数量最少,第一天肯定只放一个,并且之后加入时每次也只加入一个,充分利用细菌的增殖来增加细菌总数。又由于细菌数量在每天晚上倍增,而放入细菌的时间是某天早上,所以某天早上的细菌总数(放入新的细菌前)有以下两种情况:
考虑某些天早上不放细菌,某些天早上放细菌,使得某天早上细菌数恰好是目标数目,或者再放入一个即达到目标数目,假定目标为
总结这两个情况的表达式:
可以看出,为达到目标所需放入的最少细菌数目就是这个目标的二进制数中
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>#include <queue>using namespace std;int x;int check(int tar){ int ret = 0; while(tar){ if(tar & 1) ret++; tar >>= 1; } return ret;}int main() {#ifdef TEST freopen("test.txt", "r", stdin);#endif // TEST while(cin >> x){ cout << check(x) << endl; } return 0;}
阅读全文
0 0
- CSU-ACM2017暑假集训比赛7 B
- CSU-ACM2017暑假集训比赛1 B
- CSU-ACM2017暑假集训比赛2 B
- CSU-ACM2017暑假集训比赛7 A
- CSU-ACM2017暑假集训比赛7 C
- CSU-ACM2017暑假集训比赛7
- CSU-ACM2017暑假集训比赛7
- CSU-ACM2017暑假集训比赛7
- CSU-ACM2017暑假集训比赛1 C
- CSU-ACM2017暑假集训比赛1 A
- CSU-ACM2017暑假集训比赛1 C
- CSU-ACM2017暑假集训比赛1 C
- CSU-ACM2017暑假集训比赛2 C
- CSU-ACM2017暑假集训比赛2 D
- CSU-ACM2017暑假集训比赛2 A
- CSU-ACM2017暑假集训比赛2 E
- CSU-ACM2017暑假集训比赛2 HDU
- CSU-ACM2017暑假集训比赛2 CodeForces
- 关于Tensorflow 训练时越来越慢,重启又好了
- caffeOnSpark遇到的问题,没有解决,Caffe.java是从哪儿出来的?请教下大家
- 9.IDA-重新设置函数类型、创建数组结构
- 项目绩效管理这样做有效
- js 获取当天的时间字符串 (日期部分的字符串)
- CSU-ACM2017暑假集训比赛7 B
- 搭建zookeeper集群
- 无外网安装jumpserver v0.3.2
- 手机抓包(使用Fidders移动设备真机抓包)
- Ubuntu 10.04修改Gedit配色方案
- 键盘对应数字-keycode值大全
- android 通过adb命令控制wifi开关
- 怎样变成一个内心强大的人
- 字典树