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. 微信小程序 body属性的问题

    微信小程序里面没有了body这个节点了,取而代之的是page

  2. 【BZOJ4542】大数(莫队)

    题意:给定一个N位的由[0..9]组成的数字串和质数P,有M次不强制在线的询问,每次询问区间[l,r]中模P意义下为0的子串个数 N,M<=2e5,P<=1e10 思路:一次A,本来还以为 ...

  3. POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15767   A ...

  4. powershell 调用winform dll

    //1.加载dll,调用winform窗体,使用指定构造函数 param{ $filePath="" } [void][reflection.assembly]::LoadFile ...

  5. [LeetCode] Remove Nth Node From End of List 快慢指针

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. linux下kodi没有声音的解决

    前几天,心血来潮,就安装了manjaro的pre3版本,由于是mini kde版本的,就随手安装了kodi,可以用来看视频,听音乐和看图片. 结果在所有插件都折腾好了之后发现,在屏幕的右上角有一个喇叭 ...

  7. TensorFlow中文社区---下载与安装

    转自:http://www.tensorfly.cn/tfdoc/get_started/os_setup.html 下载与安装 你可以使用我们提供的二进制包, 或者使用源代码, 安装 TensorF ...

  8. Day 29 process&thread_1

    进程和线程 1 进程(process): 1.定义: 最小的执行单元.进程就是一个程序在一个数据集上的一次动态执行过程. 进程一般由程序.数据集.进程控制块三部分组成: 我们编写的程序用来描述进程要完 ...

  9. 属性动画详解一(Property Animation)

    效果图: Android动画有3类: 1.View Animation (Tween Animation) 2.Drawable Animation (Frame Animation) 2.Prope ...

  10. LeetCode OJ--Multiply Strings **

    https://oj.leetcode.com/problems/multiply-strings/ 用字符串实现大数乘法,细节题,细节很多 class Solution { public: stri ...