先贴代码,以后再写题解。。。

首先,直接枚举肯定是会超时的,毕竟n就有10^9那么多。。。

对于每个数,我们先把它转化为二进制;例:21-->10101;

对于00001~10101,可以分为几个部分:

00001~10000;

10001~10100;

10101

因为对于每个数,从最右边的1截断,于是就可以理解为为:

00001~10000:;

001~100;

1;

设s[i]为二进制从右边数第 i+1 个数为1 (且其他数都为0)的lowbit sum;

则 s[i]=s[i-1]*2+2^i-2^(i-1);

则lowbit sum(21)=s[0]+s[2]+s[4];

设i=4;即 10000;

可分为 :

00001~01000   s[i-1];

01001~01111   s[i-1]-lowbit(01000)=s[i-1]-2^(i-1);  (从最右边的1截断,即可理解为 00001~00111 )

10000       lowbit(10000)=2^i;

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long ll; ll s[]; ll init (int n){
if (s[n])
return s[n];
s[n]=init (n-)*-(<<(n-))+(<<n);//cout<<s[n]<<" ";
return s[n];
} int main (){
int n; //while (cin>>n&&n) cout<<(1024>>n)<<endl;
ll ans=;
int temp=;
memset (s,,sizeof s);
s[]=;
init (); //for (int i=0;i<=30;i++) cout<<s[i]<<" ";
while (~scanf ("%d",&n)){
ans=;
for (int i=;n&&i<;i++) {
if (n&)
ans+=s[i];//cout<<ans<<endl;
n>>=;
//n/=2;
}
printf ("%lld\n",ans);
}
return ;
}

acdream 1154 Lowbit Sum的更多相关文章

  1. ACdream 1154 Lowbit Sum (数位DP)

    Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...

  2. ACdreamOJ 1154 Lowbit Sum (数字dp)

    ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...

  3. ACDream - Lowbit Sum

    先上题目: C - Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...

  4. Lowbit Sum 规律

    Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...

  5. acdream 1148 GCD SUM 莫比乌斯反演 ansx,ansy

    GCD SUM Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatis ...

  6. 【noip模拟】2048

     Time limit: 1000ms         Memory limits: 256MB Description 2048曾经是一款风靡全球的小游戏.今天,我们换一种方式来玩这个小游戏.现在, ...

  7. BZOJ2064:分裂——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2064 Description 背景: 和久必分,分久必和... 题目描述: 中国历史上上分分和和次数 ...

  8. acdream Divide Sum

    Divide Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...

  9. ACdream: Sum

    Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticN ...

随机推荐

  1. Leetcode 104 Maximum Depth of Binary Tree python

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  2. 垃圾回收GC——JVM之七

    垃圾回收是个复杂的过程: 请以此阅读下列文章: 垃圾回收1:http://blog.csdn.net/sun305355024sun/article/details/41394729 垃圾回收2:ht ...

  3. Linux下安装Perl和Perl的DBI模块

    今天在虚拟机测试shell脚本的时候,有些命令使用不了. 比如说 mysqlhotcopy ,它提示Perl的版本太低. 我用的 RedHat9 的Perl才5.8.0版本...(2002年以前的) ...

  4. 用JSON 和 Google 实现全文翻译

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. haproxy nginx 多路径

    nginx 多路径: location / { root /t/deploy/zjdev/deployedApps/zjzc-web-frontEnd/; index index.html index ...

  6. Eclipse总是自动关闭

    -Dosgi.requiredJavaVersion=1.5把INI文件中的这一行删除掉,貌似这样以后模拟器也没再出现timeout的问题了,O(∩_∩)O哈哈~

  7. 理解*ptr++

    这是C语言中指针的基本用法之一,我们先来看一个小例子.下面是代码: int main(void) { char *p = "Hello"; while(*p++) printf(& ...

  8. python爬虫系列之爬京东手机数据

    python抓京东手机数据 作者:vpoet mail:vpoet_sir@163.com #coding=utf-8 import urllib2 from lxml import etree im ...

  9. 【CF 676B Pyramid of Glasses】模拟,递归

    题目链接:http://codeforces.com/problemset/problem/676/B 题意:一个n层的平面酒杯金字塔,如图,每个杯子的容量相同.现在往最顶部的一个杯子倒 t 杯酒,求 ...

  10. Yii2 框架下bootstrap 弹窗预览视频等~

    Yii2 本身已经引用了'yii\bootstrap\BootstrapAsset',所以使用bootstrap 非常简洁. 1 在PHP页面引用命名空间 use app\assets\AppAsse ...