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. 计蒜客 28317.Growling Gears-一元二次方程的顶点公式 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 G)

    G. Growling Gears 传送门 此题为签到题,直接中学的数学知识点,一元二次方程的顶点公式(-b/2*a,(4*a*c-b*b)/4*a):直接就可以得到结果. 代码: #include& ...

  2. 51nod 1649.齐头并进-最短路(Dijkstra)

    1649 齐头并进 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 在一个叫奥斯汀的城市,有n个小镇(从1到n编号),这些小镇通过 ...

  3. lua的模式匹配

    模式: 字符类:(character classes) . all characters %a letters %c control characters %d digits %l lower -ca ...

  4. 记录在Spring-Boot中使用Fegin调用RESTfull的PATCH方法设置

    使用了ZooKeeper,设置 spring.cloud.zookeeper.dependency.headers.enabled=false 参考: https://github.com/sprin ...

  5. CRC(16位)多项式为 X16+X15+X2+1

    其对应校验二进制位列为1 1000 0000 0000 0101,可这有17位啊,我怎么和16位信息进行异或啊?是不是不要最高位的1 你没有弄明白crc的意思.这17位后面再添上16个零,然后开始抑或 ...

  6. [置顶] kubernetes资源类型--RC和RS

    Replication Controller(RC) RC是K8S中的另一个核心概念,应用托管在K8S后,K8S需要保证应用能够持续运行,这是RC的工作内容. 主要功能 确保pod数量:RC用来管理正 ...

  7. 安装php扩展模块参数memcache和memcached在php中的应用

    一, memcache和memcached的区别与关系统php要想去访问memcached就得需要memcache扩展,这个道理和php连接mysql一样. 你不安装memcache扩展就没法识别me ...

  8. 如何使用apache的 work模式还是 prefork 模式

    注意: 2.4之前版本默认为prefork, 2.4已经变为event模式.三种模式比较: http://www.cnblogs.com/fnng/archive/2012/11/20/2779977 ...

  9. ambari修改admin密码

    https://community.hortonworks.com/questions/449/how-to-reset-ambari-admin-password.html 1) Postgres ...

  10. 【Hadoop】Hadoop MR 自定义分组 Partition机制

    1.概念 2.Hadoop默认分组机制--所有的Key分到一个组,一个Reduce任务处理 3.代码示例 FlowBean package com.ares.hadoop.mr.flowgroup; ...