-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextCombination.cpp
More file actions
81 lines (79 loc) · 1.19 KB
/
NextCombination.cpp
File metadata and controls
81 lines (79 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class NextCombination
{
private:
const int n,r;
VB used;
vector<int> point;
int back=0;
int c=0;
public:
NextCombination(const int n_, const int r_)
:n(n_),r(r_),used(VB(n)),point(vector<int>(r)),back(0),c(1)
{}
bool end() const
{
return c<0;
}
vector<int> next()
{
c--;
for(int j=n-1;j>=0;j--)
{
if(!used[j]) break;
used[j]=false;
c--;
if(c<0) break;
back=point[c]+1;
}
if(c<0)
return vector<int>(0);
used[point[c]]=false;
while(c<r)
{
point[c]=back;
used[back++]=1;
c++;
}
return point;
}
};
class NextCombinationInt
{
using LL=long long;
private:
const int n,r;
LL used;
vector<int> point;
int back=0;
int c=0;
public:
NextCombinationInt(const int n_, const int r_)
:n(n_),r(r_),used(0),point(vector<int>(r)),back(0),c(1)
{}
bool end() const
{
return c<0;
}
LL next()
{
c--;
for(int j=n-1;j>=0;j--)
{
if((used&(1<<j))==0) break;
used&=~(((LL)1)<<j);
c--;
if(c<0) break;
back=point[c]+1;
}
if(c<0)
return 0;
used&=~(((LL)1)<<point[c]);
while(c<r)
{
point[c]=back;
used|=(1<<(back++));
c++;
}
return used;
}
};