zoukankan      html  css  js  c++  java
  • POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252

    Round Numbers
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 14640   Accepted: 5881

    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

    题解:

    求一段区间内有多少个数的二进制表示的“0”的个数大于等于“1”的个数。经典的数位DP。

    注意不能含有前缀0(可以含有前缀0的话,就可以无限添加前缀0,那所有数都能满足条件了)。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 typedef long long LL;
    14 const double eps = 1e-6;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MOD = 1e9+7;
    18 const int MAXN = 1e6+10;
    19 
    20 int dp[50][50][50], bin[100];
    21 
    22 //pos为当前位, num0、num1分别为高位的0、1的个数, lim表示是否处在上限, ok记录之前的位是否有效,即高位是否都为前缀0
    23 int dfs(int pos, int num0, int num1, bool lim, bool ok)
    24 {
    25     if(!pos) return  num0>=num1;
    26 
    27     //如果还处在上限,就不能直接返回,因为低位的数值不能随意取。
    28     if(!lim && dp[pos][num0][num1]!=-1) return dp[pos][num0][num1];
    29 
    30     int ret = 0;
    31     int maxx = lim? bin[pos] : 1;   //求当前位的最大值
    32     for(int i = 0; i<=maxx; i++)
    33         ret += dfs(pos-1, ok?(num0+(i==0)):0, ok?num1+(i==1):(i==1), lim&&(i==bin[pos]), ok||i );
    34 
    35     //如果高位不处在上限,那么表明其低位可以自由取值,这时就需要记忆化。
    36     if(!lim) dp[pos][num0][num1] = ret;
    37     return ret;
    38 }
    39 
    40 int solve(int n)
    41 {
    42     int len = 0;
    43     while(n)
    44     {
    45         bin[++len] = n&1;
    46         n >>= 1;
    47     }
    48     return dfs(len, 0, 0, 1, 0);
    49 }
    50 
    51 int main()
    52 {
    53     int n, m;
    54     memset(dp,-1,sizeof(dp));
    55     while(scanf("%d%d",&m, &n)!=EOF)
    56     {
    57         cout<< solve(n) - solve(m-1)<<endl;
    58     }
    59 }
    View Code
  • 相关阅读:
    PHP用户注册邮箱验证激活帐号
    利用openssl进行RSA加密解密
    RSA算法使用介绍
    JS七种加密解密方法
    JS调用PHP 和 PHP调用JS的方法举例
    php注册登录时生成的验证码
    Joomla!网站扫描工具joomscan
    Xamarin XAML语言教程控件模板的模板绑定
    Xamarin.Forms使用Slider注意问题
    ASP.net 资源请求漏洞利用工具PadBuster
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7910573.html
Copyright © 2011-2022 走看看