Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8590   Accepted: 3003

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

Source

解题思路:

题意为在一个闭区间内,有多少个数它的二进制中0的个数大于等于1的个数。

整体思路为: 比方求 [2,12],  我们就求 (0,12] -(0,1]。

这样问题就转化为了 求(0,n]之间有多少个符合题意的数。

下面转载为:http://hi.baidu.com/ycdoit/item/6f64473c54a88f607d034b7f

[2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1]

 即:Rn[start,finish]=Rn[0,finish]-Rn[0,start-1]

 所以关键是给定一个X,求出Rn[0,X]

 如今如果X=10100100 

 这个X的二进制总共是8位。不论什么一个小于8位的二进制都小于X

 第一部分,求出长度为[0,7]区间内的二进制是RoundNumber的个数

  对于一个长度为Len的二进制(最高位为1)。怎样求出他的RoundNumbers呢(如果为用R(len)来表达),分为奇数和偶数两种情况

  1、奇数情况:在Len=2k+1的情况下。最高位为1,剩下2k位。至少须要k+1为0

   用C(m,n)表示排列组合数:从m个位置选出n个位置的方法

   R(len)=C(2k,k+1)+C(2k,k+2)+...+C(2k,2k).

   因为 A:C(2k,0)+C(2k,1)+...+C(2k,2k)=2^(2k)

     B:C(2k,0)=C(2k,2k), C(2k,1)=C(2k,2k-1) ,,C(2k,i)=C(2k,2k-i)

   于是  C(2k,0)+C(2k,1)+...+C(2k,2k)

    = C(2k,0)+C(2k,1)+...+C(2k,k)+C(2k,k+1)+C(2k,K+2)+...+C(2k,2k)

    = 2*R(len)+C(2k,k)

    =2^(2k)

    所以R(len)=1/2*{2^(2k)-C(2k,k)};

  2. 偶数情况 len=2*k,类似能够推到 R(len)=1/2*(2^(2k-1));

 第二部分,对于上面这个长度为8的样例:即X=10100100,首先假设本身是RoundNumbers,第二部分的结果总数+1

  第一部分已经将长度小于8的部分求出。如今要求长度=8的RoundNumber数目

  长度为8。所以第一个1不可改变

  如今到第二个1。假设Y是前缀如100*****的二进制,这个前缀下。后面取0和1必定小于X。已经有2个0,一个1。剩下的5个数字中至少须要2个0,

   所以把第二个1改为0:能够有C(5,2)+C(5,3)+C(5,4)+C(5,5)

  如今第三个1,也就是前最为101000**,相同求出。至少须要0个0就可,所以有C(2,0)+C(2,1)+C(2,2)个RoundNumbers

  。

。。

将所有除了第一个1以外的1所有变为0,如上算出有多少个RoundNumbers,结果相加(因为前缀不一样。所以后面无论怎么组合都是唯一的)

将第一部分和第二部分的结果相加,就是最后的结果了。

 精度要求方面,用int就能够了:two billion=20亿<2*1024*1024*1024=2^31,需用31位来表示数组。由于第一位总是1,所以求组合数的时候最多求30,C(30,k),k取值区间是[0,30],由于C(k,i)<2^k,所以结果用int表示就能够

參考:http://www.cnblogs.com/kuangbin/archive/2012/08/22/2651730.html

代码:

