-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
69 lines (66 loc) · 1.31 KB
/
B.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
65
66
67
68
69
#include "bits/stdc++.h"
using namespace std;
int a[400010];
int b[400010];
int d1[400010], d2[400010];
int pi[400010];
int main(int argc, char const *argv[])
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++) {
b[i] = (m - a[i]) % m;
}
sort(b, b + n);
for(int i = 0; i < n; i++) {
d1[i] = (m + a[(i + 1) % n] - a[i]) % m;
d2[i] = (m + b[(i + 1) % n] - b[i]) % m;
}
for(int i = n; i < n + n; i++) {
d2[i] = d2[i - n];
}
// for(int i = 0; i < n; i++) {
// printf("%d ", d1[i]);
// }
// printf("\n");
// for(int i = 0; i < n; i++) {
// printf("%d ", d2[i]);
// }
// printf("\n");
// exit(0);
int cur = -1;
pi[0] = -1;
for(int i = 1; i < n; i++) {
while(cur != -1 && d1[cur + 1] != d1[i]) {
cur = pi[cur];
}
if(d1[cur + 1] == d1[i]) {
++cur;
}
pi[i] = cur;
}
set <int> s;
cur = -1;
for(int i = 0; i < n + n - 1; i++) {
while(cur != -1 && d1[cur + 1] != d2[i]) {
cur = pi[cur];
}
if(d1[cur + 1] == d2[i]) {
++cur;
}
// cout << i << ' ' << cur << endl;
if(cur == n - 1) {
s.insert((m + a[0] - b[i - n + 1]) % m);
cur = pi[cur];
}
}
printf("%u\n", s.size());
for(auto i : s) {
printf("%d ", i);
}
printf("\n");
return 0;
}