【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 是否含有 ...
随机推荐
- 职场PPT达人装酷的13条秘诀
对<说服力-让你的PPT会说话>读者调查显示,88.8%的白领认为“做出漂亮的幻灯片对晋升有帮助”,99.9%的白领一致认为职场装酷神器排行榜第一位是PPT,甚至有位程序员说哥最牛的编程环 ...
- java与.net平台之间进行RSA加密验证
RSA加密算法虽然不分平台,标准都是一样的,但是各个平台的实现方式都不尽相同,下面来我来说说java与.net平台之间该如何进行RSA加密验证,即java端加密->.net端验证和.net端加密 ...
- 重温css系列01
2016-01-07——解决背景层透明度的问题 需要ie9+ 问题:如果我对div设置opacity: 0.8;这个透明属性后 希望内容不发生改变怎么弄? A:做两层,或者rgba 解决后的效果图: ...
- 将分页功能从JSP页面中独立出来
附带视频链接:http://www.tudou.com/programs/view/leaQ-YFl7W8/?bid=03&pid=2&resourceId=0_03_05_02
- 创建自定义 HTTP 模块
本主题中描述的自定义 HTTP 模块阐释了 HTTP 模块的基本功能.在响应下面两个事件时调用该模块:BeginRequest 事件和 EndRequest 事件.这使该模块可以在处理页请求之前和之后 ...
- java基础加强
一.泛型 Generic 1.集合泛型: 在没有泛型之前,集合中存入的数据,类型就会丢失掉,在取出数据时,需要做强制类型转换,就有转换失败的风险,而这种风险,在编译阶段是没有办法检查出来的 引入泛型后 ...
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
- php session_id()函数介绍及代码实例
session_id()功能: 获取设置当前回话ID. 函数说明: string session_id ([ string $id ] ) 参数: 如果指定了参数$id,那么函数会替换当前的回话id. ...
- (一)JAVA使用POI操作excel
1,Poi 简介 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能 PO ...
- WKWebView无法(通过URL schemes)跳转到其他App
Custom scheme URL 在WKWebView中默认是不支持的 (但Safari可以). 我们可以通过NSError来进行一些处理从而使得程序可以正常跳转: func webView(web ...