首页 > 代码库 > [spoj1182][Sorted Bit Sequence] (数位dp)
[spoj1182][Sorted Bit Sequence] (数位dp)
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:
20 5 3-5 -2 2
Sample output:
2-5
Solution
完了,一道简单题调了3个小时
论文:http://wenku.baidu.com/link?url=FrOOQ0uY5RDizsTypIHewuCFzdQxSpets-J5cUpu_h3NBTxn-s3BMcQhgnQYTdrqV7XTBbDgU-HKNUmt-BbhDx_dNcR4v0ZMBZfs_Fnfjai
#include<stdio.h>inline int Rin(){ int x=0,c=getchar(),f=1; for(;c<48||c>57;c=getchar()) if(!(c^45))f=-1; for(;c>47&&c<58;c=getchar()) x=(x<<1)+(x<<3)+c-48; return x*f;}int f[33][33];void init(){ int i,j; f[0][0]=1; for(i=1;i<=31;i++){ f[i][0]=f[i][i]=1; for(j=1;j<i;j++) f[i][j]=f[i-1][j-1]+f[i-1][j]; }}int cal(int x,int k){ int cnt=0,ans=0,i; for(i=31;i;i--){ if(x&(1<<i)){ cnt++; if(cnt>k)break; x^=(1<<i); } if((1<<(i-1))<=x) ans+=f[i-1][k-cnt]; } if(cnt+x==k)ans++; return ans;}int solve(int x,int y,int k){ int i,cnt=0; for(i=1;i<=31;i++){ cnt=cal(y,i)-cal(x-1,i); if(k<=cnt)break; k-=cnt; } int l=x,r=y,mid,ans=0; while(l<=r){ mid=l+r>>1; if(cal(mid,i)-cal(x-1,i)<k) l=mid+1; else ans=mid,r=mid-1; } return ans;}int main(){ init(); int T=Rin(),n,m,K; while(T--){ m=Rin(),n=Rin(),K=Rin(); if(!m && !n)puts("0"); else if(!m){ K--,m=1; if(!K)puts("0"); else printf("%d\n",solve(m,n,K)); } else if(m>0)printf("%d\n",solve(m,n,K)); else if(!n){ K--,n=-1; if(!K)puts("0"); else printf("%d\n",(1<<31)|solve(m,n,K)); } else printf("%d\n",(1<<31)|solve(m,n,K)); } getchar();getchar(); return 0;}
[spoj1182][Sorted Bit Sequence] (数位dp)