Lowbit Sum
来源:互联网 发布:知乎 椅子推荐 编辑:程序博客网 时间:2024/05/20 02:51
Problem Description
long long ans = 0;
for(int i = 1; i <= n; i ++)
ans += lowbit(i)
lowbit(i)的意思是将i转化成二进制数之后,只保留最低位的1及其后面的0,截断前面的内容,然后再转成10进制数
比如lowbit(7),7的二进制位是111,lowbit(7) = 1
6 = 110(2),lowbit(6) = 2,同理lowbit(4) = 4,lowbit(12) = 4,lowbit(2) = 2,lowbit(8) = 8
每输入一个n,求ans
Input
多组数据,每组数据一个n(1 <= n <= 10^9)
Output
每组数据输出一行,对应的ans
Sample Input
123
Sample Output
134
/** this code is made by QuQ* Problem: 1154* Verdict: Accepted* Submission Date: 2014-07-31 11:59:02* Time: 188MS* Memory: 1676KB*/#include <iostream>#include <cmath> using namespace std; typedef long long LL;LL n, ans; int main(){ while (cin >> n) { ans = 0; if (n % 2 == 0) ans += n / 2; else ans += (n / 2 + 1); n /= 2; int k = 2, cnt = 0; while (n) { cnt = (int)ceil(double(n) / 2); ans += k * cnt; k *= 2; n /= 2; } cout << ans << endl; } return 0;}
0 0
- Lowbit Sum
- Lowbit Sum
- ACdream 1154 Lowbit Sum
- ACdream OJ 1154 Lowbit Sum
- lowbit
- lowbit
- lowbit
- lowbit
- lowbit
- lowbit
- ACdream 1154 Lowbit Sum(数学:推理)
- ACdreamOJ 1154 Lowbit Sum (数位dp)
- 数位DP ACdream 1154 Lowbit Sum
- ACdream 1154 Lowbit Sum (数位DP)
- ACdream 1154 Lowbit Sum(数位DP)
- ACdream 1154 Lowbit Sum (数位dp)
- 关于lowbit
- 【lowbit(x)】
- oracle 表锁死的解决方法
- Scale的放大与缩小
- AFNetworking、ASIHTTPRequest中SSL的使用
- ubuntu 卸载apache2
- hello kitty tyvj begin 不能再水了 字符串输入
- Lowbit Sum
- OpenCV使用VideoInput库从摄像头捕获视频
- Dijkstra算法 ---java实现
- php $_SERVER详细介绍
- opencv各种内存泄露情况的大总结
- 大牛初步之Oracle服务端安装完后导致的开机启动慢的问题
- 关于STM32的存储器
- ios中的设计模式之class Clusters
- Java提高篇(三三)-----Map总结