Round Numbers

Time Limit: 2000MS   Memory Limit: 65536K
     

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分析:首先肯定是二维dp,分别维护位置和0和1的差值;   其次记忆化前提是小于上限并且出现过1,这样保证后面01都能出现;代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
const int N=5e4+10;
const int M=N*10*10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t,num[31],pos;
ll dp[31][63];
ll dfs(int pos,int val,int ok,int ca)
{
    if(pos<0)return val>=31;
    if(ok&&ca&&dp[pos][val]!=-1)return dp[pos][val];
    int now=ok?1:num[pos],i;
    ll ret=0;
    rep(i,0,now)
    {
        ret+=dfs(pos-1,val+(i==1?-1:(ca?1:0)),ok||i<num[pos],ca||i==1);
    }
    return ok&&ca?dp[pos][val]=ret:ret;
}
ll gao(int p)
{
    pos=0;
    while(p)num[pos++]=p%2,p/=2;
    return dfs(pos-1,31,0,0);
}
int main()
{
    int i,j;
    memset(dp,-1,sizeof(dp));
    while(~scanf("%d%d",&n,&m))
    {
        printf("%lld\n",gao(m)-gao(n-1));
    }
    return 0;
}
时间: 2024-10-27 04:52:59

Round Numbers的相关文章

poj3252 Round Numbers

Round Numbers POJ - 3252 题目大意: 输入两个十进制正整数a和b,求闭区间[a,b]内有多少个Round Number? 所谓的Round Number就是一个把十进制数转换成一个无符号二进制数,若该二进制数中0的个数大于等于1的个数,则它就是一个Round Number. 注意 转换所得的二进制数,最高位必然是1,最高位前不允许有0 /* 以前用的组合数学,听说dalao也用数位dp做,我也来一下 */ #include<iostream> #include<c

POJ 3252 Round Numbers

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12824   Accepted: 4946 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',

BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数(数位DP+恶心细节)

BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 Time Limit: 5 Sec  Memory Limit: 64 MB Description 正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺序.她们甚至也不能通过仍硬币的方式. 所以她们通过"round number"竞赛的方式.第一头牛选取一个整数,小于20亿.第二头牛也这样选取一个整数.如果这两个数都是 "round numbers"

POJ 3252 —— Round Numbers

Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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, S

poj 3252 Round Numbers(数位DP)

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11003   Accepted: 4064 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',

BZOJ1662: [Usaco2006 Nov]Round Numbers

1662: [Usaco2006 Nov]Round Numbers Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 147  Solved: 84[Submit][Status][Discuss] Description 正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如 谁先挤奶的顺序.她们甚至也不能通过仍硬币的方式. 所以她们通过"round number"竞赛的方式.第一头牛选取一个整数,小于20亿. 第二头牛也这样

poj 3252 Round Numbers 【推导&#183;排列组合】

以sample为例子 [2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1] 即:Rn[start,finish]=Rn[0,finish]-Rn[0,start-1] 所以关键是给定一个X,求出Rn[0,X] 现在假设X=10100100  这个X的二进制总共是8位,任何一个小于8位的二进制都小于X 第一部分,求出长度为[0,7]区间内的二进制是RoundNumber的个数  对于一个长度为Len的二进制(最高位为1),如何求出他的Rou

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

题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于一个二进制数的最高位必须为1.所以设置变量first记录前面位是否有1,若有1,则可随意放,否则,仅仅可放1. 同一时候.上面的推断决定了搜索时len的大小与二进制本身的长度不一定相等,所以需两个变量对1和0的个数进行记录. 用dp[a][b][c]保存长度a,b个0,c个1的数字个数.记忆化搜索.

poj3252(Round Numbers)

题目地址:Round Numbers 题目大意: “Round Numbers” 是一个十进制的数转化为二进制,如果在二进制中 “0”的个数不小于“1”的个数,就称为“Round Numbers” .本题求出n到m区间(闭区间)的“Round Numbers” 有多少个. 解题思路: 排列组合的公式:C(n,m)=C(n-1,m-1)+C(n,m-1). (转)   比如:    22 =  10110b  如果要求 <=22的Round Numbers,也就是找出1-22有多少个二进制的0不少