Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
1 second
256 megabytes
standard input
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?
The only line containing one integer x (1 ≤ x ≤ 109).
The only line containing one integer: the answer.
5
2
8
1
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的数量,就是添加的细胞数。
【代码】:
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt; int main()
{
while(cin>>n)
{
cnt=; //多组输入内置清零
while(n)
{
if(n%==)
cnt++;
n/=;
}
cout<<cnt<<endl;
}
return ;
}
位运算/二进制
Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】的更多相关文章
- 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) & ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game
题目链接:http://codeforces.com/contest/578/problem/B 题目大意:现在有n个数,你可以对其进行k此操作,每次操作可以选择其中的任意一个数对其进行乘以x的操作. ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness (三分)
显然f(x)是个凹函数,三分即可,计算方案的时候dp一下.eps取大了会挂精度,指定循环次数才是正解. #include<bits/stdc++.h> using namespace st ...
随机推荐
- Ubuntu设置root密码[repost]
From: http://hi.baidu.com/busybox/item/283e7d31433db7179cc65ef3 安装完Ubuntu后在终端使用命令:su -然后输入密码,总是不正确.原 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目3
2014-04-28 22:18 题目:计算N的阶乘尾巴上有多少个零? 解法:计算5的个数即可,因为2 * 5 = 10,2的个数肯定比5多.计算5的个数可以在对数时间内搞定. 代码: // 17.3 ...
- .Net导出Word和Excel
using System; using System.Collections.Generic; using System.Linq; using System.Web; public class Ex ...
- CSS UNIT 详解以及最佳实践
分类 ■ 绝对长度(Absolute units):cm,mm,in,pt,pc 绝对单位之间的换算:1in = 2.54cm=25.4mm=72pt=6pc 绝对长度在css中的表现和其他地方 ...
- 01--是时候让我们谈谈一致性hash了
--------------------- 假如你有图中三个盒子,我们有代号为 1,4,5,12 这四样东西 那根据代号作为主键,将东西放到盒子了,该如何放置? 我们可以对代号取模 1 mod 3 = ...
- 孤荷凌寒自学python第七天 列表的复制与序列的基本运算
孤荷凌寒自学python第七天 列表的复制与序列的基本运算 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (同步语音:https://www.ximalaya.com/keji/191030 ...
- PHP页面跳转总结
一.使用php内置函数:header()函数 <?php$url='./test.php'; header("Location:$url"); ?> 注意Locatio ...
- vue 实例 网站
Pure vue demo 实战第一节:Vue基础一 Pure vue demo 实战第二节:Vue基础二 Pure vue demo 实战第三节:Vue组件 Pure vue demo 实战第四节: ...
- sharePreference的几个重点
一. SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中,文件存放在/data/data/&l ...
- 初识MVC框架
MVC框架 是一个框架模式,它使应用程序的输入.处理和输出分开.它可以使业务逻辑.数据.界面显示分离,使得耦合度大大降低,在这一方面与"三层"思想类似. M--Model ...