poj3252 Round Numbers

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[x][ze][on]表示取到第x位0个数位ze,1个数位on的后续搜索答案。

这两天唯一一遍过的数位dp,水题太美好了呜呜呜。

下附代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 int dp[40][40][40],cnt[40],l,r;
 6 int dfs(int x,int ze,int on,int jb,int lz){
 7     if (!x){
 8         if (ze>=on) return 1;
 9         else return 0;
10     }
11     if (!jb && !lz && dp[x][ze][on]!=-1) return dp[x][ze][on];
12     int maxx,ret=0;
13     if (jb) maxx=cnt[x];
14     else maxx=1;
15     for (int i=0; i<=maxx; i++){
16         ret+=dfs(x-1,ze+(i==0 && !lz),on+(i==1),jb & (i==maxx),lz&&!i);
17     }
18     if (!jb && !lz) dp[x][ze][on]=ret;
19     return ret;
20 }
21 int main(){
22     memset(dp,-1,sizeof(dp));
23     scanf("%d%d",&l,&r);
24     l--;
25     int len=0;
26     while (l){
27         cnt[++len]=l%2;
28         l/=2;
29     }
30     int res1=dfs(len,0,0,1,1);
31     len=0;
32     while (r){
33         cnt[++len]=r%2;
34         r/=2;
35     }
36     int res2=dfs(len,0,0,1,1);
37     printf("%d",res2-res1);
38     return 0;
39 }
View Code

 

posted @ 2020-10-02 12:09  我是菜狗QAQ  阅读(135)  评论(0编辑  收藏  举报