Determin the lexicographic distance between two integers

后端 未结 2 1454
时光说笑
时光说笑 2021-01-15 00:23

Say we have the lexicographicaly integers 3,5,6,9,10,12 or 0011,0101,0110,1001,1010,1100 Each with two bits set.

What I want is to find the distance(how

2条回答
  •  借酒劲吻你
    2021-01-15 01:13

    Having a number
    x = 2k1+2k2+...+2km
    where k12<...m
    it could be claimed that position of number x in lexicographically ordered sequence of all numbers with the same hamming weight is
    lex_order(x) = C(k1,1)+C(k2,2)+...+C(km,m)
    where C(n,m) = n!/m!/(n-m)! or 0 if m>n

    Example:

    3 = 20 + 21
    lex_order(3) = C(0,1)+C(1,2) = 0+0 = 0

    5 = 20 + 22
    lex_order(5) = C(0,1)+C(2,2) = 0+1 = 1

    6 = 21 + 22
    lex_order(6) = C(1,1)+C(2,2) = 1+1 = 2

    9 = 20 + 23
    lex_order(9) = C(0,1)+C(3,2) = 0+3 = 3

提交回复
热议问题