-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriffle-shuffle.cpp
More file actions
51 lines (47 loc) · 1003 Bytes
/
riffle-shuffle.cpp
File metadata and controls
51 lines (47 loc) · 1003 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void shuffle(vector<int>& a) {
vector<int> b[2];
for (int i = 0; i < a.size(); i++) {
if (2 * i < a.size()) {
b[0].push_back(a[i]);
}
else {
b[1].push_back(a[i]);
}
}
int j = 0, idx[2] = {};
for (int i = 0; i < a.size(); i++) {
if (idx[j] < b[j].size()) {
a[i] = b[j][idx[j]];
++idx[j];
}
j ^= 1;
}
}
bool order(const vector<int>& a) {
for (int i = 0; i < a.size(); i++) {
if (a[i] != i + 1) return false;
}
return true;
}
int cycle(int n) {
vector<int> a(n);
for (int i = 1; i <= n; i++) {
a[i-1] = i;
}
int cnt = 0;
do
{
++cnt;
shuffle(a);
} while (!order(a));
return cnt;
}
int main() {
freopen("riffle-shuffle-cycle.txt", "w", stdout);
for (int i = 1; i <= 54; i++) {
cout << i << ": " << cycle(i) << endl;
}
return 0;
}