IT常识
技术 Python PHP JavaScript IOS Android Java 数据库 资源 公众号 代码片段 github
  • IT常识
  • web服务器

Gosper's Hack (生成 n元集合所有 k 元子集

Posted 2021-05-15

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gosper's Hack (生成 n元集合所有 k 元子集相关的知识,希望对你有一定的参考价值。

Gosper’s Hack是一种生成 n元集合所有 k元子集的算法,它巧妙地利用了位运算

void GospersHack(int k, int n) {
    int cur = (1 << k) - 1;
    int limit = (1 << n);
    while (cur < limit) {
        // do something
        int lb = cur & -cur;
        int r = cur + lb;
        cur = ((r ^ cur) >> __builtin_ctz(lb) + 2) | r;
        // 或:cur = (((r ^ cur) >> 2) / lb) | r;
    }
}

转自:https://zhuanlan.zhihu.com/p/360512296
例题:https://www.luogu.com.cn/problem/P1441

以上是关于Gosper's Hack (生成 n元集合所有 k 元子集的主要内容,如果未能解决你的问题,请参考以下文章

如何并行计算 k 个集合位的所有组合?

Mashmokh's Designed Problem 01生成树

HDU 5624 KK's Reconstruction(最小生成树)

HDU 1162 Eddy's picture (最小生成树)(java版)

hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)

HDU 4081Qin Shi Huang&#39;s National Road System(最小生成树+最小瓶颈路)

(c)2006-2024 SYSTEM All Rights Reserved IT常识