Round Numbers POJ - 3252(数位dp+判断前导零)
題意
求二進(jìn)制表示中0的個(gè)數(shù)大于1的數(shù)的個(gè)數(shù)。
題目
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
分析:
我們將二進(jìn)制考慮成01串
我們先不考慮有前導(dǎo)零的情況:那么在k個(gè)空位中放i個(gè)0,別的都放1的方案數(shù)為CkiC{_{k}}^{i}Ck?i(我們將一個(gè)位置放0表示成取出這個(gè)位置的數(shù))。
然后我們可以先計(jì)算1~r,然后減去1~l?1就是l~r這個(gè)區(qū)間內(nèi)的數(shù)量。
用數(shù)位dp+記憶化,得到求二進(jìn)制表示中0的個(gè)數(shù)大于1的數(shù)的個(gè)數(shù)。
總結(jié)
以上是生活随笔為你收集整理的Round Numbers POJ - 3252(数位dp+判断前导零)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HDU - 2444——The Acco
- 下一篇: 白火龙果的功效与作用、禁忌和食用方法