Round Numbers

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 10484 Accepted: 3831

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

USACO 2006 November Silver

看了好久的题解才看懂了,求小于这个数的二进制中0大于1的个数

如100100010 首先可以求出二进制位数小于这个数的(其值一定比这个数小如10010000),当位数相同时可以分成几部分求如[100000000-100100000)[100100000-1001000100)求的值就是组合数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef long long LL; const int MAX = 1e5+10; int Bin[35]; int c[33][33]; void ComBinnations()//打表,组合数
{
for(int i=0;i<=32;i++)
{
for(int j=0;j<=i;j++)
{
if(!j||i==j)
{
c[i][j]=1;
}
else
{
c[i][j]=c[i-1][j]+c[i-1][j-1];
} }
}
} void GetBite(int n)//分解二进制
{
Bin[0]=0;
while(n)
{
Bin[++Bin[0]]=n%2;
n/=2;
}
} int cal(int n)
{
int sum=0;
GetBite(n);
for(int i=1;i<Bin[0];i++)//比这个数的位数小
{
for(int j=(i+1)/2;j<i;j++)
{
sum+=c[i-1][j];
}
}
int ans=0;
for(int i=Bin[0]-1;i>=1;i--)//相等的时候
{
if(Bin[i])
{
for(int j=(Bin[0]+1)/2-1-ans;j<i;j++)
{
sum+=c[i-1][j];
}
}
else
{
ans++;
}
}
return sum;
} int main()
{
int n,m;
ComBinnations();
while(~scanf("%d %d",&n,&m))
{
printf("%d",cal(m+1)-cal(n));
}
return 0;
}

Round Numbers(组合数学)的更多相关文章

  1. POJ 3252 Round Numbers 组合数学

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

  2. Round Numbers(组合数学)

    Round Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

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

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

  4. POJ 3252 Round Numbers

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

  5. POJ3252——Round Number(组合数学)

    Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...

  6. POJ 3252 Round Numbers 数学题解

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

  7. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  8. [BZOJ1662][POJ3252]Round Numbers

    [POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...

  9. POJ 3252 Round Numbers(组合)

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

随机推荐

  1. Redis认识

    摘要:认识Redis 1.安装Redis npm install redis 2.Redis使用 var redis = require("redis"), client = re ...

  2. Velocity(3)——字面值和转义

    货币符号: $是美元的符号,在文本中出现的"$2.5"这样的字符串,是不会被Velocity解释为一个变量或者一个属性的,因为2.5或者2或者5都不是一个VTL标识符. 转义: 假 ...

  3. centos 6.4下设置输入法

    最近安装了centos,设置输入法步骤如下: 1)确定安装了Chinese support: : yum install "@Chinese Support" 2)输入法设置 在界 ...

  4. mysql 插入重复值 INSERT ... ON DUPLICATE KEY UPDATE

    向数据库插入记录时,有时会有这种需求,当符合某种条件的数据存在时,去修改它,不存在时,则新增,也就是saveOrUpdate操作.这种控制可以放在业务层,也可以放在数据库层,大多数数据库都支持这种需求 ...

  5. [reprint]malloc与calloc的区别

    [http://blog.163.com/crazy20070501@126/] 转自某自由人的博客: malloc与calloc的区别 函数malloc()和calloc()都可以用来动态分配内存空 ...

  6. ofbiz进击 第二节。 control 理解与创建

    首先要说的是,学习ofbiz,要去http://ofbiz.apache.org/官网里面,去看右边菜单里   Management Apps  的例子,然后找到类似的页面,去看调用的源码方法. co ...

  7. session讲解(二)——商城购物车练习

    网上商城中“添加商品到购物车”是主要功能之一,所添加的商品都存到了session中,主要以二维数组的形式存储在session中,在这里我们将以买水果为例 第一:整个水果商品列表 <body> ...

  8. Mysql 经典

  9. 关于基于.NET Framework的网络通信程序底层扫盲

    引自<三维游戏引擎的设计与实现>书中9.3节:          如果服务端在.Net Framework基础上开发,那么可以直接选择使用.Net Framework提供的Socket类, ...

  10. Floyd算法核心代码证明

    Flody  大家都知道这个最终模版: for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i ...