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. 高性能优化规范建议Mysql

    Mysql高性能优化规范建议 https://www.cnblogs.com/huchong/p/10219318.html 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据 ...

  2. ECUST 12级 Practise

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26399#overview 果断开小号水过 CodeForces 58A #include ...

  3. udev学习笔记汇总

    1.什么是udev udev--就是动态设备管理 udev 能够处理设备事件.管理设备文件的权限.在/dev目录中创建额外的符号链接.重命名网络接口,等等. 内核通常仅根据设备被发现的先后顺序给设备文 ...

  4. table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)

    table中的tr的默认display:table-row,虽然可以修改为display:block但是就失去了tr特有的显示效果,如(td自动对齐): 并且在tr中对起设定padding是有用的,可 ...

  5. SonarQube使用教程

    SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,本文将会介绍一下这个工具的安装.配置以及使用. 一.SonarQube的安装使用: 下载地址:http://ww ...

  6. Chrome 上传时打开对话框非常慢

    Chrome 上传时打开对话框非常慢 先说解决方法,将 Chrome 中这个选项关闭,打开会飞快. 如果只是图片之类是不会的,但是有 zip apk 之类的就会慢. 主要原因还是 Chrome 太安全 ...

  7. curl查询公网出口IP

    liuzhizhi@lzz-rmbp|logs # curl ipinfo.io { "ip": "114.110.1.38", "hostname& ...

  8. Linux SSH远程文件/目录 传输

    Linux SSH远程文件/目录传输命令scp 2010年08月6日 上午 | 作者:VPS侦探 相信各位VPSer在使用VPS时会经常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已 ...

  9. ASI接口

    Asynchronous Serial Interface ,异步串行接口,用于传送码流的一个标准DVB接口. 在目前的DVB-C系统设备的传输接口有两种MPEG2视频码流传输接口标准:异步串行接口A ...

  10. Spark在Windows下的环境搭建

    本文转载自:http://blog.csdn.net/u011513853/article/details/52865076 由于Spark是用Scala来写的,所以Spark对Scala肯定是原生态 ...