码迷,mamicode.com
首页 > 其他好文 > 详细

Lowbit Sum 规律

时间:2014-07-23 22:22:27      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

Lowbit Sum

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus

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

1
2
3

Sample Output

1
3
4


bubuko.com,布布扣
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <algorithm>
 6 using namespace std;
 7 #define ll long long
 8 ll solve(int x)
 9 {
10     if(x==0)return 0;
11     return ((x+1)>>1)+(solve(x/2)<<1);
12 }
13 int main()
14 {
15     int n;
16     while(~scanf("%d",&n))
17     {
18         printf("%lld\n",solve(n));
19     }
20 }
View Code

 

Lowbit Sum 规律,布布扣,bubuko.com

Lowbit Sum 规律

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/ERKE/p/3863953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!