-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermshift.cpp
64 lines (52 loc) · 1.12 KB
/
permshift.cpp
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
void solve() {
ll n, m;
cin >> n >> m;
vi p(n);
for (ll i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
vi cnt(n);
for (ll i = 0; i < n; i++) {
ll nw = (i - p[i]) % n;
if (nw < 0) nw += n;
cnt[nw]++;
}
ll cc = 0;
ll k = 0;
vi vis(n);
function<void(ll)> dfs = [&](ll i) {
if (vis[i]) return;
vis[i] = true;
ll e = (p[i] + k) % n;
if (e < 0) e += n;
dfs(e);
};
vi path;
for (; k < n; k++) {
//cout << cnt[k] << endl;
if (cnt[k] >= n / 3) {
vis.clear();
vis.resize(n);
cc = 0;
for (ll i = 0; i < n; i++) {
if (!vis[i]) cc++, dfs(i);
}
//cout << k << " " << cc << endl;
if (n - cc <= m) path.push_back(k);
}
}
cout << path.size() << " ";
for (ll i : path) cout << i << " ";
cout << endl;
}
int main() {
ll t;
cin >> t;
while (t--) solve();
return 0;
}