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

    题意

    Language:
    Round Numbers
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 17762Accepted: 7423

    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

    Source

    拆成2进制,在记录0和1的个数
    求区间[a,b]中,满足传化成2进制后,0的个数>=1的个数的数字的个数

    分析

    参照lienus的题解。总算做到了一道正经的数位dp了。之前做的都可以用各种奇技淫巧避开了数位dp经典的状态设计,没有可供借鉴之处。

    dp[pos][num0][num1]表示从高往低到达第pos位时含有num0个0和num1个1在后面任意填时该状态下的总个数。

    注意加一个变量zero来判断是否前导0.

    时间复杂度……然而我这个dp似乎跟暴力差不多?

    代码

    #include<iostream>
    #include<cstring>
    #define rg register
    #define il inline
    #define co const
    template<class T>il T read(){
        rg T data=0,w=1;rg char ch=getchar();
        while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
        while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
        return data*w;
    }
    template<class T>il T read(rg T&x) {return x=read<T>();}
    typedef long long ll;
    
    int f[40][40][40],dig[12];
    int dp(int pos,int num0,int num1,int limit,int zero){
    	if(!pos) return num0>=num1;
    	if(!limit&&~f[pos][num0][num1]) return f[pos][num0][num1];
    	int len=limit?dig[pos]:1,ans=0;
    	for(int i=0;i<=len;++i){
    		if(!zero){ //非前导0,即前面已有1
    			if(i) ans+=dp(pos-1,num0,num1+1,limit&&i==len,0);
    			else ans+=dp(pos-1,num0+1,num1,limit&&i==len,0);
    		}
    		else{ //前导0,前面没有1
    			if(i) ans+=dp(pos-1,0,1,limit&&i==len,0);
    			else ans+=dp(pos-1,0,0,limit&&i==len,1);
    		}
    	}
    	if(!limit) f[pos][num0][num1]=ans;
    	return ans;
    }
    int solve(int x){
    	int len=0;
    	for(;x;x>>=1) dig[++len]=x&1;
    	return dp(len,0,0,1,1);
    }
    int main(){
    	int a=read<int>(),b=read<int>();
    	memset(f,-1,sizeof f);
    	printf("%d
    ",solve(b)-solve(a-1));
    	return 0;
    }
    
  • 相关阅读:
    vue相关ajax库的使用
    vue-router核心概念
    Vue.js核心概念
    vue核心概念
    SQL 的各种 JOIN 用法
    应用Itextsharp 添加图片到pdf
    MVC区域路由设置
    LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
    使用命令行执行.sql文件
    MVC异常捕获处理,FilterConfig
  • 原文地址:https://www.cnblogs.com/autoint/p/10804329.html
Copyright © 2011-2022 走看看