Poj 3252 Round Numbers

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

Answer

题意,

如果某个数字化为二进制,0的数量比1的多,叫做Round Numbers

找[l,r]之间的所有Round Numbers

讲解看代码:

#include 
   
   
    
    
#include 
    
    
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #define maxn (35 + 20) #define inf 0x3f3f3f3f #define pi acos(-1.0) using namespace std; typedef long long int LLI; int dp[2][maxn][maxn];//dp[x][i][j]代表最高位是x,一共i位,其中0有j个的情况有多少种 int bit[maxn]; void getdp() { memset(dp,0,sizeof(dp)); dp[0][1][1] = 1;//最开始的时候,长度为1的,0算一种,1算一种,分别是dp[0][1][1]=dp[1][1][0]=1 dp[1][1][0] = 1; /** 然后状态转移方程是: 如果我在1前面接了一个0那么dp[0][i + 1][j + 1] += dp[1][i][j]; 如果我在1前面接了一个1那么dp[1][i + 1][j] += dp[1][i][j]; 如果我在0前面接了一个0那么dp[0][i + 1][j + 1] += dp[0][i][j]; 如果我在0前面接了一个1那么dp[1][i + 1][j] += dp[0][i][j]; **/ for(int i = 1; i <= 33; i ++) { for(int j = 0; j <= 33; j ++) { if(dp[0][i][j] != 0) { dp[0][i + 1][j + 1] += dp[0][i][j]; dp[1][i + 1][j] += dp[0][i][j]; } if(dp[1][i][j] != 0) { dp[0][i + 1][j + 1] += dp[1][i][j]; dp[1][i + 1][j] += dp[1][i][j]; } } } } /** 预处理之后,从最高位开始(最高位一定是1) 在最高位放0,然后接下来每一位,都算以当前位为最高位且为1且0数量大于1数量的所有状况 在最高位放1,接下来枚举所有位,如果当前位为0 ,那么只能放0,同时0的数量加1 如果当前位为1,那么我在当前位放0(保证了小于原数),算以当前位为0开始,后面0和前面0加起来不超过全部1的总状态 然后这是总共的小于n的状态数 再算一下n是不是符合条件的,是就加一 还是太弱了写了一下午。。。。 **/ int solve(int n) { if(n <= 0) return 0; int len = 0; while(n != 0) { bit[++len] = n % 2; n /= 2; } int cnt = 0; int zero = 0; for(int i = len; i >= 1; i --) { if(i == len) { for(int j = 1; j <= len - 1; j ++) { for(int k = j - 1; k >= j - k; k --) { cnt += dp[1][j][k]; } } } else { if(bit[i] == 0) { zero ++; continue; } else { for(int j = i; zero + j >= len - zero - j; j --) { cnt += dp[0][i][j]; } } } } if(count(bit + 1,bit + len + 1,0) >= count(bit + 1,bit + len + 1,1)) cnt ++; return cnt; } int main() { // freopen("in.txt","r",stdin); int l,r; getdp(); while(~scanf("%d%d",&l,&r)) { printf("%d\n",solve(r) - solve(l - 1)); } return 0; } 
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
   
   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值