您的位置:首页 > 其它

poj 3252组合数学

2016-04-14 17:03 363 查看
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

题目有点长,大致的意思是在两头牛给出的范围内,数转化为二进制后,0比1多或者相等的数的个数

打表大法虽然好但是如果是2,000,000,000一定会超时,那么分析一下

int left,right;        //左右区间

一个数转化成2进制区间就是2^n~2^n+1之间,那就有n位数,2,000,000,000转化成2进制为1,110,111,001,101,011,001,010,000,000,000有31位,那么就是right最大为31位的数,如果要0的数目大于等于1,那么就是

中j=0;j<=i/2;j++的所有排列组合

中间的数都解决了,那么靠近left和right的数怎么解决,那就是打表,如(10)d=(1010)b,那么比b大的就是11。。或者1011,也就是除了首位1遇到的第一个1以前可以放1的所有数加上本数除了首位1遇到的第一个1后面的数位含有的0的位数的c(j,i),同理也可以是小于。

最后贴代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  组合数学 acm