Re: [問題] 在一個給予的mask中,例舉所有k-bit 組合

作者: dnol (舞秋風 憶白雲)   2024-02-05 12:23:30
謝謝各位大神的建議,我現在可以用到Gosper's Hackw產生我需要的bit組合。
我現在有個更進階的問題。
我想要根據1 bit的count來排例n bits的數字,但不用sorting。
舉例,當n=3時。我希望數字排例如下。當我要讀第4個數字時,我會拿到3。
1, 2, 4, "3", 5, 6, 7
[001, 010, 100, 011, 101, 110, 111]
Code 可能如下。
unsigned x = get_kth_number_by_count_of_1_bits(k /*4*/, numer_of_bits /*3*/);
我現在是用gosper hackw先產生一個look up table。
但我希望可以不用look up table,直接透過bit operation或數學運算,
在constant time,拿到我所要的數字。
作者: firejox (Tangent)   2024-02-05 13:48:00
look up 不好嗎 空間限制有這麼嚴格?
作者: dnol (舞秋風 憶白雲)   2024-02-05 14:09:00
我目前是在cuda上做平均運算,所以對空間和時間比較要求
作者: Schottky (順風相送)   2024-02-05 15:49:00
CUDA 有直接數幾個 1 幾個 0 的 function
作者: FRAXIS (喔喔)   2024-02-06 03:08:00
可以研究一下 Combinatorial number system
作者: dnol (舞秋風 憶白雲)   2024-02-06 13:42:00
謝謝樓上的提示。
作者: johnjohnlin (嗯?)   2024-02-06 16:46:00
https://stackoverflow.com/questions/1776442/對小的nk來說查表還是比較快,所以要看你資料分佈記得leetcode有一題就是考O(1)

Links booklink

Contact Us: admin [ a t ] ucptt.com