SPOJ 1182 Sorted bit sequence

题目链接

题意:

分析:

其实如果会了Ural 1057. Amount of Degrees那道题目,这道题自然也就会了...

我们考虑枚举第$k$个数字的$1$的个数,那么我们需要计算的也就是区间内二进制状态下$1$的个数为$x$的数字个数,这个的求法在上一题中写过了...

我们求到第$k$的数字的$1$的个数为$x$,那么我们去二分这个数字是什么,也就是说我们要求一个最靠左的右端点,使得区间$[n,ans]$内$1$的个数为$x$的数字个数恰好为$k$,然后总体思路就解决了...

细节方面就是要注意特判$0$,然后对于负数的处理就是先去掉负数的符号位,然后判断,最后输出的时候再把符号位加上...

具体实现看代码...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;

const int maxn=32+5;

int n,m,k,ans,cas,f[maxn][maxn];

inline void init(void){
	f[0][0]=1;
	for(int i=1;i<=31;i++){
		f[i][0]=1;
		for(int j=1;j<=i;j++)
			f[i][j]=f[i-1][j]+f[i-1][j-1];
	}
}

inline int calc(int x,int y){
	int cnt=0,ans=0;
	for(int i=31;i>=1;i--){
		if((x>>i)&1){
			cnt++;
			if(cnt>y) break;
			x=x^(1<<i);
		}
		if((1<<(i-1))<=x)
			ans+=f[i-1][y-cnt];
	}
	if(cnt+x==y) ans++;
	return ans;
}

inline int solve(void){
	int len=0;
	for(int i=0,tmp;i<=31;i++){
		tmp=calc(m,i)-calc(n-1,i);
		if(tmp>=k) break;
		k-=tmp;len=i+1;
	}
	long long l=n,r=m,ans;
	while(l<=r){
		long long mid=(l+r)>>1;
		if(calc(mid,len)-calc(n-1,len)>=k)
			ans=mid,r=mid-1;
		else
			l=mid+1;
	}
	return ans;
}

signed main(void){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	scanf("%d",&cas);init();
	while(cas--){
		scanf("%d%d%d",&n,&m,&k);
		if(m==0&&n==0){
			puts("0");
			continue;
		}
		int flag=0;
		if(n==0) n++,k--;
		if(m==0) m--,k--;
		if(n<0) n^=(1<<31),flag=1;
		if(m<0) m^=(1<<31),flag=1;
		ans=solve();
		if(flag) printf("%d\n",(ans^(1<<31)));
		else printf("%d\n",ans);
	}
	return 0;
}

  



By NeighThorn

时间: 2025-01-02 05:15:13

SPOJ 1182 Sorted bit sequence的相关文章

SPOJ 1182 Sorted bit squence

标签(空格分隔): 数位DP 二分 题目链接 先算出答案1的个数,再二分查找 #include<cstdio> #include<cstring> #include<iostream> using namespace std; typedef long long ll; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

[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 bin

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 × 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 w

Sorting It All Out

Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D.

POJ 1094 Sorting It All Out 拓扑排序

Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to

volley介绍03

------------------------------------------------------------------------------ 转载:http://blog.csdn.net/crazy__chen/article/details/46486123 ------------------------------------------------------------------------------ 在上一篇文章中,我们已经提到volley的使用方式和设计的整体

插入排序——算法导论

最近在看MIT的算法导论,在网易公开课上有这门课的视频,正好讲义也在图书馆借到了,有在看的小伙伴可以一起加油. 绪论中以插入排序为例,讲述了算法中非常重要的两个概念时间复杂度T(n)和空间复杂度.详细地对程序花费时间T. 伪代码: INSERTION-SORT(A) 1 for j←2 to length[A] 2 do key←A[j] 3 Insert A[j] into the sorted 4 sequence A[1..j-1]. 5 i←j-1 6 while i>0 and A[i

poj 1094 拓扑排序

Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D.

[ACM] POJ 1094 Sorting It All Out (拓扑排序)

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 9248 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from sm