Description

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:


- - 

Sample output:

- 

Solution

完了,一道简单题调了3个小时

论文:http://wenku.baidu.com/link?url=FrOOQ0uY5RDizsTypIHewuCFzdQxSpets-J5cUpu_h3NBTxn-s3BMcQhgnQYTdrqV7XTBbDgU-HKNUmt-BbhDx_dNcR4v0ZMBZfs_Fnfjai

#include<stdio.h>
inline int Rin(){
int x=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
int f[][];
void init(){
int i,j;
f[][]=;
for(i=;i<=;i++){
f[i][]=f[i][i]=;
for(j=;j<i;j++)
f[i][j]=f[i-][j-]+f[i-][j];
}
}
int cal(int x,int k){
int cnt=,ans=,i;
for(i=;i;i--){
if(x&(<<i)){
cnt++;
if(cnt>k)break;
x^=(<<i);
}
if((<<(i-))<=x)
ans+=f[i-][k-cnt];
}
if(cnt+x==k)ans++;
return ans;
}
int solve(int x,int y,int k){
int i,cnt=;
for(i=;i<=;i++){
cnt=cal(y,i)-cal(x-,i);
if(k<=cnt)break;
k-=cnt;
}
int l=x,r=y,mid,ans=;
while(l<=r){
mid=l+r>>;
if(cal(mid,i)-cal(x-,i)<k)
l=mid+;
else
ans=mid,r=mid-;
}
return ans;
}
int main(){
init();
int T=Rin(),n,m,K;
while(T--){
m=Rin(),n=Rin(),K=Rin();
if(!m && !n)puts("");
else
if(!m){
K--,m=;
if(!K)puts("");
else printf("%d\n",solve(m,n,K));
}
else if(m>)printf("%d\n",solve(m,n,K));
else if(!n){
K--,n=-;
if(!K)puts("");
else printf("%d\n",(<<)|solve(m,n,K));
}
else printf("%d\n",(<<)|solve(m,n,K));
}
getchar();getchar();
return ;
}

[spoj1182][Sorted Bit Sequence] (数位dp)的更多相关文章

  1. HDU 2062 Subset sequence 数位dp,思路 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2062 Subset sequence Time Limit: 1000/1000 MS (Java/Others ...

  2. 【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 ...

  3. 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)

    BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...

  4. 2018牛客网暑假ACM多校训练赛(第四场)C Chiaki Sequence Reloaded (组合+计数) 或 数位dp

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round4-C.html 题目传送门 - https://www.no ...

  5. [DP]数位DP总结

     数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step   http://blog.csdn.net/dslovemz/article/details/ ...

  6. 【专题】数位DP

    [资料] ★记忆化搜索:数位dp总结 之 从入门到模板 by wust_wenhao 论文:浅谈数位类统计问题 数位计数问题解法研究 [记忆化搜索] 数位:数字从低位到高位依次为0~len-1. 高位 ...

  7. hdu3555 数位dp

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  8. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. ZOJ 3494 BCD Code(AC自动机+数位DP)

    BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...

随机推荐

  1. openssh常用命令记录

    command description date ssh [user@]hostname[:port] 登录远程机器 2017-03-21 scp <local_file> <use ...

  2. Eclipse导入Java 的jar包的方法

    打开eclipse1.右击要导入jar包的项目,点properties 2.左边选择java build path,右边选择libraries 3.选择add External jars 4.选择ja ...

  3. Rails5 radio_button

    容易错,集中记下来 首先是radio button的三种形式  函数名  参数意义  radio_button_tag(prop, value [, opts])  prop: radio的属性  v ...

  4. [Swift]Array(数组)扩展

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. RabbitMQ的一些基本操作

    $ sudo chkconfig rabbitmq-server on # 添加开机启动RabbitMQ服务 $ sudo /sbin/service rabbitmq-server start # ...

  6. [Usaco2013 Nov]No Change

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  7. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  8. 经典矩阵dp寻找递增最大长度

    竖向寻找矩阵最大递增元素长度,因为要求至少一列为递增数列,那么每行求一下最大值就可以作为len[i]:到i行截止的最长的递增数列长度. C. Alyona and Spreadsheet time l ...

  9. 6.13---example

    example如何使用?简单查询这个例子展示了如何用生成后的Example类去生成一个简单的where子句: TestTableExample example = new TestTableExamp ...

  10. 如何手工搭建本地Yum仓库

    如何手工搭建本地Yum仓库(重点推荐)  https://www.linuxidc.com/Linux/2016-09/135480.htm CentOS7.2 创建本地YUM源和局域网YUM源: h ...