【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)

探讨了基于32位整数表示的排序算法,包括正数和负数的二进制表示及其排序方法,并提供了一种通过计算二进制中1的数量进行排序的解决方案。

SORTBIT - Sorted bit squence

no tags

 

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 


 

  我的天哪!感觉这道题做了100年,啊负数搞死我了!!
  我觉得不用二分,又不是很大,然后先算出是多少个1,再一边统计一边填数。

 

  原始的代码风格233~

 

代码如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cmath>
 8 using namespace std;
 9 #define LL long long
10 
11 int f[40][40],g[2][40];
12 
13 void init()
14 {
15     memset(f,0,sizeof(f));
16     f[0][0]=1;
17     for(int i=1;i<=32;i++)
18     {
19         for(int j=0;j<i;j++)
20         {
21             f[i][j+1]+=f[i-1][j];
22             f[i][j]+=f[i-1][j];
23         }
24     }
25 }
26 
27 void get_g(LL x,int q)
28 {
29     if(x==-1) return;
30     int y=0;
31     for(int i=32;i>=1;i--)
32     {
33         for(int j=y;j<=i+y;j++)
34         {
35             if(x&(1<<i-1)) g[q][j]+=f[i-1][j-y];
36         }
37         if(x&(1<<i-1)) y++;
38     }
39     g[q][y]++;
40     g[q][0]=1;
41 }
42 
43 LL ffind(LL x,LL y,LL k)
44 {
45     LL ans=0;
46     bool flag=1;
47     for(int i=32;i>=1;i--)
48     {
49         if( ( (x&(1<<i-1))||!flag)&&f[i-1][y]<k)
50         {
51             ans+=1LL<<i-1;
52             k-=f[i-1][y];
53             y--;
54         }
55         else if((x>>i-1)&1) flag=0;
56     }
57     return ans;
58 }
59 
60 int main()
61 {
62     init();
63     int T;
64     scanf("%d",&T);
65     LL mx=1LL<<32;
66     while(T--)
67     {
68         bool q=0;
69         LL m,n,k,t;
70         scanf("%lld%lld%lld",&m,&n,&k);
71         if(m<0) m=mx+m,n=mx+n,q=1;
72         memset(g,0,sizeof(g));
73         get_g(n,0);get_g(m-1,1);
74         int st,h=0;
75         for(st=0;st<=32;st++)
76         {
77             if(h+g[0][st]-g[1][st]>=k) break;
78             h+=g[0][st]-g[1][st];
79         }
80         LL ans=ffind(n,st,k-h+g[1][st]);
81         if(q) ans=ans-mx;
82         printf("%lld\n",ans);
83     }
84     return 0;
85 }
[SPOJ 1182]

 

这种题是数位DP里面我最熟悉的了,接下来要来一题厉害的高精度!!

 

2016-10-10 21:02:10

 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5947252.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值