Round Numbers

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
",gao(m)-gao(n-1));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dyzll/p/6389369.html