[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 ...
随机推荐
- 24. [Ext JS 4] 实战之Load Mask(加载遮罩)的显示与隐藏
转自:https://blog.csdn.net/oscar999/article/details/27176791
- unable to unroll loop 报错
unable to unroll loop, loop does not appear to terminate in a timely manner (1024 iterations) 原本代码 f ...
- bzoj 1800: [Ahoi2009]fly 飞行棋【枚举】
在圆里所以没有平行四边形,n^4枚举点即可 #include<iostream> #include<cstdio> using namespace std; const int ...
- 2017杭电多校第七场1011Kolakoski
Kolakoski Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Tota ...
- 模拟 HDOJ 4552 Running Rabbits
题目传送门 /* 模拟:看懂题意,主要是碰壁后的转向,笔误2次 */ #include <cstdio> #include <algorithm> #include <c ...
- [转].net cookie版购物车
本文转自:http://www.sulong.cc/article/program/aspx/110613114249.html #region 添加到购物车AddShoppingCar /// &l ...
- while和for的内嵌
迭代,从初始情况按照规律不断求解中间情况,最终推导出结果.(折纸珠峰) 穷举:把所有情况都列举一遍,选择符合条件的选项(百鸡百钱) 循环四要素:初始条件,循环条件,循环体,状态改变. While的使用 ...
- 实用and常用shell命令汇编
很久没写blog了,基本都在用 github和笔记.现在将一些常用的shell并且很使用的shell用法分享一下: 分行读取,切割,计数: cat product.txt | while read l ...
- 使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏(转)
我们知道,MFC程序如果检测到存在内存泄漏,退出程序的时候会在调试窗口提醒内存泄漏.例如: class CMyApp : public CWinApp{public:BOOL InitApplicat ...
- CF817C Really Big Numbers
思路: 二分. 实现: #include <iostream> #include <cstdio> using namespace std; typedef long long ...