POJ 3252 Round Numbers 数学题解

时间:2022-10-07 11:44:48

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

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

  4. poj 3252 Round Numbers&lpar;数位dp 处理前导零&rpar;

    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&lpar;数位dp&amp&semi;amp&semi;记忆化搜索&rpar;

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

  8. POJ - 3252 - Round Numbers&lpar;数位DP)

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

  9. poj 3252 Round Numbers 【推导&&num;183&semi;排列组合】

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

随机推荐

  1. 字符串匹配算法——KMP算法

    处理字符串的过程中,难免会遇到字符匹配的问题.常用的字符匹配方法 1. 朴素模式匹配算法(Brute-Force算法) 求子串位置的定位函数Index( S, T, pos). 模式匹配:子串的定位操 ...

  2. jquery模拟下拉框单选框复选Select,Checkbox,Radio

    在项目中,你会发现设计稿中常常会有单选框,复选框,但都不是系统默认的样式,这就可以用jquery来模拟它们:如图所示,实现它们所需要的代码如下: 首先需要引入的代码: <link rel=&qu ...

  3. 用 &lt&semi;a&gt&semi; 实现 &lt&semi;form&gt&semi; 表单的提交

    <form action="{:U('Index/fwbhss')}" method="post" id="tform" name=& ...

  4. POJ 2528 &lpar;线段树 离散化&rpar; Mayor&&num;39&semi;s posters

    离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...

  5. 每天一个Linux命令&lpar;2&rpar;&colon; ls

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  6. maven 编译部署src&sol;main&sol;java下的资源文件

    maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到targ ...

  7. matlab 函数说明&mdash&semi;ordfilt2

    今天看harris角点实现的源码,在某一个版本中看到了这个函数,不是很理解,doc ordfilt2之后还是不清楚,终于在matlab论坛上搞清楚了ordfilt2的功能.   中文理解函数名就是顺序 ...

  8. JVMGC机制

    GC 是JVM的垃圾回收器.与C/C++不同,java程序员无需考虑太多内存分配的位置,更不用考虑内存释放的机制,java对象内存的申请和释放都有JVM托管.JVM的内存释放机制就是GC. GC的过程 ...

  9. maven &plus; bat 实现快速编译打包模块代码

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  10. Retrieve id of record just inserted into a Java DB &lpar;Derby&rpar; database

    https://*.com/questions/4894754/retrieve-id-of-record-just-inserted-into-a-java-db-derby ...