zoukankan      html  css  js  c++  java
  • poj 3252 Round Numbers(数位dp)

    Round Numbers
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 11681   Accepted: 4392

    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

    题意是求一个范围中的Round数,这种数的二进制形式中的0多于1。把数字转换为二进制,然后简单的搞数位dp。发现:数位dp中判断条件和0有关的话,要用一个flag来防止数字倒过来以后开头的0,这种0显然是不合适的。

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <bitset>
    using namespace std;
    #define ll long long
    
    int dig[33];
    ll dp[33][33][33];
    
    ll dfs(int pos, int cnt0, int cnt1, int flag, int lim) {
        //if(pos == -1) printf("%d %d
    ", cnt0, cnt1);
        if(pos == -1) return cnt0 > cnt1;
        if(!lim && !flag && dp[pos][cnt0][cnt1] != -1) return dp[pos][cnt0][cnt1];
        int End = lim ? dig[pos] : 1;
        ll ret = 0;
        for(int i = 0; i <= End; i++) {
            if(flag) {
                if(i) ret += dfs(pos - 1, 0, 0, 0, lim && (i == End));
                else ret += dfs(pos - 1, 0, 0, 1, lim && (i == End));
            }
            else {
                if(i) ret += dfs(pos - 1, cnt0, cnt1 + 1, 0, lim && (i == End));
                else ret += dfs(pos - 1, cnt0 + 1, cnt1, 0, lim && (i == End));
            }
        }
        if(!lim && !flag) dp[pos][cnt0][cnt1] = ret;
        return ret;
    }
    
    ll func(ll num) {
        int n = 0;
        //bitset<32> btt(num);
        while(num) {
            dig[n++] = num & 1;
            num >>= 1;
        }
        return dfs(n - 1, 0, 0, 1, 1);
    }
    
    int main() {
        ll n, m;
        memset(dp, -1, sizeof(dp));
        while(~scanf("%I64d %I64d", &n, &m)) {
            printf("%I64d
    ", func(m) - func(n - 1));
        }
    }
  • 相关阅读:
    怎样使用Secure CRT查看vcenter和esxi主机的日志文件(转)
    Linux下如何查看系统启动时间和运行时间
    Java使用线程并发库模拟弹夹装弹以及发射子弹的过程
    使用Java线程并发库实现两个线程交替打印的线程题
    Android Exception Type "share_dialog_title" is not translated in en, zh-rTW strings
    Java JDK1.5、1.6、1.7新特性整理
    Java 中long类型转换成为int类型时可能会出错的地方
    Java 将任意数组的任意两个位置的数据进行交换
    Java设置以及获取JavaBean私有属性进阶
    Java使用PropertyDescriptor获取实体类中私有属性的值,并给私有属性赋值
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5656621.html
Copyright © 2011-2022 走看看