zoukankan      html  css  js  c++  java
  • POJ3252 Round Numbers

    链接

    Round Numbers

    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 17189 Accepted: 7139

    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

    题目大意: 求x, y之间二进制0的个数不小于1个数的数的个数

    数位dp板子题, 有点忘记, 调了1~2h T_T

    总结一下数位dp中的关键点
    1. 记搜中的dp数组严格要在flag==0的前提下更新与使用
    2. 注意数字总数位大小对答案的影响
    3. dp数组中的状态依题意而定,去杂就简
    4. 对于每个数位注意边界由2个因素控制

    Code

    #include<cstdio>
    #include<cstring>
    #include<vector>
    
    std::vector <int> B;
    int dp[55][55][55];
    
    int DFS(int k, int pre, int flag, int cnt, int sz){
            if(k == -1){ 
                    if(sz && (cnt<<1) >= sz) return 1;	//注意判sz==0
                    return 0;
            }
            if(!flag && ~dp[k][sz][cnt]) return dp[k][sz][cnt]; //!flag
            int end = flag?B[k]:1;
            int tmp = 0;
            for(int i = 0; i <= end; i ++){
                    if(!i && pre == 2) tmp += DFS(k-1, 2, 0, 0, 0);	//继续无数字
                    else{
                            if(i && pre == 2) tmp += DFS(k-1, i, (i==end)&&flag, cnt + (!i), k+1); //创立新数字
                            else tmp += DFS(k-1, i, (i==end)&&flag, cnt + (!i), sz);	//延续旧数字
                    }
            }
            if(!flag) dp[k][sz][cnt] = tmp;					//!flag
            return tmp;
    }
    
    int Work(int x){
            memset(dp, -1 , sizeof dp);
            B.clear();
            while(x) B.push_back(x & 1), x >>= 1;
            return DFS(B.size()-1, 2, 1, 0, 0);
    }
    
    int main(){
            int x, y;
            scanf("%d%d", &x, &y);
            printf("%d
    ", Work(y)-Work(x-1));
            return 0;
    }
    
    
  • 相关阅读:
    How to produce the first draft of research paper?
    ETL
    BDA chapter 10
    <转>Java转iOS-第一个项目总结(2):遇到问题和解决方案
    <转>从Java转iOS第一个项目总结
    (转)总结iOS 8和Xcode 6的各种坑
    iOS开发之Xcode6之后不再自动创建Pch预编译文件,该如何解决这个问题?
    iOS开发:Objective-C中通知与协议的区别?
    PT和PX是什么鬼?
    使用cocoaPods经常出现的问题以及解决方案
  • 原文地址:https://www.cnblogs.com/zbr162/p/11822693.html
Copyright © 2011-2022 走看看