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> ...
随机推荐
- POJ 1985 Cow Marathon【树的直径】
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...
- 2016 Multi-University Training Contest 2 solutions BY zimpha
Acperience 展开式子, \(\left\| W-\alpha B \right\|^2=\displaystyle\alpha^2\sum_{i=1}^{n}b_i^2-2\alpha\su ...
- SUSE linux使用zypper 安装软件-比yum更好用
1. How to install "wget" command in Linux and how to use multiple : https://www.cyberciti. ...
- Linux下出现launch failed.Binary not found的解决方案
Linux下出现launch failed.Binary not found的解决方案: Project->Properties->C/C++Build->Settings-> ...
- PatentTips - Solid State Memory Wear Leveling
BACKGROUND OF THE INVENTION Solid-state memory devices encompass rewritable non-volatile memory devi ...
- ZOJ 3471 【状态压缩DP】
题意: 有n种化学物质,他们彼此反应会有一种消失并释放出能量. 给出矩阵,第i行j列代表i和j反应j消失释放的能量. 求最大释放多少能量. 思路: 状态压缩DP,我是这么想的. 利用二进制0代表该物质 ...
- 2018 11.2 PION模拟赛
期望:100 + 50 + 30 = 180 实际:0 + 50 + 30 =80 期望:100 实际:0 数值有负数,边界应该设为-0x7f 此处 gg /* 期望的分:50+ */ ...
- 【SQL Server 学习系列】-- ConnectionTimeout、CommandTimeout和BulkCopyTimeout
1. SqlConnection.ConnectionTimeout获取在尝试建立连接时终止尝试并生成错误之前所等待的时间.单位:秒默认值:15秒设置为0时,表示无限制 2. SqlCommand.C ...
- 大型网站技术架构(四)--核心架构要素 开启mac上印象笔记的代码块 大型网站技术架构(三)--架构模式 JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)
大型网站技术架构(四)--核心架构要素 作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载.此篇已收录至<大型网站技 ...
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
C#构造方法(函数) 一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...