【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 是否含有 ...
随机推荐
- Linux Bash终端快捷键小结
Ctrl + A 定位至行首 Ctrl + E 定位至行尾 Ctrl + U 向前删除至行首 Ctrl + K 向后删除至行尾 Ctrl + L 清屏
- CAS 单点登录,通过ticket 获取登录用户
string url =SSOValidate+"?service=" + WebValidate + "&ticket=" + Ticket + &q ...
- android使用广播退出应用程序
由于在(Widget或Service.BroadcastReceiver中)使用startActivity()方法启动activity时需使用FLAG_ACTIVITY_NEW_TASK flag,所 ...
- DDX_Text (MFC)
DDX_Text (MFC) 描述:该DDX_Text功能管理int的转移,UINT,long,DWORD,CString,float, 或 double编辑控件之间的数据在对话框中,表单视图或控制视 ...
- javascript调用oc的方法
1.引入#import <JavaScriptCore/JavaScriptCore.h> 2.JSContext *jsContext = [self.webView valueForK ...
- C#微信开发之旅--基本信息的回复
上一篇说到配置和验证<C#微信开发之旅--准备阶段> 下面来实现一下简单的信息回复. 也就是接收XML,返回XML 可以去看下微信开发文档的说明:http://mp.weixin.qq.c ...
- sharepoint2013 新建母板页 新建页面布局 关联母板页和页面布局
1 母板页的应用和layout(页面布局)的创建和应用 母板页上传:将准备好的html和样式 通过spd中的导入方式导入模版html, 导入后: 然后在网站设置中进行转换为母板页. 随后编辑 ...
- Java教程——CMD手动编译运行失败原因(高手略过)
(仅对新手,高手略过)在学习Java初期,我们在利用cmd手动编译java程序的时候,会遇到编译成功,但运行却总是提示失败.已经排除了java配置环境的问题,Path和ClassPath以及%JAVA ...
- ubuntu fcitx 安装 使用
系统内置的ibus的86五笔,感觉有些老不太好用, 所以安装试用了一下fcitx下的五笔,记录一下安装方法 ,各种搜索... 我的ubuntn版本: #48-Ubuntu SMP Fri Aug 24 ...
- 测试php页面执行代码时间
//生命一个计算脚本运行时间的类 class Timer { private $startTime = 0; //保存脚本开始执行时的时间(以微秒的形式保存) private $stopTime = ...