[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:
- -
Sample output:
-
Solution
完了,一道简单题调了3个小时
#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)的更多相关文章
- HDU 2062 Subset sequence 数位dp,思路 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2062 Subset sequence Time Limit: 1000/1000 MS (Java/Others ...
- 【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 ...
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
- 2018牛客网暑假ACM多校训练赛(第四场)C Chiaki Sequence Reloaded (组合+计数) 或 数位dp
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round4-C.html 题目传送门 - https://www.no ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- 【专题】数位DP
[资料] ★记忆化搜索:数位dp总结 之 从入门到模板 by wust_wenhao 论文:浅谈数位类统计问题 数位计数问题解法研究 [记忆化搜索] 数位:数字从低位到高位依次为0~len-1. 高位 ...
- hdu3555 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- ZOJ 3494 BCD Code(AC自动机+数位DP)
BCD Code Time Limit: 5 Seconds Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...
随机推荐
- TI BLE STACK - OSAL
TI 的OSAL做的很不错,不过看起来也挺费劲可能自己水平太差吧,网上买的谷雨的开发板觉得确实挺不错的. 做点学习笔记,首先是记录OSAL里执行的顺序流程,主要是task ,event,message ...
- libnids 中哈希表的建立
//hash.c #include <sys/types.h>#include <sys/time.h>#include <stdio.h>#include < ...
- bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】
仿佛学到了贪心的新姿势-- 考虑相邻两头牛,交换它们对其他牛不产生影响,所以如果交换这两头牛能使这两头牛之间的最大值变小,则交换 #include<iostream> #include&l ...
- [App Store Connect帮助]六、测试 Beta 版本(3.2)管理测试员:邀请外部测试员
在您上传至少一个构建版本之后,您可以邀请外部测试员(您组织之外的人员)使用“TestFlight Beta 版测试”来测试您的 App.为了使您的构建版本可用于外部测试,请创建一个群组.添加构建版本, ...
- [ZJOI2008]杀蚂蚁
题意翻译 注意在(0,0)已经有蚂蚁的时候是不会生成新蚂蚁的 还有如果有蚂蚁扛着蛋糕,但是不在某个炮的范围内,炮仍然会打最近的蚂蚁 题目描述 最近,佳佳迷上了一款好玩的小游戏:antbuster. 游 ...
- Objective-C设计模式——外观Faced(接口适配)
外观模式 外观设计模式和适配器差不多,不过它门对对象控制的粒度不同,适配器一般只是控制一个系统和客户端的对接.外观则是用来抽象多个系统一起工作. 外观一般具有多个子系统,所以外观应持有多个子系统的引用 ...
- express模块安装使用命令配置
之前的博客nodejs安装和配置好路径之后就可以安装express了: 随便打开个文件夹右键选择,git bash here 命令行里输入[npm install express -g] -g是全局安 ...
- OKHTTP 简单分析
内部使用了OKIO库, 此库中Source表示输入流(相当于InputStream),Sink表示输出流(相当于OutputStream) 特点: ·既支持同步请求,也支持异步请求,同步请求会阻塞当前 ...
- android studio 的Error:No such property: packageApplicationTask for class: com.android.build.gradle.internal.variant.ApkVariantOutputData解决方法
出现这个原因是安装了jrebel热部署插件,在projectStructure中的projec选项中,android 插件源仓会有热部署的配置.将jcenter后的配置全部删除就可以 注:本人只安装了 ...
- 【C++】智能指针简述(四):shared_ptr
在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...