Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7558   Accepted: 2596

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 Output6


题意:给两个数Start 和 Finish,求介于这两个数之间的二进制表示满足0的个数不大于1的个数的整数个数; 思路:另[0,X]表示0到x之间满足题意的正数的个数,则该题即为求[0,Finish]-[0,Start-1]; [0,X]表示0到x之间满足题意的正数的个数求法:(令num[len]表示长度为len的满足题意的整数个数,c[n][m]表示从n个位置中选出m个位置)(假设x的二进制为1010 0100,其长度是8) 1> 二进制形式长度小于8的肯定小于x,假设长度为len(len < 8) 若len是奇数,len=2*k+1;因第一位都是1,在剩余的2*k位中,0的个数至少是k+1,
则num[len] = c[2k][k+1]+c[2k][k+2]+.....+c[2k][2k];
又因为c[2k][0]+c[2k][1]+c[2k][2]+....+c[2k][2k] = 2^2k,
且c[2k][0]=c[2k][2k],c[2k][1]=c[2k][2k-1]...c[2k][k-1]=c[2k][k+1];推导得num[len]=(2^2k-c[2k][k])/2;
同理,若len是偶数,num[len]=2^(2k-1)/2; 2> 二进制形式长度等于8时,当把除了第一个1之外的1依次变为0得到的数肯定小于x;例如1010 0100变为前缀是100的肯定小于1010 0100,
此时0有两个,在后面的5个数中,0至少有2个,所以共有c[5][2]+c[5][3]+c[5][4]+c[5][5]个;1010 0100还可以变为前缀是1010 00的,
这时0有4个,在后面的两个数中,至少有0个0,共有c[2][0]+c[2][1]+c[2][2]个;还要注意若x本身满足题意,计数器再加1;

 #include<stdio.h>
#include<string.h>
const int MS = ;
using namespace std;
int power2[MS],c[MS][MS];
int Binary[MS]; int RoundNumber(int x)
{
memset(Binary,,sizeof(Binary));
if(x <= ) return ;
int len,i;
int number = ;
int num_1,num_0;//记录二进制中1和0的个数; num_1 = ,num_0 = ;
int tmp = x,cnt = ; while(tmp)//将x转化为二进制,其长度为cnt;
{
int t =tmp%;
Binary[cnt++] = t;
tmp = tmp/; if(t == )
num_1++;
else num_0++; } //求长度小于cnt的roundnumber数;
for(len = ; len <= cnt-; len++)
{
if(len%==)
number += ((power2[len-]-c[len-][(len-)/])>>);
else number += (power2[len-]>>);
} //求长度等于cnt的roundnumber数;
if(num_1 <= num_0)
number ++; num_1 = ,num_0 = ;
for(i = cnt-; i >= ; i--)
{
if(Binary[i] == )
{
for(int j = i; j >=&& j+num_0+ >= i-j+num_1; j--)
number += c[i][j];
num_1++;
}
else num_0++;
}
return number;
} int main()
{
int n,m;
for(int i = ; i < MS; i++)
{
c[i][] = ;
c[i][i] = ;
power2[i] = (<<i);
} for(int i = ; i < MS; i++)
{
for(int j = ; j < i; j++)
{
c[i][j] = c[i-][j-] + c[i-][j];
}
}
scanf("%d %d",&n,&m);
int ans = RoundNumber(m) - RoundNumber(n-);
printf("%d\n",ans); return ; }

 

Round Numbers (排列组合)的更多相关文章

  1. POJ 3252 Round Numbers(组合)

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

  2. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  3. light oj 1095 - Arrange the Numbers排列组合(错排列)

    1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...

  4. poj 1715 Hexadecimal Numbers 排列组合

    /** 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(1 ...

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

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

  6. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  7. Round Numbers(组合数学)

    Round Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  8. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  9. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

随机推荐

  1. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  2. Java基础知识强化之集合框架笔记37:用户登录注册案例

    1. 登录注册案例分析图解: 2. 用户登录案例 详细分析 和 分包实现: (1)用户登录案例详细分析(面向对象思想) 按照如下的操作,可以让我们更符合面向对象思想: • 有哪些类呢?         ...

  3. Eclipse下使用Fat Jar插件对源代码进行打包

    这两天需要对一个项目进行打包,并在服务器上部署成后台服务模式进行执行,原来使用eclipse进行打包很难用,配置文件容易出错,生成的jar不能正常运行.后来发现Fat Jar Eclipse Plug ...

  4. mssql 创建触发器

    MS-SMS里创建触发器: 首先右击表内的触发器文件夹图标 然后输入触发器创建指令,一下案例:(添加创建时间) 创建: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFI ...

  5. ASP.NET中如何生成图形验证码

    通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3 ...

  6. JS中的事件绑定,事件捕获,事件冒泡以及事件委托,兼容IE

    转载请注明出处:http://www.cnblogs.com/zhangmingze/p/4864367.html   ● 事件分为三个阶段:   事件捕获 -->  事件目标 -->   ...

  7. java 基本类型和包装类的比较

    public class BoxingTest { @Test public void test1(){ String a = new String("1"); String b ...

  8. HTML5 文件域+FileReader 分段读取文件并上传到服务器(六)

    说明:使用Ajax方式上传,文件不能过大,最好小于三四百兆,因为过多的连续Ajax请求会使后台崩溃,获取InputStream中数据会为空,尤其在Google浏览器测试过程中. 1.简单分段读取文件为 ...

  9. WCF,WebAPI,WCFREST和WebService的区别

    Web ServiceIt is based on SOAP and return data in XML form.It support only HTTP protocol.It is not o ...

  10. C#.net时间戳转换

    //long ticks = (DateTime.Parse(DateTime.Now.ToString(CultureInfo.InvariantCulture)).ToUniversalTime( ...