spoj SORTBIT - Sorted bit squence
Let's consider the 32 bit representation of all integers i from m up to n inclusive (m ≤ i ≤ n; m × n ≥ 0, -2^31 ≤ m ≤ n ≤ 2^31-1). Note that a negative number is represented in 32 bit Additional Code. That is the 32 bit sequence, the binary sum of which and the 32 bit representation of the corresponding positive number is 2^32 (1 0000 0000 0000 0000 0000 0000 0000 0000 in binary).
For example, the 32 bit representation of 6 is 0000 0000 0000 0000 0000 0000 0000 0110
and the 32 bit representation of -6 is 1111 1111 1111 1111 1111 1111 1111 1010
because
0000 0000 0000 0000 0000 0000 0000 0110 (6)
+
1111 1111 1111 1111 1111 1111 1111 1010 (-6)
-------------------------------------------------
= 1 0000 0000 0000 0000 0000 0000 0000 0000 (2^32)
Let's sort the 32 bit representations of these numbers in increasing order of the number of bit 1. If two 32 bit representations that have the same number of bit 1, they are sorted in lexicographical order.
For example, with m = 0 and n = 5, the result of the sorting will be
No. |
Decimal number |
Binary 32 bit representation |
1 |
0 |
0000 0000 0000 0000 0000 0000 0000 0000 |
2 |
1 |
0000 0000 0000 0000 0000 0000 0000 0001 |
3 |
2 |
0000 0000 0000 0000 0000 0000 0000 0010 |
4 |
4 |
0000 0000 0000 0000 0000 0000 0000 0100 |
5 |
3 |
0000 0000 0000 0000 0000 0000 0000 0011 |
6 |
5 |
0000 0000 0000 0000 0000 0000 0000 0101 |
with m = -5 and n = -2, the result of the sorting will be
No. |
Decimal number |
Binary 32 bit representation |
1 |
-4 |
1111 1111 1111 1111 1111 1111 1111 1100 |
2 |
-5 |
1111 1111 1111 1111 1111 1111 1111 1011 |
3 |
-3 |
1111 1111 1111 1111 1111 1111 1111 1101 |
4 |
-2 |
1111 1111 1111 1111 1111 1111 1111 1110 |
Given m, n and k (1 ≤ k ≤ min{n − m + 1, 2 147 473 547}), your task is to write a program to find a number corresponding to k-th representation in the sorted sequence.
Input
The input consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 1000. The following lines describe the data sets.
For each data set, the only line contains 3 integers m, n and k separated by space.
Output
For each data set, write in one line the k-th number of the sorted numbers.
Example
Sample input:
2
0 5 3
-5 -2 2
Sample output:
2
-5
题解:
我们首先考虑 m、n 同正的情况。
由于排序的第一关键字是 1 的数量,第二关键字是数的大小, 因此我们很容易确定答案
中 1 的个数:依次统计区间[m,n]内二进制表示中含 1 的数量为 0,1,2,...的数,直到累加的答
案超过 k,则当前值就是答案含 1 的个数,假设是 s。利用例一的算法可以解决这个问题。
同时,我们也求出了答案是第几个[m,n]中含 s 个 1 的数。因此,只需二分答案,求出[m,ans]
中含 s 个 1 的数的个数进行判断即可。
由于每次询问的复杂度为 O(log(n)),故二分的复杂度为 O(log 2 (n)),这同时也是预处理
的复杂度,因此此算法较为理想。
m<0 的情况,也不难处理,我们只要忽略所有数的最高位,求出答案后再将最高位赋回
1 即可。或者也可以直接将负数视为 32 位无符号数,采用同正数一样的处理方法。两种方
法都需要特别处理 n=0 的情况。
code:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void read(unsigned int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
typedef long long int64;
const int maxn=;
const int64 inf=1LL<<;
int T,a,b,s,l,r,m,tmp[maxn];
unsigned int k,x,y,c[maxn][maxn];
void init(){
for (int i=;i<=;i++) c[i][]=;
for (int i=;i<=;i++) for (int j=;j<=i;j++) c[i][j]=c[i-][j-]+c[i-][j];
}
unsigned int calc(int64 n,int k){
int64 t=n;
int len=,res=;
unsigned int ans=;
memset(tmp,,sizeof(tmp));
while (t) tmp[++len]=t&,t>>=;
for (int i=len;i;i--){
if (tmp[i]) ans+=c[i-][k-res],res++;
if (res>k) break;
}
return ans;
}
unsigned int query(unsigned int l,unsigned int r,int s){
if (l<=r) return calc(r+1LL,s)-calc(l,s);
else return calc(inf,s)-calc(l,s)+calc(r+1LL,s);
}
int main(){
init();
for (read(T);T;T--){
read(a),read(b),read(k);
for (s=;s<=;s++){
unsigned int res=query(a,b,s);
if (k>res) k-=res; else break;
}
l=a,r=b;
while (l!=r){
m=(0LL+l+r)>>;
unsigned int res=query(a,m,s);
if (k>res) l=m+; else r=m;
}
printf("%d\n",l);
}
return ;
}
spoj SORTBIT - Sorted bit squence的更多相关文章
- SPOJ SORTBIT Sorted bit squence (数位DP,入门)
题意: 给出一个范围[m,n],按照二进制表示中的1的个数从小到大排序,若1的个数相同,则按照十进制大小排序.求排序后的第k个数.注意:m*n>=0. 思路: 也是看论文的.一开始也能想到是这种 ...
- 【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)
SORTBIT - Sorted bit squence no tags Let's consider the 32 bit representation of all integers i from ...
- SPOJ 1182 Sorted bit sequence
题目链接 题意: 分析: 其实如果会了Ural 1057. Amount of Degrees那道题目,这道题自然也就会了... 我们考虑枚举第$k$个数字的$1$的个数,那么我们需要计算的也就是区间 ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
- [转] POJ数学问题
转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...
- ACM数学
1.burnside定理,polya计数法 这个专题我单独写了个小结,大家可以简单参考一下:polya 计数法,burnside定理小结 2.置换,置换的运算 置换的概念还是比较好理解的,< ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- 【专题】数位DP
[资料] ★记忆化搜索:数位dp总结 之 从入门到模板 by wust_wenhao 论文:浅谈数位类统计问题 数位计数问题解法研究 [记忆化搜索] 数位:数字从低位到高位依次为0~len-1. 高位 ...
- 【SPOJ】1182 Sorted bit sequence
[算法]数位DP [题解]动态规划 写了预处理函数却忘了调用是一种怎样的体验? #include<cstdio> #include<cstring> #include<a ...
随机推荐
- php中strlen和{}的效率对比
很少有人知道{}用来判断字符串长度 今天试试 发现好像没有strlen快
- Mina学习之与Spring整合
本章中演示在Spring中整合Mina,为了整合到Spring,需要做以下几个步骤: 1. 设置IoHandler <bean id="trapHandler" class= ...
- JVM类载入过程及主动引用与被动引用
了解类载入全过程,有助于了解JVM执行过程,以及更深入了解java动态性(解热部署,动态载入),提高程序灵活性. 类载入全过程: JVM将class文件字节码文件载入到内存中.并对数据进行校验解析和初 ...
- Linux Resin 安装
1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...
- 在32位Windows 8或Windows 7上安装Windows Phone 8 SDK
这需要一点技巧的,其实WPSDK8是支持32位系统的,可因为微软脑残的限制,安装方法如下: 这里必需要有一个工具,OllyDbg 2.0,去官网下载即可. 启动OD,在菜单打开选项窗口,在Events ...
- 为Android GridView 设置行背景
经常有这样的需求,你的功能图标要像一个个物品,摆放在书架上,像这样: 我的思路比较简单,重载GridView,在他绘制子视图前,先把背景绘制完成 1 2 3 4 5 6 7 8 9 10 11 12 ...
- 理解ArcGIS Javascript Viewer Widget及编程模型
一个ArcGIS Javascript Viewer for JavaScript Widget是一组可以共享.迁移及部署到JavaScript View程序中的的文本文件.通常,一个程序员如果要开发 ...
- (转载)Java 1.7的新特性
网上找了一下,整理了贴出来. 下面是Java 1.7 里新出现的功能的一些用法例子: 对集合的支持 摒弃了 Java 集合接口的实现类,如: ArrayList . HashSet 和 HashMap ...
- wpf提示背景,资源样式
查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...
- Bash远程文件传输命令scp
备份远程文件(远程——>本地) scp -r 远程用户名@ip:文件绝对路径 本地绝对路径 还原远程文件(本地——>远程) scp -r 本地路径 远程用户名@ip:远程绝对路径 如果SS ...