ACDream - Lowbit Sum

先上题目:

C - 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

  规律题,如果熟悉树状数组的话说不定可以直接得到答案,如果不熟悉的话可以打表看一下,可以发现前几项lowbit大概是1,2,1,4,1,2,1,8,······发现奇数项都是1,这是lowbit运算导致的,然后偶数项提出来,如果都除以2,发现就会变成1,2,1,4,···然后就有感觉是迭代了,最终得到公式  ans(x)=2*ans(x/2)+n/2+(n&1)

上代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #define lowbit(x) (x & (-x))
 6 #define MAX 100002
 7 #define LL long long
 8 using namespace std;
 9
10
11 LL solve(int n){
12     if(n==1) return 1;
13     LL ans =  2*solve(n>>1) + n/2;
14     if(n&1) ans++;
15     return ans;
16 }
17
18 int main()
19 {
20     int n;
21     while(scanf("%d",&n)!=EOF){
22         printf("%lld\n",solve(n));
23     }
24     return 0;
25 }

Lowbit Sum

ACDream - Lowbit Sum

时间: 2024-10-11 01:46:27

ACDream - Lowbit Sum的相关文章

acdream 1154 Lowbit Sum

先贴代码,以后再写题解... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 7 typedef long long ll; 8 9 ll s[100]; 10 11 ll init (int n){ 12 if (s[n]) 13 return s[n]; 14 s[n]=init (

ACdream 1154 Lowbit Sum (数位DP)

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进制数 比

ACDream - Divide Sum

先上题目: Divide 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 ++)    for(int j = 1; j <= n; j ++)        ans += a[i] / a[j];给出n,a[1]...a[

Lowbit Sum 规律

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进制数比

ACDream - Power Sum

先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出n,m,p,求 (1^m + 2^m + 3^m + 4^m + ... + n^m) % p Input 第一行一个数T( <= 10),表示数据总数 然后每行给出3个数n,m,p(1 <= n <= m <= 10

ACdream: Sum

Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description You are given an N*N digit matrix and you can get several horizontal or vertical digit strings from any position. For

ACdreamOJ 1154 Lowbit Sum (数位dp)

ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:ACdreamOJ 1154 题意: long long ans = 0; for(int i = 1; i <= n; i ++) ans += lowbit(i) lowbit(i)的意思是将i转化成二进制数之后,只保留最低位的1及其后面的0,截断前面的内容,然后再转成10进制数.即lowbit(i) = i&(-i). 每输入一个n,求ans 分析: 用二进制去考虑,可以发现这是个数位dp,如果当前第i

ACdream 1431 Sum vs Product

题目链接:http://115.28.76.232/problem?pid=1431 Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description Peter has just learned mathematics. He learned how to add, and h

[补档]noip2019集训测试赛(八)

Problem B: 2048 Special Judge Time Limit: 1000 ms Memory Limit: 256 MB Description 2048曾经是一款风靡全球的小游戏. 今天,我们换一种方式来玩这个小游戏. 现在,你有一个双端队列,你只能把元素从左端或从右端放入双端队列中.一旦放入就不得取出.放入后,若队列中有连续两个相同的元素,它们将自动合并变成一个新的元素--原来那两个元素的和.若新的元素与它相邻的元素相同,则继续合并-- 如:双端队列中有2, 4, 16三