【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)
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 × 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~
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
这种题是数位DP里面我最熟悉的了,接下来要来一题厉害的高精度!!
2016-10-10 21:02:10