xtu summer individual 1 D - Round Numbers

D - Round Numbers

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

 

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

解题:乱搞,找规律,毛线数位dp啊,搞死人


 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #define LL long long

13 #define INF 0x3f3f3f3f

14 using namespace std;

15 LL dp[35] = {1,1,2,3,7};

16 LL c[40][40];

17 void init() {

18     int i,j;

19     for(i = 1; i <= 32; i++) {

20         c[i][0] = c[i][i] = 1;

21         for(j = 1; j < i; j++)

22             c[i][j] = c[i-1][j-1]+c[i-1][j];

23     }

24     for(i = 5; i <= 32; i++){

25         for(j = 0; j < i-j-1 ; j++)

26             dp[i] += c[i-1][j];

27         dp[i] += dp[i-1];

28     }

29 }

30 LL analysis(int x,int y,int base){

31     int i,j;

32     LL ans = 0;

33     for(i = 0; i+x <= base-i+y; i++)

34         ans += c[base][i];

35     return ans;

36 }

37 LL cal(int x) {

38     if(x == 1 || x == 0) return 1;

39     int d[40],i,len,one = 0,zero = 0;

40     LL ans = 0;

41     for(len = 0; x; x >>= 1,len++) {

42         d[len] = x&1;

43     }

44     ans += dp[len-1];

45     one++;

46     for(i = len-2; i > 0; i--) {

47         if(d[i]) {

48             ans += analysis(one,zero+1,i);

49             one++;

50         } else zero++;

51     }

52     if(d[0]){

53         if(one == zero || one == zero+1) ans += 1;

54         else if(one < zero) ans += 2;

55         one++;

56     }else zero++;

57     if(one >= 1 && d[0] == 0 && one <= len - one) ans++;

58     return ans;

59 }

60 int main() {

61     init();

62     int a,b,i = 1;;

63     while(~scanf("%d %d",&a,&b))

64         printf("%I64d\n",cal(b)-cal(a-1));

65     return 0;

66 }
View Code

 

你可能感兴趣的:(number)