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

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

Source

这题的约束就是一个数的二进制中0的数量要不能少于1的数量,通过上一题,这题状态就很简单了,dp[pos][num],到当前数位pos,0的数量减去1的数量不少于num的方案数,一个简单的问题,中间某个pos位上num可能为负数(这不一定是非法的,因为我还没枚举完嘛,只要最终的num>=0才能判合法,中途某个pos就不一定了),这里比较好处理,Hash嘛,(最小就-32吧(好像),直接加上32,把32当0用)。这题主要是要想讲一下lead的用法,显然我要统计0的数量,前导零是有影响的。至于!lead&&!limit才能dp,都是类似的,自己慢慢体会吧。
 
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[33][64]; //dp[i][j]表示i位数二进制0的个数比1的个数不低于的数的个数
int digit[33]; //二进制数的每一位 int dfs(int pos, int sum, int lead, int limit){
// i, j, 是否有前导0, 取值是否受限
//sum即j表示0比1多的个数
if(pos <= 0){
return sum >= 0;
}
if(!lead && !limit && dp[pos][sum] != -1){
return dp[pos][sum];
}
int rt = 0;
int end = limit ? digit[pos] : 1;
for(int i = 0; i <= end; i++){
if(lead && i == 0){ //有前置0并且当前数位位0,则此0不计数,sum不变
rt += dfs(pos - 1, sum, lead, limit && i == end);
}
else{
rt += dfs(pos - 1, sum + (i ? -1 : 1), lead && i == 0, limit && i == end);
}
}
if(!limit && !lead){
dp[pos][sum] = rt;
}
return rt;
} int solve(int x){
int len = 0;
while(x){
digit[++len] = x & 1;
x = x >> 1;
}
return dfs(len, 0, 1, 1);
} int main(){
int n, m;
memset(dp, -1, sizeof(dp)); cin >> n >> m;
cout << solve(m) - solve(n - 1) << endl; return 0;
}

  

4-圆数Round Numbers(数位dp)的更多相关文章

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

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

  2. poj3252 Round Numbers (数位dp)

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

  3. POJ3252 Round Numbers —— 数位DP

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

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

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

  5. poj 3252 Round Numbers 数位dp

    题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...

  6. Round Numbers(数位DP)

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

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

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位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. $POJ$3252 $Round\ Numbers$ 数位$dp$

    正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...

随机推荐

  1. C++17中那些值得关注的特性(上)

    C++17标准在2017上半年已经讨论确定,正在形成ISO标准文档,今年晚些时候会正式发布.本文将介绍最新标准中值得开发者关注的新特新和基本用法. 总的来说C++17相比C++11的新特性来说新特性不 ...

  2. ArrayAdapter的用法

    list = new ArrayList<String>(); //创建一个list list.add("ID列表"); //列表里面的条目 /*ArrayAdapte ...

  3. 使用docker 部署graylog集群

    graylog 相比elk 有比较简单的方面,使用简单,配置简单,可视化工具是一体化的,比较方便 搭建使用docker,多主机部分,结合docker-compose 进行管理 具体docker 配置文 ...

  4. LeetCode OJ - Best Time to Buy and Sell Stock

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...

  5. HTML5 Geolocation用来定位用户的位置。

    HTML5 Geolocation用来定位用户的位置. 定位用户的位置 HTMl5 Geolocation API用来得到用户的地理位置. 由于这个可能和个人隐私相关.除非用户同意否则不能使用. 浏览 ...

  6. Genymotion使用分析

    1.从官网下载Genymotion Genymotion官方下载地址:https://www.genymotion.com/#!/download 没有注册,先进行注册 公司规模选择个人 2.Andr ...

  7. JSONHelp json解析成类,类解析成string

    using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization. ...

  8. 安装memcached服务 和 php 安装memcache扩展

    这是所有的命令,至于哪个命令是干嘛的自己悟去吧  ,顺便穿插一些知识点 安装libevent cd /home/ wget  http://www.monkey.org/~provos/libeven ...

  9. ASP.NET 执行bat文件。

    参考代码: http://blog.csdn.net/youngivan/article/details/7722384 在VS开发环境上是 网站应用程序能够正常执行bat,在winServer服务器 ...

  10. html5播放mp4视频代码

    1.nginx支持flv和mp4格式播放 默认yum安装nginx centos7安装nginx时候应该是默认安装nginx_mod_h264_streaming模块的 # nginx -V查看是否安 ...