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 ...
随机推荐
- 初学JS——利用JS制作的别踩白块儿(街机模式) 小游戏
这个是上个星期5写的了,当时是突然想写个游戏,就想到了别踩白块儿,当时的想法是 可能普通模式的别踩白块儿因为他的“块儿”是滚动的向上这种,以我目前会的技术想不出怎么写, 但是如果是街机模式,通过你每按 ...
- 为什么要搞vim
一. 先得想清楚折腾vim受的这顿折磨值不值.值.零碎记录几点. 迫使我使用vim的原因如下: (1)之前实习的公司的开发机上只有vim,以后工作的公司也只有vim,同部门的同事大都用vim:如果不用 ...
- Python全栈 MySQL 数据库(SQL命令大全、MySQL 、Python调用)
为了梦想与了信仰 开局一张图 主要三个方面: 1.Linux终端命令 2.MySQL语句 3.Python调用 先删库 再跑路..... ...
- Oracle 插入数据时获取系统时间
insert into table_xx (xx,dateTime) values( 'xx',sysdate) 这个sysdate 相当于sql server中的GETDATE()函数 另to_ch ...
- ironic baremetal rescue process
1.用户调用Nova的rescue函数 nova/virt/ironic/driver.py class IronicDriver(virt_driver.ComputeDriver): ...... ...
- hadoop2.5.2学习及实践笔记(六)—— Hadoop文件系统及其java接口
文件系统概述 org.apache.hadoop.fs.FileSystem是hadoop的抽象文件系统,为不同的数据访问提供了统一的接口,并提供了大量具体文件系统的实现,满足hadoop上各种数据访 ...
- Axios & cookie
Axios & cookie `withCredentials: true https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRe ...
- 简单的FreeBSD 的内核编译
简单的FreeBSD 的内核编译 删除并重新下载内核源码 删除自带的内核源码rm -rf /usr/src 下载内核源码wget https://download.freebsd.org/ftp/re ...
- 架构-UML类图
在UML 2.0的13种图形中,类图是使用频率最高的UML图之一.Martin Fowler在其著作<UML Distilled: A Brief Guide to the Standard O ...
- [codeforces] 527A Playing with Paper
原题 简单的gcd #include<cstdio> #include<algorithm> typedef long long ll; using namespace std ...