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> ...
随机推荐
- Codeforces396A - On Number of Decompositions into Multipliers
Portal Description 给出\(n(n\leq500)\)个\([1,10^9]\)的数,令\(m=\prod_{i=1}^n a_i\).求有多少个有序排列\(\{a_n\}\),使得 ...
- ubuntu samba 配置简介
Ubuntu 11.04下虚拟机Samba的共享配置详细步骤 一. Ubuntu 11.04下Samba的安装: $ sudo apt-get insall samba ...
- django学习之- 信号
- Django内置的信号Model signals pre_init # django的modal执行其构造方法前,自动触发 post_init # django的modal执行其构造方法后,自动触 ...
- python学习之-- 面向对象
面向对象(简写:OOP) 面向对象编程定义:利用类和对象来创建各种模型,来实现对真实世界的描述. 优点:使程序更容易理解和维护以及扩展代码. 类定义:用来描述具有相同的属性和方法的对象的集合.(简单讲 ...
- Match the string--hdu1797(模拟)
http://acm.hdu.edu.cn/showproblem.php?pid=1797 就是模拟 我的思路是标记aba 和h的位置 然后就判断是否正确 就行了 还有就是 最后 fkfkfkf ...
- 洛谷——P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows
https://www.luogu.org/problem/show?pid=2916 题目描述 Farmer John has grown so lazy that he no longer wan ...
- Distinct Subsequences (dp)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 11-Js类和对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- DELPHI跨平台的临界替代者
在WINDOWS里面使用临界来保护多线程需要访问的共享对象,现在,DELPHI有了新的跨平台临界保护者--System.TMonitor 代码演示如下: FConnections := TObject ...
- linux是类unix操作系统
linux是类unix操作系统,linux与unix使用的基础命令是一样的,没有区别.Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程 ...