求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和。

设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和。

dp[i][1]代表长度为i的二进制数,首位为1,所含有的连续的1的个数之和。

a: d[i][1]=d[i-1][0]+d[i-1][1]+(1<<(i-2));
 b: d[i][0]=d[i-1][0]+d[i-1][1];

这里面有一个需要注意的地方是,假设有一个数字是111,那么它含有2个连续的1,具体体现在

方程上是分两次计算的,一个是a式中的第二项,i位为1,加上长度为i-1的二进制数,首位为1,所含有的连续的1的个数之和,

那么已经算了一次111,另外一个是a式中的第三项,i位和i-1位为1,第i-2位之后任意枚举,这是也计算了一次111.

同理1111也是一样的,初始时d[i][1]=d[i-1][0],一个是a式中的第二项,i位为1,加上长度为i-1的二进制数,首位为1,所含有的连续的1的个数之和,

那么d[i][1]=d[i-1][0]+2,另外一个是a式中的第三项,i位和i-1位为1,第i-2位之后任意枚举,那么d[i][1]=d[i-1][0]+2+1.

d[i][1]=d[i-1][0]+3;所以这里对长度为3以上的连续的1处理是,通过两种方式,叠加处理。

计算时,从高位到低位,按位枚举,如果当前位是1,说明就是上界,i-1位之后可以任意枚举,如果当前位是0,只能当前缀,此时什么都不加。

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
using namespace std;
#define maxn 35
LL d[maxn][maxn];
LL digit[maxn];
LL C[maxn][maxn];
void init()
{
memset(d,,sizeof(d));
d[][]=;
for(int i=;i<maxn;i++)
for(int j=;j<=;j++)
{
d[i][]=d[i-][]+d[i-][]+(<<(i-));
d[i][]=d[i-][]+d[i-][];
} }
LL solve(LL x)
{
LL len=,xx=x;
while(x>)
{
digit[++len]=x%;
x>>=;
}
digit[len+]=; //初始化前缀为0,0是没有任何影响的,后面一位可能会用到前面一位
LL ans=;
int flag=;
for(int i=len;i>=;i--) //当前位是1,就是上界,i-1位之后可以任意枚举,否则只能当前缀。
{
if(digit[i]==) //如果当前位是1,i-1位可以任意枚举,如果当前位是0,那么说明是上界
ans+=d[i-][]+d[i-][];
if(flag)
{
if(digit[i]==) //只有前缀不是上界的时候,才可以任意枚举
ans+=flag * ( << (i-)); //i位填0
}
if(flag && (digit[i+]== && digit[i]==))
{
flag++;
}
else if(!flag && digit[i+]== && digit[i]==)
{
flag=;
}
}
return ans;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int t,Case=;
scanf("%d",&t);
init();
LL n=;
while(t--)
{
scanf("%lld",&n);
printf("Case %d: %lld\n",++Case,solve(n+));
}
return ;
}
B - Fast Bit Calculations

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Appoint description:
 

Description

A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true" or "false" respectively). And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is 1 and its next bit is also 1 then we can say that the number has a 1 adjacent bit. And you have to find out how many times this scenario occurs for all numbers up to N.

Examples:

      Number         Binary          Adjacent Bits

12                    1100                        1

15                    1111                        3

27                    11011                      2

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer N (0 ≤ N < 231).

Output

For each test case, print the case number and the summation of all adjacent bits from 0 to N.

Sample Input

7

0

6

15

20

21

22

2147483647

Sample Output

Case 1: 0

Case 2: 2

Case 3: 12

Case 4: 13

Case 5: 13

Case 6: 14

Case 7: 16106127360

light oj 1032(数位DP)的更多相关文章

  1. light oj 1205(数位DP)

    题目描述: 求给定区间中的回文数有多少个? 首先明确一点,如果一个数是回文数,那么给这个数两边加上相同的数,那么这个数还是回文数. 根据这点就可以进行递推了,p[start][end]=9*p[sta ...

  2. light oj 1068 数位dp

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  3. Light OJ 1032 - Fast Bit Calculations(数位DP)

    题目大意: 一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少?   分解成2进制数字,然后数位DP就行了.   ======================== ...

  4. loj 1032 数位dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...

  5. Light OJ 1032

    数位dp,许多数位dp需要统计某种模式(子串)出现的数量,这种题通常需要在递归参数中加入高位已经出现过的模式的数量. #include <cstdio> #include <cstr ...

  6. Light OJ 1032 - Fast Bit Calculations(数学)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1032 题目大意:一个十进制数变化为二进制,那么对于这个数,如果连着两个二进制位 ...

  7. Light oj 1030 概率DP

    D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768 ...

  8. LightOJ - 1032 数位DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  9. light oj 1422 区间dp

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

随机推荐

  1. Eclipse安装插件长时间停留在calculating requirements and dependencies

    如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示" ...

  2. 积累js里有用的函数库

    一.兼容地获取非行间样式(兼容火狐,ie,chrome) function getStyle(obj,name) { if(obj.currentStyle){ return obj.currentS ...

  3. TimePickerDialog

    package com.pingyijinren.helloworld.activity; import android.app.TimePickerDialog; import android.su ...

  4. 2017-10-04-afternoon

    注意完全平方数统计时的特判 #include <cstdio> inline void read(int &x) { x=; register char ch=getchar(); ...

  5. 洛谷——P1038 神经网络

    P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...

  6. [Bzoj1030][JSOI2007]文本生成器(AC自动机)(dp)

    1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5254  Solved: 2172[Submit][Stat ...

  7. 开头第一篇Hello World

    以前在折腾个人博客的时候,使用过的WordpPress.Z-Blog.Typecho建站程序,开头第一篇都是Hello World,作为程序员的社区,开头第一篇当然也要是Hello World! 一句 ...

  8. 临远大神,你为啥要建立一个 TASK表。HumanTaskDTO

    临远大神,你为啥要建立一个 TASK表.HumanTaskDTO HumanTask这张表的作用是什么. 为了实现理想中的任务中心.TaskCenter. 首先,工作流可能会完全不包含任何人工节点,全 ...

  9. Delphi中匿名方法动态绑定事件

    应恢弘之约,写了一个对其发布的匿名函数动态绑定到事件的封装,代码如下: type TAnonEvent=class public class function Wrap<T1,T2>(On ...

  10. 关闭Windows 2003/2008中IE增强的安全配置的方法

           在使用Windows Server 2003/2008操作系统时,打开IE浏览网页时,发现浏览器总提示 "是否需要将当前访问的网站添加到自己信任的站点中去",要是不信 ...