light oj 1032(数位DP)
求一段区间中,每个十进制数所对应的二进制数中连续的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 ;
}
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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)的更多相关文章
- light oj 1205(数位DP)
题目描述: 求给定区间中的回文数有多少个? 首先明确一点,如果一个数是回文数,那么给这个数两边加上相同的数,那么这个数还是回文数. 根据这点就可以进行递推了,p[start][end]=9*p[sta ...
- light oj 1068 数位dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- Light OJ 1032 - Fast Bit Calculations(数位DP)
题目大意: 一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少? 分解成2进制数字,然后数位DP就行了. ======================== ...
- loj 1032 数位dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...
- Light OJ 1032
数位dp,许多数位dp需要统计某种模式(子串)出现的数量,这种题通常需要在递归参数中加入高位已经出现过的模式的数量. #include <cstdio> #include <cstr ...
- Light OJ 1032 - Fast Bit Calculations(数学)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1032 题目大意:一个十进制数变化为二进制,那么对于这个数,如果连着两个二进制位 ...
- Light oj 1030 概率DP
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
- LightOJ - 1032 数位DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- light oj 1422 区间dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
随机推荐
- 2016 Multi-University Training Contest 1 solutions BY HIT
首先向大家表示抱歉,因为这套题是去年出的,中间间隔时间太长,今年又临时准备仓促, 所以部分题目出现了一些问题,非常抱歉. Abandoned country 首先注意到任意两条边的边权是不一样的,由此 ...
- hdu4115:Eliminate the Conflict
n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...
- php装饰者模式
php装饰者模式 装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 示例: A.B.C编辑同一篇文章. class ...
- for-else和wihle-else组合用法
当for和else组合一起使用的时候,for循环正常执行完毕,会执行else语句,否则,不会执行else语句 for i in range(10): if i == 2: break else: pr ...
- C. Day at the Beach---cf559
http://codeforces.com/problemset/problem/599/C 题目大意: 有n个城堡的高度 让你最多分成几个块 每个块排过序之后 整体是按照升序来的 分析: ...
- Nginx配置upstream实现负载均衡及keepalived实现nginx高可用
(原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...
- Java泛型的主要用途
1.泛型的主要用途就是代替各种类型,作为一个笼统的整体类型代替,也就是代替参数,不论是传入参数还是返回参数.都可以用泛型来代替. 如dao操作类的增删改查操作,因为传入参数的类型不同,但基本都是相同接 ...
- Office Adobe Acrobat把PDF转换为Word时候提示不支持的Type2字体怎么办
如下图所示,在使用Adobe Acrobat Pro9将PDF转换为Word的时候出现下面的错误 很简单,不要用Adobe Acrobat Pro9了,用Adobe Acrobat Pro X,还 ...
- RxJava系列之中的一个 初识Rxjava
1.简单介绍 基础知识 响应式代码的基本组成部分是Observables和Subscribers(事实上Observer才是最小的构建块,但实践中使用最多的是Subscriber.由于Subscrib ...
- Native进程之Trace原理(转)——可直接输出某进程的栈帧——debuggerd
一. 概述 当发生ANR(Application Not Response,对于Java进程可通过kill -3向目标进程发送信号SIGNAL_QUIT, 输出相应的traces信息保存到目录/dat ...