Round Numbers (排列组合)
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
Output
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 (排列组合)的更多相关文章
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- light oj 1095 - Arrange the Numbers排列组合(错排列)
1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...
- poj 1715 Hexadecimal Numbers 排列组合
/** 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(1 ...
- [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8590 Accepted: 3003 Des ...
- 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 ...
- Round Numbers(组合数学)
Round Numbers Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
随机推荐
- linux共享windows资料
linux 只有NTFS格式不能访问,其的都可以.1.用fdisk -l 查看分区表.2.然后用mount -t vfat /mnt/hda1 /dev/hda1 就可以了./mnt/hda1是一普通 ...
- Qapp使用总结
QApp构建项目总结 1.view module 区别
- android学习笔记----JNI中的c控制java
面向对象的底层实现 java作为面向对象高级语言,可对现实世界进行建模.和面向过程不同的是面向对象软件的编写不是流程的堆积,而是对业务逻辑的多视角分解和分类.其过程大致为: 1).将知识分解 ...
- AS【常用插件】
安装插件,Settings -->[Plugins]-->搜索-->点击install-->重启AS 禁用插件,右侧面板会显示出已经安装的插件列表,取消勾选即可禁用插件 AS插 ...
- Node.js + Express + Mongodb 开发搭建个人网站(一)
一.Node + Express环境搭建 0.去Node官网下载安装node,如果安装了 npm 和 node的话 那么就 安装 全局的 express,-g全局安装 npm install expr ...
- .net 安装Swagger
官网:http://swagger.io/ 教程:http://www.wmpratt.com/swagger-and-asp-net-web-api-part-1/ 1:安装Dll: https:/ ...
- java异常类的使用
1.异常的概念 什么是异常?程序出错分为两部分,编译时出粗和运行时出错.编译时出错是编译器在编译源码时发生的错误: 运行时出错是在编译通过,在运行时出现的错误.这种情况叫异常. 例如:数组越界,除数为 ...
- php验证是否为手机端还是PC
<?php $forasp = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($forasp,'mobile')==true) { ech ...
- 数据库连接报错之IO异常(The Network Adapter could not establish the connection)
Io 异常: The Network Adapter could not establish the connection 有以下四个原因: 1.oracle配置 listener.ora 和tnsn ...
- Unity Manual 用户手册
unity3d 文档的中文网址: http://game.ceeger.com/Manual/