• [POJ 3252]Round Numbers


    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

    题解

    数位 $DP$ 。

    令 $f_{i,j,0/1}$ 表示二进制下位数为 $i$ ,$i$ 位中 $0$ 的个数为 $j$ 且第 $i$ 位为 $0/1$ 的方案数。

    首先转移有: $$f_{i,j,0} = f_{i-1,j-1,0}+f_{i-1,j-1,1} \ f_{i,j,1} = f_{i-1,j,0}+f_{i-1,j,1}$$

    对于统计答案的过程,注意边界情况,并且注意最后一位的考虑。

     1 //It is made by Awson on 2018.1.17
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <queue>
     7 #include <stack>
     8 #include <cstdio>
     9 #include <string>
    10 #include <vector>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Min(a, b) ((a) < (b) ? (a) : (b))
    19 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
    20 #define writeln(x) (write(x), putchar('
    '))
    21 using namespace std;
    22 void read(int &x) {
    23     char ch; bool flag = 0;
    24     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    25     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    26     x *= 1-2*flag;
    27 }
    28 void write(int x) {
    29     if (x > 9) write(x/10);
    30     putchar(x%10+48);
    31 }
    32 
    33 int a, b, f[35][35][2];
    34 
    35 void pre() {
    36     f[1][1][0] = f[1][0][1] = 1;
    37     for (int i = 2; i <= 32; i++) {
    38     for (int j = 0; j <= i; j++) {
    39         if (j) f[i][j][0] = f[i-1][j-1][1]+f[i-1][j-1][0];
    40         f[i][j][1] = f[i-1][j][1]+f[i-1][j][0];
    41     }
    42     }
    43 }
    44 int query(int x) {
    45     int a[35], len = 0, ans = 0, tol = 0; while (x) a[++len] = (x&1), x >>= 1;
    46     for (int i = len; i >= 1; i--) {
    47     if (a[i]) {
    48         if (i != len)
    49         for (int j = Max((len/2+len%2)-(len-i+1-tol), 0); j <= i; j++)
    50             ans += f[i-1][j][1]+f[i-1][j][0];
    51         ++tol;
    52     }
    53     if (i != len) {
    54         for (int j = (i/2+i%2); j <= i; j++)
    55         ans += f[i][j][1];
    56     }
    57     }
    58     if (a[1]) tol--;
    59     if (len > 1) {
    60     if (a[1] == 1) {
    61         if (tol*2 < len) ans += 2;
    62         else if (tol*2 == len) ans++;
    63     }else if (tol*2 <= len) ans++;
    64     }
    65     return ans;
    66 }
    67 void work() {
    68     read(a), read(b); pre();
    69     writeln(query(b)-query(a-1));
    70 }
    71 int main() {
    72     work();
    73     return 0;
    74 }
  • 相关阅读:
    控件右键菜单的实现以及选中后勾选
    DataGridView控件使用
    return,continue,break的区别
    break和continue的区别 循环终止办法
    事件
    跨线程改变控件属性 线程调用带参数方法
    XML配置文件相关
    抽象类及与接口的区别
    字典
    Oracle学习第一天
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8301103.html
Copyright © 2020-2023  润新知