【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)

探讨了基于32位整数表示的排序算法,包括正数和负数的二进制表示及其排序方法,并提供了一种通过计算二进制中1的数量进行排序的解决方案。

SORTBIT - Sorted bit squence

no tags

 

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~

 

代码如下:

 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 }
[SPOJ 1182]

 

这种题是数位DP里面我最熟悉的了,接下来要来一题厉害的高精度!!

 

2016-10-10 21:02:10

 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5947252.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值