#include <iostream>
#include <string.h>
using namespace std;
int c[32][32];
int b[32]; void getCom()
{
memset(c,0,sizeof(c));
c[0][0]=c[1][0]=c[1][1]=1;
for(int i=2;i<=30;i++)
{
c[i][i]=c[i][0]=1;
for(int j=1;j<=i;j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}
} int cal(int n)
{
if(n<=1)//注意正整数,0不是round number
return 0;
int bit=0;
int temp=n;
int ans=0; while(temp)//b[]保存每一位二进制数,一共bit位
{
b[bit++]=temp%2;
temp/=2;
} for(int i=bit-1;i>=1;i--)//位数比当前数少一位的round number
{
if(i%2==0)
ans+=(1<<(i-1))/2;
else
ans+=((1<<(i-1))-c[i-1][(i-1)/2])/2;
} int num0=0,num1=0;
for(int i=0;i<bit;i++)//推断自身
if(b[i])
num1++;
else
num0++;
if(num0>=num1)
ans++; num0=0;num1=1;
for(int i=bit-2;i>=0;i--)
{
if(b[i]==0)
num0++;
else
{
num1++;
for(int k=i;k>=0&&k+num0+1>=i-k+num1-1;k--)//k是选择0的个数。总共0的个数要大于等于1的个数
ans+=c[i][k];
}
}
return ans;
} int main()
{
getCom();
int a,b;
cin>>a>>b;
cout<<cal(b)-cal(a-1)<<endl;
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)的更多相关文章

  1. POJ 3252 Round Numbers(组合)

    题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...

  2. POJ 3252 Round Numbers

     组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...

  3. poj 3252 Round Numbers(数位dp 处理前导零)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  4. POJ 3252 Round Numbers 数学题解

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  5. POJ 3252 Round Numbers 组合数学

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13381   Accepted: 5208 Description The ...

  6. POJ 3252 Round Numbers(组合数学)

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10223   Accepted: 3726 De ...

  7. POJ 3252 Round Numbers(数位dp&amp;记忆化搜索)

    题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...

  8. POJ - 3252 - Round Numbers(数位DP)

    链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...

  9. poj 3252 Round Numbers 【推导·排列组合】

    以sample为例子 [2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1] 即:Rn[start,finish]=Rn[0,finish]-R ...

随机推荐

  1. POJ1300(欧拉回路)

    Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2139   Accepted: 858 Descripti ...

  2. ZeroBrane Studio远程调试Lua程序(转)

    环境: ZeroBrane Studio安装在Windows 7上,而要调试的程序运行在CentOS上: 设置: 在windows 7上,打开ZeroBrane Studio,打开需要调试的文件,例如 ...

  3. WEB 3D SVG CAD 向量 几个实施

    一.他们所有的发展.从地上爬起来 VML+SVG发展矢量地图.你并不需要导入第三方的图片作为背景,直接在地图编辑器可以在底图内容编辑,由于岩石.巷道.煤层.画水.础地图样子再在其上面画出智慧线等设备, ...

  4. Android 角色时间戳

    我是在用MediaRecorder进行录像时发生视频和音频不同步的问题,请教了一些人后感觉应该是没有时间戳,之前一直觉得时间戳就是给用户看的一个数据,查了一下发现不是的,以下是转载的.希望对大家实用: ...

  5. NServiceBus开发

    使用NServiceBus开发分布式应用 系列主题:基于消息的软件架构模型演变 NServiceBus 是一个.Net平台下开源的消息服务框架,这类产品有时也被称作ESB(Enterprise Ser ...

  6. 有人实践过 Phabricator 以及 Arcanist 作为 code review 的工具么?(转)

    作者:覃超链接:http://www.zhihu.com/question/19977889/answer/13539702来源:知乎 平时就经常实践. 整个公司的code review就是使用这个. ...

  7. 添加服务引用和添加Web引用对比

    原文:添加服务引用和添加Web引用对比 在WindowsForm程序中添加服务引用和Web引用对比 为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验. 一. ...

  8. HDU 4067 Random Maze

    意甲冠军: 一个"随机图"它被定义为具有以下性质如: 一个入口和一个出口 有向图 对于入口  出度比入度大1 对于出口  入度比出度大1 对于其它点  入度等于出度 现给出一幅有向 ...

  9. java javaEE javaWEB J2EE程序猿猿程序是脑损伤,终身工作程序猿

    这几天我越来越郁闷.程序员现在很火----特javaEE员. 但我觉得火只是给人们的工作程序员. 原因 javaweb该项目是非常大的.没听过那个码农能单独接到什么项目.仅仅能被人剥削. 有人不信,我 ...

  10. CI-持续集成(2)-软件工业“流水线”技术实现(转)

    1   概述 持续集成(Continuous Integration)是一种软件开发实践.在本系列文章的前一章节已经对其背景及理论体系进行了介绍.本小节则承接前面提出的理论构想进行具体的技术实现. & ...