Suppose n<=32, we can enumerate C(k, n), with bits representing absence or presence, in the following way:

#include <iostream>
#include <vector>
#include <bitset>
using namespace std; bitset<32> getComb(const vector<int> &comb) {
bitset<32> bitcombs;
for (int i=0; i<comb.size(); ++i) bitcombs.set(comb[i], true);
return bitcombs;
} bool nextComb(vector<int> &comb, int n) {
int k = comb.size(), i = k - 1;
++comb[i];
while (i>=1 && comb[i]>=n-k+1+i) ++comb[--i];
if (comb[0] > n-k) return false;
for (++i; i<k; ++i) comb[i] = comb[i-1] + 1;
return true;
} int main() {
int n = 3, k = 2;
vector<int> comb(k);
for (int i=0; i<k; ++i) comb[i] = i;
do {
cout<<getComb(comb).to_ulong()<<endl;
} while (nextComb(comb, n));
return 0;
}

The algorithms works like this when k=4, n=5:

01111->10111->11011->11101->11110

So, could you find the pattern?

Enumerate Combination C(k, n) in a bitset的更多相关文章

  1. 第k小团(Bitset+bfs)牛客第二场 -- Kth Minimum Clique

    题意: 给你n个点的权值和连边的信息,问你第k小团的值是多少. 思路: 用bitset存信息,暴力跑一下就行了,因为满足树形结构,所以bfs+优先队列就ok了,其中记录下最后进入的点(以免重复跑). ...

  2. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. 内置函数(sorted、map、enumerate、filter、reduce)

    1.sorted() 语法: sorted(iterable, cmp=None, key=None, reverse=False) 把iterable中的items进行排序之后,返回一个新的列表,原 ...

  4. 扶苏的bitset浅谈

    bitset作为C++一个非常好用的STL,在一些题目中巧妙地使用会产生非常不错的效果.今天扶苏来分享一点bitset的基础语法和应用 本文同步发布于个人其他博客,同时作为P3674题解发布. 本文感 ...

  5. AtCoder Regular Contest 070 D - No Need 想法:利用单调性二分+bitset优化

    /** 题目:D - No Need 链接:http://arc070.contest.atcoder.jp/tasks/arc070_b 题意:给出N个数,从中选出一个子集,若子集和大于等于K,则这 ...

  6. python的enumerate()函数

    其中的一篇是这样的:一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate 比如: for index,value in enumerate(list):       ...

  7. 2019牛客暑期多校训练营(第二场)D bitset

    题意 给一个n个结点的带点权的图,找到第k小的团的权值 分析 用bitset表示团的状态,一个结点必须和团里的每个结点都连边才能加进去,所以可以直接用\(\&\)运算来判断一个结点是否能加进去 ...

  8. 60第K个排列

    题目:给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:    "123"    &quo ...

  9. Codeforces 789e The Great Mixing (bitset dp 数学)

    Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th typ ...

随机推荐

  1. Java 学习(1): windows java 运行& 环境配置

    Java 文件的运行 如下,创建了一个 名为"HelloWorld.java" 的 Java 文件: public class HelloWorld { public static ...

  2. 在react当中巧用扩展运算符

    ...props可以把没有写到的属性补充完整 ...style 可以把style 属性在styles当中展开

  3. H5 基本标签使用 浅析 (含video标签、input表单等)

    1. 音频标签<audio> <audio src = “./music/Alone.mp3” controls autoplay loop = “3” ></audio ...

  4. 洛谷P1965 转圈游戏 [NOIP2013]

    题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此 ...

  5. MVP MVVM MVC

    上一篇得到大家的关注,非常感谢.由于自己对于这些模式的理解也是有限,对于MVC,MVP,MVVM这些模式的比较,是结合自己的理解,一些地方不一定准确,需要的朋友可以参考下 上一篇得到大家的关注,非常感 ...

  6. Linux下的软连接和硬链接

    由于教学上的原因,需要下载Android源码,后来使用repo时,系统提示需要python2,我的系统中是两个都有的,但是默认是python3,无法下载,通过创建了个链接搞定,下面就来说说linux下 ...

  7. Scrapy笔记:持久化,Feed exports的使用

    首先要明确的是,其实所有的FeedExporter都是类,里面封装了一般进行io操作的方法.因此,要怎么输出呢?其实从技术实现来说,在生成item的每一步调用其进行储存都是可以的,只不过为了更加符合s ...

  8. 通过vSphere-client虚拟化服务器

    一.什么是vClientvClient是vSphere的重要组件之一.用于用户连接ESXi或vCenter管理和分配vSphere的各种资源.有vClient和WebvClient两个版本.安装部署了 ...

  9. LeetCode OJ-- Pow(x, n) **@

    https://oj.leetcode.com/problems/powx-n/ 二分法 class Solution { public: double pow(double x, int n) { ...

  10. 怎样录制简单GIF动图

    看到视频里的精彩画面,想用动图的形式保存下来,应该如何录制呢,今天就介绍一款小巧实用,操作简单的软件,GifCam 2.0 汉化绿色版.相比其它的录制软件,它是免费无水印又可以在线录制的. 本来学习一 ...