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 ...
随机推荐
- Entity Framework context per request
原文发布时间为:2011-09-24 -- 来源于本人的百度文章 [由搬家工具导入] http://www.blog.cyberkinetx.com/2011/05/15/entity-framewo ...
- Program "D:\AndroidDevelopment\android-ndk-r9\ndk-build.cmd" not found in PATH
1.问题描述 2.解决方法:修改ndk-build.cmd的配置路径, 修改成本地ndk-build.cmd所在路径,如下
- IIS 发布双证书
1.端口都用443 2.配置主机名 3.勾选需要服务器名称指示
- 10.1综合强化刷题 Day3 afternoon
竞赛时间:????年??月??日??:??-??:?? 题目名称 a b c 名称 a b c 输入 a.in b.in c.in 输出 a.out b.out c.out 每个测试点时限 1s 1s ...
- [TJOI2014] Alice and Bob
非常好的一道思维性题目,想了很久才想出来qwq(我好笨啊) 考虑a[]数组有什么用,首先可以yy出一些性质 (设num[i]为原来第i个位置的数是什么 , 因为题目说至少有一个排列可以满足a[],所以 ...
- java文件下载导出
前台代码: $("#btnExport").click(function(){ top.$.jBox.confirm("确认要导出房屋信息吗?","系 ...
- Linux下Shell文件内容替换(sed)(转)
sed -i 's/被替换的内容/要替换成的内容/g' file #-i为直接修改并保存 参考: http://blog.sina.com.cn/s/blog_7211cb9201019hgd.htm ...
- XCode删除多余的Simulator(模拟器)
每个xocde都会自带一个模拟器,且都是随安装包一起打包的,比如xcode8.0的就自带iOS10的模拟器,这个是没办法删除. 但是对于想要用iOS10以前的模拟器,可以通过这里进行下载: 最终下载的 ...
- setTimeout改变this指向(****************************************)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 多线程一共就俩问题:1.线程安全(访问共享数据) 2.线程通信(wait(),notify())
多线程一共就俩问题:1.线程安全(访问共享数据) 2.线程通信(wait(),notify()) 1.线程安全,无非就是加锁,访问共享资源时,synchronized 2.线程通信,就是控制各个线程之 ...