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
题目大意:就是让你求出给定范围内的Round Numbers(转换成二进制后0的数量大于等于1的数量)的数量,很明显的就是数位dp了。
我的做法里dp[x][y][z]代表二进制位数为x,1的数量为y,开头为z的数字的数量,所以
dp[i][j][1] = dp[i - 1][j - 1][1] + dp[i - 1][j - 1][0];
dp[i][j][0] = dp[i - 1][j - 1][0] + dp[i - 1][j][0];
然后对于题目给出的数字进行数位统计即可,这题的数位统计跟那些求数字中不出现特定数字的题目不一样,位数从大到小统计时还需要记录前面的零和一的数量,所有满足条件的数都是以1开头的。详见代码注释。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
#define N 100005
ll dp[40][40][2];
void init(){
dp[0][0][0] = 1;
for (int i = 1; i <= 35; i++){
dp[i][0][0] = dp[i][i][1] = 1;
for (int j = 1; j < i; j++){
dp[i][j][1] = dp[i - 1][j - 1][1] + dp[i - 1][j - 1][0];
dp[i][j][0] = dp[i - 1][j - 1][0] + dp[i - 1][j][0];
}
}
}
int sum(int n){
if (n <=1 )return 0;
int num[40], cc = 0, nn = n;
int ans = 0;
while (nn != 0){
num[cc++] = nn % 2;
nn /= 2;
}
int zeronum = 0, onenum = 0;
bool flag = 0;
for (int i = cc - 1; i >= 0; i--){
if (i != 0){
int cnt = i / 2;//求1的最大数量
for (int j = 0; j <= cnt; j++){ ans += dp[i][j][1]; }
//上面两行是统计所有i位以1位开头的满足条件的数字
}
if (num[i] == 0){ zeronum++; continue; }
//这位是零,不能进行统计,否则会统计到比参数n大的数字
if (flag){
//这位是一,且前面的位数中有一了,取这位为零的情况进行统计
int cnt = (zeronum + 1 + i - onenum);
//求1的最大数量
if (cnt < 0)continue;
else{ cnt = cnt / 2; }
for (int j = 0; j <= cnt; j++){ ans += dp[i][j][1] + dp[i][j][0]; }
}
if (num[i] == 1){
onenum++;
flag = 1;
}
}
if (zeronum >= onenum)ans++;//若满足,说明参数n本身也是一个答案
return ans;
}
int a, b;
int main(){
init();
while (~scanf("%d%d", &a, &b)){
printf("%d\n", sum(b) - sum(a - 1));
}
return 0;
}