POJ 3252 Round Numbers 数学题解
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
Output
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 数学题解的更多相关文章
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
- POJ 3252 Round Numbers
组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...
- [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8590 Accepted: 3003 Des ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ 3252 Round Numbers 组合数学
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13381 Accepted: 5208 Description The ...
- POJ 3252 Round Numbers(组合数学)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10223 Accepted: 3726 De ...
- POJ 3252 Round Numbers(数位dp&记忆化搜索)
题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...
- POJ - 3252 - Round Numbers(数位DP)
链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...
- 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 ...
随机推荐
- (18)PL/SQL
PL/SQL(Procedure Language/SQL) PL/SQL是Oracle对sql语言的过程化扩展---指在sql命令语言中增加了过程处理语句(如分支.循环等),使sql语言具有过程处理 ...
- bzoj2440(莫比乌斯函数)
bzoj2440 题意 求第 k 个不是完全平方数(除 1 以外)的正倍数的数. 分析 利用二分法求解,二分 x ,判断 x 是否是第 k 个数即可,那么我们就要计算 [1, x] 有几个符合条件的数 ...
- [POJ 2329] Nearest number-2
Link: POJ 2329 传送门 Solution: 比较明显的$dp$,但爆搜好像也能过 用多个方向$dp$来解决此题,最后汇总答案即可 一开始我写了4个,但后来发现只要相反的2个方向即可,同时 ...
- androd 获得wifi列表
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- 基于Rebound制造绚丽的动画效果-入门篇
基于Rebound制造绚丽的动画效果-入门篇 Rebound是什么? Rebound是一个来自 Facebook 公司的 Java物理和动画库.Rebound spring 模型可用于创建动画,让你感 ...
- 设计模式之过滤器模式(php实现)
/** * github地址:https://github.com/ZQCard/design_pattern * 过滤器模式(Filter Pattern)或标准模式(Criteria Patter ...
- oracle 被另一个用户锁定
于是我就直接在上面改字段,在点打钩(记入改变)的时候提示,记录被另一个用户锁住,一开始还以为整个表被锁住了,后来发现,仅仅是这个字段不能改变,其他的字段可以. 网上找了资料,发现是:当多个用户并发地存 ...
- Qt之QStyledItemDelegate类
主要用于自定义项的display和编辑: 通常有两个重载函数: // 决定该单元格的推荐大小 virtual QSize sizeHint(const QStyleOptionViewItem &am ...
- python核心编程学习记录之正则表达式
- [转载]SecureCRT 绝佳配色方案, 保护你的眼睛
FROM:http://blog.csdn.net/zklth/article/details/8937905 SecureCRT 绝佳配色方案, 保护你的眼睛 关键词:SecureCRT配色, ...