POJ 3252:Round Numbers

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 10099

Accepted: 3669

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

题目给了一个区间,要求的是这个区间之内有多少个数,这种数满足转为二进制时0的数量大于等于1的数量。

也是头一次接触数位DP,捡起了很久没有用的C[j][i] = C[j-1][i-1] + C[j-1][i]。这个高中时做排列组合题目时会用到的公式。

求[start,finnish]这个区间之内有多少数符合条件,这样做比较麻烦,因为要考虑到start与finnish的各种情况,不如求0到start有多少个符合条件的数,求0到finnish+1有多少符合条件的数,之后两者相减即是结果。

所以这样就归结为求从0到n,符合条件的数的数量。

首先求转成二进制之后,位数本身就小于n的数量。这个求的时候很简单,满足0大于等于一般即可。

之后是位数相等的,由于n第一位一定是1,所以从左至右看0的数量,如果面对的是1,那么假设其是0。(因为此时,假设是0之后后面所有求出符合条件的数都是小于n的,所以符合题意。)求后面满足0的数量占整个数量一半以上的有多少个,不断相加。如果是0,则0的数量++即可。

代码:

#include <iostream>
using namespace std; int c[33][33];
int bin[35]; void init()
{
int i,j;
memset(c,0,sizeof(c)); for(i=0;i<=32;i++)
{
for(j=0;j<=i;j++)
{
if(j==0||j==i)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
} void dec_to_bin(int x)
{
memset(bin,0,sizeof(bin));
while(x!=0)
{
bin[++bin[0]]=x&1;
x=x>>1;
}
} int result(int x)
{
dec_to_bin(x); int i,j;
int sum=0; for(i=0;i<bin[0]-1;i++)
{
for(j=i/2+1;j<=i;j++)
{
sum +=c[i][j];
}
} int zero=0; for(i=bin[0]-1;i>=1;i--)
{
if(bin[i])
{
for(j=(bin[0]+1)/2-(zero+1);j<=i-1;j++)
sum += c[i-1][j];
}
else
zero++;
}
return sum;
}
int main()
{
init();
int a,b;
cin>>a>>b; cout<<result(b+1)-result(a)<<endl; return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3252:Round Numbers的更多相关文章

  1. POJ - 3252 A - Round Numbers

    The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' ...

  2. POJ 1320:Street Numbers

    Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2753   Accepted: 1530 De ...

  3. POJ 1142:Smith Numbers(分解质因数)

                                   Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  4. POJ 3252 Round Numbers(组合)

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

  5. [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)

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

  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

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

随机推荐

  1. Android之收音机UI实现(转)

    源码: http://www.2cto.com/kf/201211/171417.html 最近在研究收音机的源码,本来想把收音机从源码中提取出来,做成一个单独的应用,但是,收音机需要底层的支持,所以 ...

  2. Linux centosVMware shell编程 for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本

    一.for循环 语法:for 变量名 in 条件; do …; done 案例1 #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] ec ...

  3. CSS文本居中显示

    因为一直为元素居中问题而困扰,所以决定把自己遇到和看到的方法记录下来,以便以后查看 如果要让inline或inline-block元素居中显示,则父元素css中包含text-align:center; ...

  4. 学习:Android框架

      引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWorld Android 开发之旅:HelloWorld项目的目录结构 我 们对android有了个大致的了解,知道如何搭建a ...

  5. delphi中的pansichar和pchar等类型的区别

    varc: Char; {Char 类型的取值范围是: #0..#255, 用十六进制表示是: #$0..#$FF}begin{用十进制方式赋值:}c := #65;ShowMessage(c); { ...

  6. linux下的文件操作

    彻底删除文件 rm -rf + [文件目录 可相对可绝对] 是彻底删除而且linux无回收站 创建文件 touch + [文件名] 创建文件夹 mkdir + [文件夹名] 文件提权:chmod 77 ...

  7. 认识系统服务 (daemons)

      daemon(守护进程:后台程序)与服务:   系统为了某些功能必须要提供一些服务 (不论是系统本身还是网络方面),这个服务就称为 service .但是 service 的提供总是需要程序的运作 ...

  8. 第2节 storm路由器项目开发:8 - 9、集群监控软件ganglia的安装和使用

    Ganglia监控Hadoop集群的安装部署 详情请参见 http://boendev.iteye.com/blog/1750615 一. 安装环境 CentOS6.5x86_64 安装gmetad的 ...

  9. SpringCloud学习之Zuul路由转发、拦截和熔断处理(七)

    Spring Cloud Zuul 服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能. Sp ...

  10. luogu P2762 太空飞行计划问题

    好像是最大权闭合图,也就是最大流最小割啦,找出最大流的路径输出,这题如何建模呢,一样的先设源点和汇点,源点向每个计划连capacity为赞助数的边,每个计划连相应装置capacity为无穷的边,每个装 ...