poj3252 Round Numbers

Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14628   Accepted: 5878

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

题目大意:求区间[a,b]中的数转化为二进制后0比1多的数的个数.
分析:典型的数位dp题,先在二进制上做dp,最后转化到十进制上.求出[0,b]和[0,a-1]的答案,相减就可以了.
      一个坑点:二进制数必须要存在,也就是说必须要有一个1开头.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

ll a, b, num[40], cnt, f[100][100][100];

ll dfs(ll len, ll num1, ll num0, bool limit,bool can)
{
    if (len == 0)
    {
        if (num0 >= num1)
            return 1;
        return 0;
    }
    if (!limit && f[len][num1][num0])
        return f[len][num1][num0];
    ll cntt = 0, shangxian = (limit ? num[len] : 1);
        for (int i = 0; i <= shangxian; i++)
        {
            if (i == 0)
            {
                if (can)
                    cntt += dfs(len - 1, num1, num0 + 1, (limit && i == shangxian), can || i == 1);
                else
                    cntt += dfs(len - 1, num1, num0, (limit && i == shangxian), can || i == 1);
            }
            if (i == 1)
                cntt += dfs(len - 1, num1 + 1, num0, (limit && i == shangxian),can || i == 1);
        }
    if (!limit)
        return f[len][num1][num0] = cntt;
    else
        return cntt;
}

ll solve(ll x)
{
    memset(num, 0, sizeof(num));
    cnt = 0;
    while (x)
    {
        num[++cnt] = x % 2;
        x /= 2;
    }
    return dfs(cnt, 0, 0, true,false); 
}

int main()
{
    scanf("%lld%lld", &a, &b);
    printf("%lld\n", solve(b) - solve(a - 1));
return 0; }

 

posted @ 2017-11-25 09:13  zbtrs  阅读(324)  评论(0编辑  收藏  举报