Codeforces 579A. 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?
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的次方 下面两种方法
解法一:(树状数组)
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define lowbit(x) x&(-x)
int main()
{
int n,t;
while(cin>>n){
t=;
while(n){
t++;
n-=lowbit(n);
}
cout<<t<<endl;
}
return ;
}
解法二:(除以二迭代)
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int n,t;
while(cin>>n){
t=;
while(n){
if(n&) t++;
n/=;
}
cout<<t<<endl;
}
return ;
}
Codeforces 579A. Raising Bacteria的更多相关文章
- CF 579A Raising Bacteria
题意:细菌爱好者在盒子里面培养细菌.最初盒子是空的,每天可以往盒子里面添加任意数目的细菌,而且每天每个细菌会分裂成两个.在某一时刻要想获得某一数量的细菌,要求最初放置的最少的细菌数目. 思路: 求出 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces Dima and Bacteria
题意:给出n,m和k,表示有n个细菌,m种仪器和k种细菌,给出k种细菌的数量ci,然后每个细菌按照种类排成一排(所以有第i种细菌的序号从∑(1≤j≤i-1)cj + 1 到∑(1≤j≤i)cj):接下 ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- [Codeforces] Round #320 (Div.2)
1.前言 虽然这次我依旧没有参加正式比赛,但是事后还是看了看题目的...一般不怎么刷Codeforces. A.Raising Bacteria You are a lover of bacteria ...
- Codeforces__Raising Bacteria
题目传送门:Raising Bacteria //问题描述:一个盒子里面放一个细菌在一天可以增生两个细菌. 现在已知盒子里面细菌的个数,问你最初放多少个细菌可以增生盒子里面的细菌数量 //输入:盒子中 ...
- CodeForces Round #320 Div2
A. Raising Bacteria 计算一下x的bitcount就是答案. #include <iostream> #include <cstdio> #include & ...
- 【解题报告】CF Round #320 (Div. 2)
Raising Bacteria 题意:盒子里面的细菌每天会数量翻倍,你可以在任意一天放任意多的细菌,最后要使得某天盒子里面的细菌数量等于x,求至少要放多少个细菌 思路:显然,翻倍即为二进制左移一位, ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
随机推荐
- pip升级时报错--- No module named 'pip._internal'
一.问题: 之前python3.6是安装的pip版本为:pip=9.0.1,我按照提示升级报错,一直装不上pip18.0,于是直接在site-package目录下删掉了pi ...
- css篇-less,scss 用calc问题
在less或者scss中 calc(100% -4rem) 等带单位混合运算会被less解析忽略单位,全部按照百分比计算,此例中的计算被less编译成calc(96%), 解决办法: width:(& ...
- java native方法及JNI实例 (转)
转自:http://blog.csdn.net/xw13106209/article/details/6989415 1.参考文献: http://blog.csdn.net/youjianbo_ha ...
- 堆(heap)、栈(stack)、方法区(method)
JVM内存分为3个区:堆(heap).栈(stack).方法区(method) 1.堆(heap):存储的全部对象,每个对象有个与之对应的class信息.即通过new关键字和构造器创建的对象.JVM只 ...
- 代码调试--自定义一个简单的debug函数
function debug(){ $num_args = func_num_args(); //实参个数 $arg_list = func_get_args(); //返回某一个实参,必须是实参数组 ...
- canvas原生js写的贪吃蛇
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 实践 : Ubuntu 上 Testlink 部署
1.安装apache sudo apt-get install apache2 2. sudo /etc/init.d/apache2 restart 测试: Http:\localhost or I ...
- selenium元素单击不稳定解决方法
selenium自动化测试过程中,经常会发现某一元素单击,很不稳定,有时候执行了点击没有反映. 以下总结两种解决方法:都是通过js注入的方式去点击. 1.F12查一看,要点击的按钮,或连接,有没有on ...
- Sift中尺度空间、高斯金字塔、差分金字塔(DOG金字塔)、图像金字塔
转自:https://blog.csdn.net/dcrmg/article/details/52561656 一. 图像金字塔 图像金字塔是一种以多分辨率来解释图像的结构,通过对原始图像进行多尺度像 ...
- sqlserver备份还原数据库时报占用错误
.做项目时遇到这种情形:原来的test_dev数据库,想复制出test_ft供测试用.此时备份test_dev出test_backup文件,想直接还原成test_ft时会报占用错误. 还原数据库:Th ...