Enumerate Combination C(k, n) in a bitset
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的更多相关文章
- 第k小团(Bitset+bfs)牛客第二场 -- Kth Minimum Clique
题意: 给你n个点的权值和连边的信息,问你第k小团的值是多少. 思路: 用bitset存信息,暴力跑一下就行了,因为满足树形结构,所以bfs+优先队列就ok了,其中记录下最后进入的点(以免重复跑). ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 内置函数(sorted、map、enumerate、filter、reduce)
1.sorted() 语法: sorted(iterable, cmp=None, key=None, reverse=False) 把iterable中的items进行排序之后,返回一个新的列表,原 ...
- 扶苏的bitset浅谈
bitset作为C++一个非常好用的STL,在一些题目中巧妙地使用会产生非常不错的效果.今天扶苏来分享一点bitset的基础语法和应用 本文同步发布于个人其他博客,同时作为P3674题解发布. 本文感 ...
- AtCoder Regular Contest 070 D - No Need 想法:利用单调性二分+bitset优化
/** 题目:D - No Need 链接:http://arc070.contest.atcoder.jp/tasks/arc070_b 题意:给出N个数,从中选出一个子集,若子集和大于等于K,则这 ...
- python的enumerate()函数
其中的一篇是这样的:一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate 比如: for index,value in enumerate(list): ...
- 2019牛客暑期多校训练营(第二场)D bitset
题意 给一个n个结点的带点权的图,找到第k小的团的权值 分析 用bitset表示团的状态,一个结点必须和团里的每个结点都连边才能加进去,所以可以直接用\(\&\)运算来判断一个结点是否能加进去 ...
- 60第K个排列
题目:给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" &quo ...
- 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 ...
随机推荐
- 使用PropTypes进行类型检测
PropTypes 是react提供的用于检验数据类型的typechecking,避免应用越来越大的时候出现意料之外的bug class Greeting extends React.Componen ...
- python转exe2
转载自 xiake200704 最终编辑 xiake200704 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你 ...
- 【linux】进程存储管理
看<Linux高级程序设计>的笔记 设有一个hello的可执行文件 ①显示该文件的基本信息 ls hello -l ②文件基本情况 file hello ③列出文件的存储区域情况 size ...
- hdu 1573(中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 横竖屏切换,activity重建问题
最近有个需求,横屏直播A退出后返回直播列表页B(竖屏)时,在小米8上列表页B直接变成横屏的了,因为列表页B由竖屏切换成横屏了,还会重新执行生命周期onCreate()-onResume()等等. 为了 ...
- HDU 6249 Alice’s Stamps(2017 CCPC-Final G题,DP)
题目链接 HDU 6249 题意 给定$m$个区间,在这些区间中选出不超过$k$个,求被覆盖的点的数量的最大值. 设$f[i][j]$表示选到第$i$个点并选了$j$个区间的时候能得到的最大答案. 处 ...
- Tiny4412 学习
平台: Tiny4412ADK + S700 + 4GB FlashU-boot: 友善之臂提供的开源U-boot Linux: linux-3.0.31 Android: android_4_1_2 ...
- 【mybatis】从一个错误,看mybatis中的#和$的区别
事情的发展是这样的: 因为一个需求,需要在java中拼接出一个完整的sql语句,然后将整条sql语句传递给mybatis执行. mapper.java是这样的: int insertMaster(Wo ...
- 2017.2.20 activiti实战--第五章--用户与组及部署管理(一)用户与组
学习资料:<Activiti实战> 第五章 用户与组及部署管理(一)用户与组 内容概览:讲解activiti中内置的一套用户.组的关系,以及如何通过API添加.删除.查询. 5.1 用户与 ...
- 基于Wiremock创建Mock Service平台
转载:http://blog.csdn.net/liuchunming033/article/details/52399397 1.Wiremock工具介绍 一般开发项目都会分模块进行,比如都会把前端 ...