Description

The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first.
They can't even flip a coin because it's so hard to toss using hooves.

They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,

otherwise the second cow wins.

A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus,
9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.

Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.

Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).

Input

Line 1: Two space-separated integers, respectively Start and Finish.

Output

Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish

Sample Input

2 12

Sample Output

6

题意:一个数位round数,要满足这个数化成二进制后0的个数大于等于1的个数。

思路:先预处理出dp[i][j]表示前i位有j个0的方案数,然后从高位到低位数位dp就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 805
ll dp[40][40];
void init()
{
int i,j;
memset(dp,0,sizeof(dp));
dp[1][1]=1;dp[1][0]=1;
for(i=1;i<=32;i++){
for(j=0;j<=i;j++){
dp[i+1][j]+=dp[i][j];
dp[i+1][j+1]+=dp[i][j];
}
}
} ll solve(int x)
{
int i,j,t=x;
int wei[40],len=0;
while(t){
wei[++len]=t%2;
t/=2;
} ll sum=0;
int num0=0,num1=0;
for(i=len-1;i>=1;i--){ //这里先把第len位变为0,然后一次枚举最高的位数在第i位
for(j=0;j<=i-1;j++){
if(j>=i-j)sum+=dp[i-1][j];
} } num1=1;
for(i=len-1;i>=1;i--){ //这里是在第len位为1的情况下进行dp
if(wei[i]==1){
if(i==1){
if(num0+1>=num1)sum++;
}
else{
for(j=0;j<=i-1;j++){
if(j+num0+1>=num1+i-1-j ){
sum+=dp[i-1][j];
}
}
}
num1++;
}
else num0++;
}
return sum;
}
int main()
{
int n,m,i,j;
init();
while(scanf("%d%d",&m,&n)!=EOF)
{
printf("%lld\n",solve(n+1)-solve(m));
}
return 0;
}

这题也可以用记忆化搜索做,搜索比直接dp要好些,而且用途广,写的时候在dfs中加一维zero,表示当前这一位是不是任然是前导0.用dp[pos][num0][num1]表示前pos位0的个数为num0,1的个数为num1方案数。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
int dp[35][35][35];
int wei[35];
int dfs(int pos,int num0,int num1,int lim,int zero)
{
int i,j;
if(pos==0){
if(zero==0){ //这里不判断zero==0也可以,判不判断的区别在于是不是把0算上,判断就不把0算上了
if(num0>=num1)return 1;
else return 0;
}
return 0;
/*
if(num0>=num1)return 1;
return 0;
*/
}
if(lim==0 && dp[pos][num0][num1]!=-1){
return dp[pos][num0][num1];
} int ed=lim?wei[pos]:1;
int ans=0,nu0,nu1;
for(i=0;i<=ed;i++){
if(zero && i==0){
nu0=nu1=0;
}
else{
if(i==0){
nu0=num0+1;
nu1=num1;
}
else{
nu0=num0;
nu1=num1+1;
}
}
ans+=dfs(pos-1,nu0,nu1,lim&&i==ed,zero&&i==0 );
}
if(lim==0){
dp[pos][num0][num1]=ans;
}
return ans;
} int solve(int x)
{
int i,j,tot=0;
while(x){
wei[++tot]=x%2;
x/=2;
}
return dfs(tot,0,0,1,1);
} int main()
{
int n,m,i,j;
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&m,&n)!=EOF)
{
printf("%d\n",solve(n)-solve(m-1));
}
return 0;
}

poj3252 Round Numbers (数位dp)的更多相关文章

  1. POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Su ...

  2. poj3252 Round Numbers[数位DP]

    地址 拆成2进制位做dp记搜就行了,带一下前导0,将0和1的个数带到状态里面,每种0和1的个数讨论一下,累加即可. WA记录:line29. #include<iostream> #inc ...

  3. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  4. [poj3252]Round Numbers_数位dp

    Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...

  5. poj 3252 Round Numbers(数位dp 处理前导零)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  6. 4-圆数Round Numbers(数位dp)

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14947   Accepted: 6023 De ...

  7. POJ 3252 Round Numbers(数位dp&amp;记忆化搜索)

    题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...

  8. POJ - 3252 - Round Numbers(数位DP)

    链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...

  9. Round Numbers(数位DP)

    Round Numbers http://poj.org/problem?id=3252 Time Limit: 2000MS   Memory Limit: 65536K Total Submiss ...

随机推荐

  1. js原型链原理

    先附上原型链的图,能看懂的本文就没必要看了,看不懂的可以带着疑问看文章 一.构造函数 什么是构造函数:当一个普通函数创建一个类对象是,那么就程它为构造函数. 特点: 默认首字母大写 使用new关键字来 ...

  2. 肌肤管家SkinRun V3S智能皮肤检测仪,用AI探索肌肤问题

    继肌肤管家SkinRun V3皮肤检测仪之后,肌肤管家SkinRun近期又一重磅推出的肌肤管家SkinRun V3S 智能肌肤测试仪引起了美业人的广泛关注.据了解它汇集百万皮肤数据,利用五光谱原理和人 ...

  3. 聊聊 g0

    很多时候,当我们跟着源码去理解某种事物时,基本上可以认为是以时间顺序展开,这是编年体的逻辑.还有另一种逻辑,纪传体,它以人物为中心编排史事,使得读者更聚焦于某个人物.以一种新的视角,把所有的事情串连起 ...

  4. 安装MySQL数据库(在Windows下通过zip压缩包安装)

    安装MySQL 这里建议大家使用压缩版,安装快,方便.不复杂. 软件下载 mysql5.7 64位下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7 ...

  5. kubernets之从应用访问pod元数据以及其他资源

    一  downwardAPI的应用 1.1  前面我们介绍了如何通过configmap以及secret将配置传入到pod的容器中,但是传递的这些都是预先能够安排和只晓得,对于那些只有当pod创建起来之 ...

  6. 【JAVA并发第三篇】线程间通信

    线程间的通信 JVM在运行时会将自己管理的内存区域,划分为不同的数据区,称为运行时数据区.每个线程都有自己私有的内存空间,如下图示: Java线程按照自己虚拟机栈中的方法代码一步一步的执行下去,在这一 ...

  7. expect的使用

    1. expect概述 1.1 expect的功能 脚本执行时,有时会需要人工进行交互输入,这时可以通过expect工具来实现自动交互. expect是一种shell解释器,但是expect可以在命令 ...

  8. IE浏览器兼容问题总结

    IE浏览器兼容问题总结 引自掘金:https://juejin.cn/post/6844903825854185480 一.标准盒模型和怪异盒模型 浏览器的盒子模型分为两类: 标准的W3C盒子模型. ...

  9. linux中的虚拟环境工具

    1.虚拟环境工具的学习 python的虚拟环境,其实就是在机器上,方便的创建出多个解释器,每个解释器运行一个项目,互相之间不受影响 2.virtualenv工具,可以方便的创建,使用,删除也很方便 3 ...

  10. (003)每日SQL学习:普通视图和物化视图

    关于这一点一直就是很懵懂的状态,今天特意网上查了一下资料,以下摘抄网上比较好的答案.以作记录. 普通视图和物化视图的区别答曰:普通视图和物化视图根本就不是一个东西,说区别都是硬拼到一起的,首先明白基本 ...