poj3252-Round Number 组合数学
题目:
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 8492 | Accepted: 2963 |
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
思路基本上和网上http://zhyu.me/acm/poj-3252.html做的非常类似:
举例说明,
[2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1]
即:Rn[start,finish]=Rn[0,finish]-Rn[0,start-1]
所以关键是给定一个X,求出Rn[0,X]
如今如果X=10100100
这个X的二进制总共是8位,不论什么一个小于8位的二进制都小于X
第一部分。求出长度为[0,7]区间内的二进制是RoundNumber的个数
对于一个长度为Len的二进制(最高位为1),怎样求出他的RoundNumbers呢(如果为用R(len)来表达)。分为奇数和偶数两种情况
1、奇数情况:在Len=2k+1的情况下,最高位为1。剩下2k位,至少须要k+1为0
用C(m,n)表示排列组合数:从m个位置选出n个位置的方法
R(len)=C(2k,k+1)+C(2k,k+2)+...+C(2k,2k).
因为 A:C(2k,0)+C(2k,1)+...+C(2k,2k)=2^(2k)
B:C(2k,0)=C(2k,2k), C(2k,1)=C(2k,2k-1) ,,C(2k,i)=C(2k,2k-i)
于是 C(2k,0)+C(2k,1)+...+C(2k,2k)
= C(2k,0)+C(2k,1)+...+C(2k,k)+C(2k,k+1)+C(2k,K+2)+...+C(2k,2k)
= 2*R(len)+C(2k,k)
=2^(2k)
所以R(len)=1/2*{2^(2k)-C(2k,k)};
2. 偶数情况 len=2*k,类似能够推到 R(len)=1/2*(2^(2k-1));
第二部分,对于上面这个长度为8的样例:即X=10100100,首先假设本身是RoundNumbers,第二部分的结果总数+1
第一部分已经将长度小于8的部分求出。如今要求长度=8的RoundNumber数目
长度为8,所以第一个1不可改变
如今到第二个1,假设Y是前缀如100*****的二进制。这个前缀下。后面取0和1必定小于X,已经有2个0,一个1,剩下的5个数字中至少须要2个0,
所以把第二个1改为0:能够有C(5,2)+C(5,3)+C(5,4)+C(5,5)
如今第三个1,也就是前最为101000**。相同求出,至少须要0个0就可,所以有C(2,0)+C(2,1)+C(2,2)个RoundNumbers
。。。
将所有除了第一个1以外的1所有变为0,如上算出有多少个RoundNumbers,结果相加(因为前缀不一样。所以后面无论怎么组合都是唯一的)
将第一部分和第二部分的结果相加。就是最后的结果了。
唯一特别须要注意的是在计算组合数的时候非常easy越界。尽管上面分析了计算结果在int范围内是没有问题的,可是计算组合数中间过程还是非常可能越界,所以这里要特别注意。
解决方法是利用C(n,m)=C(n-1,m-1)+C(n-1,m)进行递归计算,而不是使用传统的乘法计算方式。为了更有效率一点,能够事先计算好n=1~32,m=1~32的组合数的结果然后存起来。
import java.util.*;
public class Combinatorics_RoundNumbers3252 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Init();
while(in.hasNext())
{
int a=in.nextInt();
int b=in.nextInt();
//System.out.println(roundNumber(a-1)+" " +roundNumber(b));
System.out.println(roundNumber(b)-roundNumber(a-1));
}
}
static int c[][]=new int[35][35];
public static void Init(){
for(int i=0;i<33;i++){
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}
}
public static int roundNumber(int value)
{
char b[]=toBinary(value);
int sum=0;
for(int len=1;len<b.length;len++)
{
for(int j=(len+1)/2;j<len;j++)
sum+=c[len-1][j];
}
int zeros=0;
for(int i=1;i<b.length;i++)
{
if(b[i]=='1')
{
int k=(b.length+1)/2;
int m=Math.max(0, k-(zeros+1));
int n=b.length-i-1;
for(int j=n;j>=m;j--)
sum+=c[n][j];
}
else
{
zeros++;
}
}
if(2*zeros>=b.length)
sum++;
return sum;
}
private static char[] toBinary(int value) {
return Integer.toBinaryString(value).toCharArray();
}
/*public static int roundNumberOfLength(int len)
{
int k=len/2;
if(len%2==0)
{
return (1<<(len-2));
}
else
{
return ((1<<(len-1))-choose(len-1,k))/2;
}
}*/
public static int choose(int n, int m) {
if(n==0)
return 0;
if(m==0||m==n)
return 1;
if(m>n)
return 0;
return choose(n-1,m-1)+choose(n-1,m);
}
}
poj3252-Round Number 组合数学的更多相关文章
- POJ3252——Round Number(组合数学)
Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...
- [poj3252]Round Numbers_数位dp
Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...
- POJ 3252 Round Numbers 组合数学
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13381 Accepted: 5208 Description The ...
- Round Numbers(组合数学)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10484 Accepted: 3831 Descri ...
- poj3252 Round Numbers
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7625 Accepted: 2625 Des ...
- POJ 3252 Round Number(数位DP)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6983 Accepted: 2384 Des ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- poj3252 Round Numbers(数位dp)
题目传送门 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16439 Accepted: 6 ...
随机推荐
- vue路由传值params和query的区别
vue路由传值params和query的区别1.query传参和接收参数传参: this.$router.push({ path:'/xxx' query:{ id:id } })接收参数: this ...
- POI 详细介绍
Apache POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.目前POI已经有了Ruby版本. 结构: HSSF - 提供读写Microsoft Excel XLS格式 ...
- jvm 堆、栈 、方法区概念和联系
一.三者联系 1.堆:解决数据的存储问题( 即 数据怎么放,放到哪 ). 2.栈:解决程序运行的问题( 即 程序如何执行,或者说如何处理数据 ). 3.方法区:辅助堆栈的一块永久区,解决堆栈信息的产生 ...
- Objective-C基础笔记(3)OC的内存管理
Objective-C的内存基本管理 在OC中每一个变量都保存着引用计数器,当这个对象的引用计数器为0的时候该对象会被回收.当使用alloc.new或者copy创建一个对象的时候,对象的引用计数器被置 ...
- ANSI编码
ANSI编码 ANSI简介 不同的国家和地区制定了不同的标准,由此产生了 GB2312.GBK.Big5.Shift_JIS 等各自的编码标准.这些使用 1 至 4 个字节来代表一个字符的各种汉字延伸 ...
- cximage功能简介
CxImage是一个可以用于MFC 的C++图像处理类库类,它可以打开,保存,显示,转换各种常见格式的图像文件,比如BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, ...
- Redis-2-对于key的通用操作
Redis-2-对于key的通用操作 标签(空格分隔): redis del key key1 key2 作用: 删除1个或多个键 返回值: 不存在的key忽略掉,返回真正删除的key的数量 rena ...
- jQuery学习(三)——选择器总结
1.基本选择器 id选择器:$(“#id名称”); 元素选择器:$(“元素名称”); 类选择器:$(“.类名”); 通配符:* 多个选择器共用(并集) 案例代码: <!DOCTYPE html& ...
- NEON基本知识
http://blog.csdn.net/EmSoftEn/article/details/51834171 http://blog.csdn.net/yxnyxnyxnyxnyxn/article/ ...
- POJ 3067 Japan 【 树状数组 】
题意:左边有n个城市,右边有m个城市,现在修k条路,问会形成多少个交点 先按照x从小到大排,x相同的话,则按照y从小到大排,然后对于每一个y统计前面有多少个y比它大,它们就一定会相交 另外要用long ...