2013
11-12

Round Numbers

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).

Line 1: Two space-separated integers, respectively Start and Finish.

Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish

2 12

6

import java.io.BufferedReader;
import java.math.BigInteger;

public class Main {

static long[] arr = new long[] { 0, 0, 1, 2, 6, 11, 27, 49, 113, 206, 462,
848, 1872, 3458, 7554, 14030, 30414, 56747, 122283, 229045, 491189,
923099, 1971675, 3716111, 7910415, 14946945, 31724161, 60078293,
127187157, 241346585, 509782041, 969094193, 2042836017 };

public static void main(String[] args) throws Exception {
System.in));
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
System.out.println(jisuan(b) - jisuan(a - 1));
}

public static long jisuan(int a) {
String bi = Integer.toBinaryString(a);
long sum = arr[bi.length() - 1];
char[] ci = bi.toCharArray();
int c0 = 0;
int c1 = 1;
int temp, top, mid;
if (ci.length % 2 == 0) {
mid = ci.length / 2;
} else {
mid = ci.length / 2 + 1;
}
for (int i = 1; i < ci.length; i++) {
if (ci[i] == '0') {
c0++;
} else {
temp = mid - (c0 + 1);
if (temp < 0) {
temp = 0;
}
top = ci.length - i - 1;
for (int j = temp; j <= top; j++) {
sum += c(top, j);
}
c1++;
}
}
if (c0 >= c1) {
sum++;
}
return sum;
}

public static long c(int m, int n) {
BigInteger sub = new BigInteger("1");
BigInteger div = new BigInteger("1");
for (int i = 1; i <= n; i++) {
sub = sub.multiply(new BigInteger(i + ""));
}
for (int i = m; i >= m - n + 1; i--) {
div = div.multiply(new BigInteger(i + ""));
}
return div.divide(sub).longValue();
}
}

1. 你的理解应该是：即使主持人拿走一个箱子对结果没有影响。这样想，主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率，但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

2. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count