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

整体来说,十分困难的一道数学counting problem。

利用组合数学去做这些题目总是须要很费力去总结规律的。

或许是数学思维还须要多锻炼吧。

详细是找出规律,依照二进制数位去数这种题目。当然是不能模拟的。

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <bitset>
using namespace std; int cTbl[33][33]; int calCombinate(int up, int down)
{
down = min(down, up - down);
int ans = 1;
for (int i = 1; i <= down; i++)
{
ans *= (up - i + 1);
ans /= i;
}
return ans;
} void genTbl()
{
cTbl[0][0] = 1;
for (int i = 1; i < 33; i++)
{
cTbl[i][0] = 1;
for (int j = 1; j <= i; j++)
{
cTbl[i][j] = calCombinate(i, j);
}
}
} int calZeros(int n)
{
bitset<33> bn = n;
int len = 32;
while (!bn[len]) len--; int ans = 0;
for (int i = 1; i < len; i++)
{
for (int j = (i+2)>>1; j <= i; j++)
ans += cTbl[i][j];
}
int zeros = 0, half = (len+2) >> 1;
for (int i = len-1; i >= 0; i--)
{
if (bn[i])//前面选择好01了。改为为1。变为0。然后选择余下的0有多少个
{
for (int j = half-zeros-1; j <= i; j++)
{
if (j < 0) continue;
ans += cTbl[i][j];
}
}
else zeros++;
}
return ans;
} int main()
{
genTbl();
int a, b;
while (scanf("%d %d", &a, &b) != EOF)
{
printf("%d\n", calZeros(b+1) - calZeros(a));
}
return 0;
}

POJ 3252 Round Numbers 数学题解的更多相关文章

  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. [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)

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

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

    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. Python的并发并行[2] -> 队列[0] -> queue 模块

    queue 模块 / queue Module 1 常量 / Constants Pass 2 函数 / Function Pass 3 类 / Class 3.1 Queue类 类实例化:queue ...

  2. Codeforces #425 Div2 D

    #425 Div2 D 题意 给出一个树形图,每次询问给出三个点,从其中选择两个作为起始点,一个终点,求从两个起始点出发(走最短路)到达终点经过的共同的点最多的数量. 分析 这种树上点与点之间距离有关 ...

  3. HDU 4417 Super Mario (划分树)(二分)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 九. 常用类库、向量与哈希1.Java基础类库

    Java 的类库是 Java 语言提供的已经实现的标准类的集合,是 Java 编程的 API(Application Program Interface),它可以帮助开发者方便.快捷地开发 Java ...

  5. JDBC-登陆功能实现

    1.user.java package songyan.jdbc.entity; import java.util.Date; public class User { private int id; ...

  6. 解析HTML文件 - 运用SgmlReader类来解析HTML文件

    运用.NET Framework类来解析HTML文件.读取数据并不是最容易的.虽然你可以用.NET Framework中的许多类(如StreamReader)来逐行解析文件,但XmlReader提供的 ...

  7. Visio中如何画面积一样的形状

    如图,刚开始我想着用辅助的那些线(对齐),实现不了,后来想想直接复制就行了...........

  8. Solr In Action 中文版 第一章(三)

    3.1              为什么选用Solr? 在本节中.我们希望能够提供一些关键信息来帮助于你推断Solr是否是贵公司技术方案的正确选择.我们先从Solr吸引软件架构师的方面说起. 3.1  ...

  9. CentOS7环境下SSH端口修改笔记

    CentOS7环境下SSH端口修改笔记 说明: CentOS7服务器环境,默认SSH端口为22,考虑到安全方面问题,欲修改端口为62231(机器内网IP为192.168.1.31) ssh配置文件和i ...

  10. 通过Linux定时任务实现定时轮询数据库及发送Http请求

    通过Linux定时任务实现定时轮询数据库及发送Http请求 概述 有时需要临时增加一个定时任务(需要根据数据库查询结果然后发送HTTP请求),如果在项目中额外增加(Java+Spring+Quartz ...