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

POJ3252——Round Number(组合数学)

时间:2014-11-13 16:05:00      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

Round Numbers


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

题目大意:

    问区间[a,b]中,有多少个数化成二进制后,0比1多。

解题思路:

    组合数学问题。

    看大牛博客过的。直接贴网址吧。

            This is the link

Code:

 1 /*************************************************************************
 2     > File Name: poj3252.cpp
 3     > Author: Enumz
 4     > Mail: 369372123@qq.com
 5     > Created Time: 2014年10月29日 星期三 00时53分32秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 100000
23 using namespace std;
24 int com[40][40];
25 int num[400];
26 void init()
27 {
28     for (int i=0; i<=39; i++)
29         for (int j=0; j<=i; j++)
30             if (!j||i==j)
31                 com[i][j]=1;
32             else
33                 com[i][j]=com[i-1][j-1]+com[i-1][j];
34 }
35 int ret(int a)
36 {
37     memset(num,0,sizeof(num));
38     num[0]=0;
39     while (a)
40     {
41         num[++num[0]]=a%2;
42         a/=2;
43     }
44     int ans=0;
45     for (int i=1; i<=num[0]-2; i++)
46         for (int j=i/2+1;j<=i;j++)
47             ans+=com[i][j];
48     int zero=0;
49     for (int i=num[0]-1;i>=1;i--)
50     {
51         if (num[i])
52             for (int j=(num[0]+1)/2-zero-1;j<=i-1;j++)
53                 ans+=com[i-1][j];
54         else zero++;
55     }
56     return ans;
57 }
58 int a,b;
59 int main()
60 {
61     init();
62     cin>>a>>b;
63     cout<<ret(b+1)-ret(a)<<endl;
64     return 0;
65 }

 

POJ3252——Round Number(组合数学)

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/Enumz/p/4095009.html

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