A. Raising Bacteria
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
 
input
  1. 5
output
  1. 2
input
  1. 8
output
  1. 1
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.

【题意】:在培养皿中,每个细胞每天会繁殖,数量*2 ,我们可以在任意天加入任意数量的细胞入培养皿中。 想要知道最少加入多少个细胞,可以使得有一天,培养皿中细胞的数量会恰好为x。

【分析】:x的二进制表示中1的个数即为答案.

原因是,每天晚上细胞数量翻倍,相当于左移1位,这时候二进制表示中1的数量不变。也就是说,二进制表示中的所有的1,一定都是添加进去的。而且也只有二进制表示中的1是添加进去的。所以二进制表示中1的数量,就是添加的细胞数。

【代码】:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,m,cnt;
  4.  
  5. int main()
  6. {
  7. while(cin>>n)
  8. {
  9. cnt=; //多组输入内置清零
  10. while(n)
  11. {
  12. if(n%==)
  13. cnt++;
  14. n/=;
  15. }
  16. cout<<cnt<<endl;
  17. }
  18. return ;
  19. }

位运算/二进制

Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】的更多相关文章

  1. Codeforces Round #721 (Div. 2)A. And Then There Were K(位运算,二进制) B1. Palindrome Game (easy version)(博弈论)

    半个月没看cf 手生了很多(手动大哭) Problem - A - Codeforces 题意 给定数字n, 求出最大数字k, 使得  n & (n−1) & (n−2) & ...

  2. Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维

    & -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...

  3. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp

    C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

    B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...

  5. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game

    题目链接:http://codeforces.com/contest/578/problem/B 题目大意:现在有n个数,你可以对其进行k此操作,每次操作可以选择其中的任意一个数对其进行乘以x的操作. ...

  6. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E. Weakness and Poorness 三分

    E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 数学+(前缀 后缀 预处理)

    D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分+连续子序列的和的绝对值的最大值

    E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)

    显然f(x)是个凹函数,三分即可,计算方案的时候dp一下.eps取大了会挂精度,指定循环次数才是正解. #include<bits/stdc++.h> using namespace st ...

随机推荐

  1. Android FrameWork 概述

    Framework是什么 Framework的中文意思是“框架”,在软件开发中通常指开发框架,在一个系统中处于内核层之上,为顶层应用提供接口,被设计用来帮助开发者快速开发顶层应用,而不必关心系统内核运 ...

  2. 【Insert Interval】cpp

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  3. 二 APPIUM Android自动化 测试初体验

    本文转自:http://www.cnblogs.com/sundalian/p/5629358.html 1.创建一个maven项目 成功新建工程: 编辑pom.xml,在<dependenci ...

  4. js 全局变量和局部变量

    Javascript在执行前会对整个脚本文件的声明部分做完整分析(包括局部变量),但是不能对变量定义做提前解析 1.作用域和作用域链 2.变量声明提前

  5. Python导出sql语句结果到Excel

    本文档是因为每周需要统计线上数据库中客户新增资源,手动执行实在是麻烦,就写了个脚本导出到Excel,顺便发一封邮件. (当然这不是线上的真实脚本,不过根据个人需求稍微修改下,还是可以直接用的.拿去不谢 ...

  6. cocos2d安装

    cocos2d引擎要求Python 2 为2.6 以上版本,Python 3 为3.3以上版本 cocos2d引擎安装支持pip安装,安装指令如下: pip install cocos2d

  7. 【转】mysql 计划事件

    转自:http://www.cnblogs.com/c840136/articles/2388512.html MySQL5.1.x版本中引入了一项新特性EVENT,顾名思义就是事件.定时任务机制,在 ...

  8. 【Android】实验7 BindService模拟通信 截止提交日期2016.5.3

    实验7 BindService模拟通信 [目的] 实现启动端和BindService之间的双向通信 [要求] 1)   实现从启动端传递一个数据至BindService端: 2)   实现使用Bind ...

  9. PAT 1070 结绳

    https://pintia.cn/problem-sets/994805260223102976/problems/994805264706813952 给定一段一段的绳子,你需要把它们串成一条绳. ...

  10. C# 在子窗体调用父窗体的值(转)

    1.在父窗体中 public delegate void SetVisiableHandler();//定义委托类型 注意此委托定义在namespace下一级,在form类外 private void ...