动态规划 | 数位 | 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 tossusing hooves.
Theyhave thus resorted to “round number” matching. The first cow picks aninteger less than two billion. The second cow does the same. If the numbers areboth “round numbers”, the first cow wins, otherwise the second cowwins.
A positive integer N is said to be a “round number” if the binary representation of N has as many or more zero es than it has ones. For example,the integer 9, when written in binary form, is 1001. 1001 has two zero es and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zero es 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.
Helpher by writing a program that tells how many round numbers appear in theinclusive 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
1 | 2 12 |
Sample Output
1 | 6 |
Hint
No Hint
分析
一道数位 $DP$ 题,根据题目要求,要在 $[L,R]$ 区间内求出转化为二进制之后 $0$ 的个数不少于 $1 $ 的个数的数字个数,用试填法 + $DFS$ 的记忆化实现
注意前导0的问题,代码中用 $lead$ l来指示前导0
Codes
1 |
|