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));
        }
    }
  • 相关阅读:
    用TortoiseSVN忽略文件或文件夹(ignore)(网络摘抄记录)
    GridView解决同一行item的高度不一样,如何同一行统一高度问题?
    解决android studio引用远程仓库下载慢(转)
    Databinding在自定义ViewGroup中如何绑定view
    (转 )【Android那些高逼格的写法】InvocationHandler与代理模式
    (转)秒懂,Java 注解 (Annotation)你可以这样学
    View拖拽 自定义绑定view拖拽的工具类
    bcrypt对密码加密的一些认识(学习笔记)
    Node.js+Koa开发微信公众号个人笔记(三)响应文本
    Node.js+Koa开发微信公众号个人笔记(二)响应事件
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5656621.html
Copyright © 2011-2022 走看看