码迷,mamicode.com
首页 > 其他好文 > 详细

xtu summer individual 1 D - Round Numbers

时间:2014-07-31 16:39:27      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   strong   io   

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啊,搞死人


bubuko.com,布布扣
 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

 

xtu summer individual 1 D - Round Numbers,布布扣,bubuko.com

xtu summer individual 1 D - Round Numbers

标签:des   style   blog   http   color   os   strong   io   

原文地址:http://www.cnblogs.com/crackpotisback/p/3880860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!