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;
    }
    
    
  • 相关阅读:
    MySQL Binlog信息查看
    MySQL Binlog和Relaylog生成和清理
    MySQL Binlogbinlog_format参数
    Maven的核心概念及安装
    Maven工程约定目录结构
    解决 internal/modules/cjs/loader.js:983 throw err; ^ Error: Cannot find module ‘express‘ 报错问题
    Linux之开机、重启和用户登录注销等操作
    Linux之系统文件概述
    Linux之基本指令
    express的基本使用
  • 原文地址:https://www.cnblogs.com/zbr162/p/11822692.html
Copyright © 2011-2022 走看看