Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7625   Accepted: 2625

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
如果一个数二进制数中的,0比1多或相等,就是目标数,求一定范围的个数!数位dp,可以看出来,数位dp,我们用dp[i][j][k][a]表是第i位已经有j个1,总的位数是k,是否已经出现过0,这样,就可以轻易的用dp,做出来了!
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define M 40
int pri[M],dp[M][40][40][2];
int get(int i,int one,int bit){
if(one==0&&i==0)return bit-1;
return bit;
}
int dfs(int pos,int bit,int t,int flag,int one){
if(pos==0)return t<=bit/2;
if(!flag&&dp[pos][t][bit][one]!=-1)return dp[pos][t][bit][one];
int u=flag?pri[pos]:1,ans=0;
for(int i=0;i<=u;i++)
ans+=dfs(pos-1,get(i,one,bit),t+i,flag&&i==u,one||i);
return flag?ans:dp[pos][t][bit][one]=ans;
}
int solve(int x){
int cnt=0;
while(x){
pri[++cnt]=x%2;x/=2;
}
return dfs(cnt,cnt,0,1,0);
}
int main()
{
int n,m;
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&n,&m)!=EOF){
//for(m=100;m>=0;m--)
printf("%d\n",solve(m)-solve(n-1));
}
return 0;
}
												

poj3252 Round Numbers的更多相关文章

  1. [BZOJ1662][POJ3252]Round Numbers

    [POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...

  2. POJ3252 Round Numbers —— 数位DP

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

  3. poj3252 Round Numbers(数位dp)

    题目传送门 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16439   Accepted: 6 ...

  4. poj3252 Round Numbers (数位dp)

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

  5. POJ3252 Round Numbers 【数位dp】

    题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...

  6. POJ3252 Round Numbers(不重复全排列)

    题目问区间有多少个数字的二进制0的个数大于等于1的个数. 用数学方法求出0到n区间的合法个数,然后用类似数位DP的统计思想. 我大概是这么求的,确定前缀的0和1,然后后面就是若干个0和若干个1的不重复 ...

  7. poj3252 Round Numbers[数位DP]

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

  8. POJ3252 Round Numbers 题解 数位DP

    题目大意: 求区间 \([x,y]\) 范围内有多少数的二进制表示中的'0'的个数 \(\ge\) '1'的个数. 解题思路: 使用 数位DP 解决这个问题. 我们设状态 f[pos][num0][n ...

  9. 题解【POJ3252】Round Numbers

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

随机推荐

  1. 学渣也要搞 laravel(3)—— HTTP控制器

    1. laravel 控制器在app/Http/Controllers/ 下,你会看到里面有一个Controller.php ,之后我们创建的控制器都是继承这个总控制器的.创建控制器只需要在这里面添加 ...

  2. Ajax请求ashx一般处理程序实现文件下载

    具体功能为,在文件数据列表中选择一行,点击表格上方的下载按钮,下载文件.由于表格中不包含文件路径,只能取到在数据库表中的ID,所以具体实现就是这样:首先点击一行,获取点击的一行数据的ID,用Ajax传 ...

  3. git推送失败的问题

    git报错如下: fatal: 'origen' does not appear to be a git repositoryfatal: The remote end hung up unexpec ...

  4. Python核心编程2第三章课后练习

    1. 标识符.为什么Python 中不需要变量名和变量类型声明? Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程.每个变量在内存中创建,都包括变量的标识,名称和数据这些信息.每 ...

  5. iOS: 学习笔记实例, 用代码控制视图创建与切换

    1. 创建iOS, Single View Application.2. 修改YYViewController.m // // YYViewController.m // DynamicViewDem ...

  6. Junit4拓展工具JCategory与Testng的Group功能比较

    前言 笔者前段时间分享过一遍文章,关于如何通过引入新注解来扩展Junit4,以解决Process上的问题: JUnit扩展:引入新注解Annotation 最近在跟外面的同事聊的时候,得知Testng ...

  7. 从UI Automation看Windows平台自动化测试原理

    前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...

  8. jquery平滑滚动到锚点 一行代码

    $("html,body").animate({scrollTop: $("#box").offset().top}, 1000); $("#box& ...

  9. Nodejs and json

    http://cnodejs.org/topic/51bbe16960af11cd33304b75 http://www.cnblogs.com/nano/archive/2012/05/09/249 ...

  10. java多线程 ReentrantLock

    本章对ReentrantLock包进行基本介绍,这一章主要对ReentrantLock进行概括性的介绍,内容包括:ReentrantLock介绍ReentrantLock函数列表ReentrantLo ...