【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)
SORTBIT - Sorted bit squence
no tagsLet'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
我的天哪!感觉这道题做了100年,啊负数搞死我了!!
我觉得不用二分,又不是很大,然后先算出是多少个1,再一边统计一边填数。
原始的代码风格233~
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long int f[][],g[][]; void init()
{
memset(f,,sizeof(f));
f[][]=;
for(int i=;i<=;i++)
{
for(int j=;j<i;j++)
{
f[i][j+]+=f[i-][j];
f[i][j]+=f[i-][j];
}
}
} void get_g(LL x,int q)
{
if(x==-) return;
int y=;
for(int i=;i>=;i--)
{
for(int j=y;j<=i+y;j++)
{
if(x&(<<i-)) g[q][j]+=f[i-][j-y];
}
if(x&(<<i-)) y++;
}
g[q][y]++;
g[q][]=;
} LL ffind(LL x,LL y,LL k)
{
LL ans=;
bool flag=;
for(int i=;i>=;i--)
{
if( ( (x&(<<i-))||!flag)&&f[i-][y]<k)
{
ans+=1LL<<i-;
k-=f[i-][y];
y--;
}
else if((x>>i-)&) flag=;
}
return ans;
} int main()
{
init();
int T;
scanf("%d",&T);
LL mx=1LL<<;
while(T--)
{
bool q=;
LL m,n,k,t;
scanf("%lld%lld%lld",&m,&n,&k);
if(m<) m=mx+m,n=mx+n,q=;
memset(g,,sizeof(g));
get_g(n,);get_g(m-,);
int st,h=;
for(st=;st<=;st++)
{
if(h+g[][st]-g[][st]>=k) break;
h+=g[][st]-g[][st];
}
LL ans=ffind(n,st,k-h+g[][st]);
if(q) ans=ans-mx;
printf("%lld\n",ans);
}
return ;
}
[SPOJ 1182]
这种题是数位DP里面我最熟悉的了,接下来要来一题厉害的高精度!!
2016-10-10 21:02:10
【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)的更多相关文章
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
- SPOJ SORTBIT Sorted bit squence (数位DP,入门)
题意: 给出一个范围[m,n],按照二进制表示中的1的个数从小到大排序,若1的个数相同,则按照十进制大小排序.求排序后的第k个数.注意:m*n>=0. 思路: 也是看论文的.一开始也能想到是这种 ...
- 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 × ...
- [spoj1182][Sorted Bit Sequence] (数位dp)
Description Let's consider the 32 bit representation of all integers i from m up to n inclusive (m ≤ ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- SPOJ KPSUM ★(数位DP)
题意 将1~N(1<=N<=10^15)写在纸上,然后在相邻的数字间交替插入+和-,求最后的结果.例如当N为12时,答案为:+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2=5 ...
- 数位DP:SPOJ KPSUM - The Sum
KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...
- [数位dp] spoj 10738 Ra-One Numbers
题意:给定x.y.为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 样例: 10=1-0=1 所以10是这种数 思路:数位dp[i][sum][ok] i位和为sum 是否含有 ...
随机推荐
- 在AngularJS的controller外部直接获取$scope
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5560843.html ...
- selendroid项目实战教程1
selendroid是国内使用非常少的框架.资料也少.刚好公司项目用到,给大家分享下,技术不太行,有错误还望指正. 使用selendroid契机,是公司开发的APP,需要大量捕捉Toast信息.公司的 ...
- MySQL基本查询语句
创建一张表 create table user ( id ) not null, name ) not null, birthDate date not null, gender ) not null ...
- Redis 集群常见问题
Redis集群相关问题 1:远程连接问题 远程连接保护模式下,需要做一些配置.
- 16Aspx.com源码2014年7月详细
Web电子商务网(三层)V2.0源码 2014-07-31 [VS2010] 源码介绍: Web电子商务网(三层)V2.0源码 源码描述: 一.源码特点 采用三层架构开发, ...
- Gerrit 删除项目
今天手滑把一个Gerrit上的项目epa写成了epp,想找个重命名的地方也找不到...到网络上搜索了下,发现都是改数据库的,然后就进入的数据库: $ ssh -p 29418 10.27.149.22 ...
- Oracle 11g gateways(透明网关)配置
配置要点主要有三点: 1.%GATEWAYS_HOME%(透明网关安装目录)\dg4msql\admin\initdg4msql.ora 内容: HS_FDS_CONNECT_INFO=localho ...
- 500 OOPS: cannot change directory:/home/test
问题: 以root 从远程客户端 登录 FTP 一直密码错误. 发现不能以root 登录, 需要创建其它的用户. 创建一个test 用户后(如下): useradd test; passwd ...
- WPF TreeView递归遍历相关方法
/// <summary> /// 递归改变组织树选中状态. /// </summary> /// <param name="org">< ...
- WPF动画之线性插值动画(1)
XAML代码: <Window x:Class="线性插值动画.MainWindow" xmlns="http://schemas.microsoft.com/wi ...