poj3252——Round Numbers(数位dp)

来源:互联网 发布:java api安卓版 编辑:程序博客网 时间:2024/06/05 10:31

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

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <map>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 10000005#define Mod 10001using namespace std;int dight[40];long long dp[40][40][40];long long dfs(int pos,bool limit,int zero,int one,int first){    if(pos==0)    {        if(first)            return 1;        return zero>=one;    }    if(!limit&&dp[pos][zero][one]!=-1&&!first)        return dp[pos][zero][one];    int end;    long long ret=0;    if(limit)        end=dight[pos];    else        end=1;    for(int d=0; d<=end; ++d)    {        if(first)        {            if(d==1)                ret+=dfs(pos-1,limit&&d==end,0,1,0);            else                ret+=dfs(pos-1,limit&&d==end,0,0,1);        }        else        {            if(d==0)                ret+=dfs(pos-1,limit&&d==end,zero+1,one,0);            else                ret+=dfs(pos-1,limit&&d==end,zero,one+1,0);        }    }    if(!limit&&!first)        dp[pos][zero][one]=ret;    return ret;}long long solve(long long a){    memset(dight,0,sizeof(dight));    int cnt=1;    while(a!=0)    {        dight[cnt++]=a%2;        a/=2;    }    return dfs(cnt-1,1,0,0,1);}int main(){    memset(dp,-1,sizeof(dp));    long long x,y;    while(~scanf("%lld%lld",&x,&y))    {        printf("%lld\n",solve(y)-solve(x-1));    }    return 0;}
0 0
原创粉丝点击