#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define ft first
#define sd second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pll pair<ll, ll>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 400100;
vector<int> ts;
int n, m, len, mrk[N], pf[N];
bool cyc;
void dfs(int v){
if (cyc) return;
mrk[v] = 1;
if (v >= m){
if (mrk[v - m] == 0)
dfs(v - m);
else if (mrk[v - m] == 1){
cyc = 1;
return;
}
}
if (v + n <= len){
if (mrk[v + n] == 0)
dfs(v + n);
else if (mrk[v + n] == 1){
cyc = 1;
return;
}
}
mrk[v] = 2;
}
bool ok(){
fill(mrk, mrk + len + 1, 0);
cyc = 0;
for (int i = 0; i <= len && !cyc; i++)
if (mrk[i] == 0)
dfs(i);
return !cyc;
}
void DFS(int v){
mrk[v] = 1;
if (v >= m){
if (mrk[v - m] == 0)
DFS(v - m);
}
if (v + n <= len){
if (mrk[v + n] == 0)
DFS(v + n);
}
ts.PB(v);
}
int main(){
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif // _LOCAL
int qq; cin >> qq;
for (; qq; qq--){
cin >> n >> m;
int l = 0, r = (n + m) * 2;
while (l < r){
int md = (l + r + 1) >> 1;
len = md;
if (ok())
l = md;
else r = md - 1;
}
l = n + m - __gcd(n, m) - 1;
cout << l << '\n';
len = l;
fill(mrk, mrk + len + 1, 0);
fill(pf, pf + len + 1, -1);
ts.clear();
for (int i = 0; i <= len; i++)
if (!mrk[i])
DFS(i);
int it = -len;
for (int v : ts){
int mx = it;
it++;
if (v >= m)
mx = max(mx, pf[v - m]);
if (v + n <= len)
mx = max(mx, pf[v + n]);
pf[v] = mx + 1;
}
for (int i = 1; i <= len; i++)
cout << pf[i] - pf[i - 1] << " ";
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
4 ms |
492 KB |
Ok |
7 |
Correct |
20 ms |
1260 KB |
Ok |
8 |
Correct |
9 ms |
748 KB |
Ok |
9 |
Correct |
20 ms |
1344 KB |
Ok |
10 |
Correct |
12 ms |
876 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
219 ms |
10708 KB |
Ok |
7 |
Correct |
203 ms |
11112 KB |
Ok |
8 |
Correct |
398 ms |
14312 KB |
Ok |
9 |
Correct |
260 ms |
12520 KB |
Ok |
10 |
Correct |
133 ms |
6640 KB |
Ok |
11 |
Correct |
226 ms |
12592 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
13 |
Correct |
1 ms |
364 KB |
Ok |
14 |
Correct |
1 ms |
364 KB |
Ok |
15 |
Correct |
1 ms |
364 KB |
Ok |
16 |
Correct |
1 ms |
364 KB |
Ok |
17 |
Correct |
1 ms |
364 KB |
Ok |
18 |
Correct |
1 ms |
364 KB |
Ok |
19 |
Correct |
1 ms |
364 KB |
Ok |
20 |
Correct |
1 ms |
364 KB |
Ok |
21 |
Correct |
1 ms |
364 KB |
Ok |
22 |
Correct |
1 ms |
364 KB |
Ok |
23 |
Correct |
1 ms |
364 KB |
Ok |
24 |
Correct |
3 ms |
492 KB |
Ok |
25 |
Correct |
3 ms |
492 KB |
Ok |
26 |
Correct |
3 ms |
492 KB |
Ok |
27 |
Correct |
3 ms |
492 KB |
Ok |
28 |
Correct |
3 ms |
492 KB |
Ok |
29 |
Correct |
3 ms |
492 KB |
Ok |
30 |
Correct |
2 ms |
364 KB |
Ok |
31 |
Correct |
3 ms |
492 KB |
Ok |
32 |
Correct |
3 ms |
492 KB |
Ok |
33 |
Correct |
3 ms |
492 KB |
Ok |
34 |
Correct |
7 ms |
620 KB |
Ok |
35 |
Correct |
8 ms |
620 KB |
Ok |
36 |
Correct |
8 ms |
748 KB |
Ok |
37 |
Correct |
9 ms |
876 KB |
Ok |
38 |
Correct |
7 ms |
748 KB |
Ok |
39 |
Correct |
7 ms |
620 KB |
Ok |
40 |
Correct |
8 ms |
748 KB |
Ok |
41 |
Correct |
8 ms |
620 KB |
Ok |
42 |
Correct |
8 ms |
620 KB |
Ok |
43 |
Correct |
8 ms |
748 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
13 |
Correct |
1 ms |
364 KB |
Ok |
14 |
Correct |
1 ms |
364 KB |
Ok |
15 |
Correct |
1 ms |
364 KB |
Ok |
16 |
Correct |
1 ms |
364 KB |
Ok |
17 |
Correct |
1 ms |
364 KB |
Ok |
18 |
Correct |
4 ms |
492 KB |
Ok |
19 |
Correct |
20 ms |
1260 KB |
Ok |
20 |
Correct |
9 ms |
748 KB |
Ok |
21 |
Correct |
20 ms |
1344 KB |
Ok |
22 |
Correct |
12 ms |
876 KB |
Ok |
23 |
Correct |
1 ms |
364 KB |
Ok |
24 |
Correct |
1 ms |
364 KB |
Ok |
25 |
Correct |
1 ms |
364 KB |
Ok |
26 |
Correct |
1 ms |
364 KB |
Ok |
27 |
Correct |
1 ms |
364 KB |
Ok |
28 |
Correct |
1 ms |
364 KB |
Ok |
29 |
Correct |
1 ms |
364 KB |
Ok |
30 |
Correct |
1 ms |
364 KB |
Ok |
31 |
Correct |
1 ms |
364 KB |
Ok |
32 |
Correct |
1 ms |
364 KB |
Ok |
33 |
Correct |
1 ms |
364 KB |
Ok |
34 |
Correct |
3 ms |
492 KB |
Ok |
35 |
Correct |
3 ms |
492 KB |
Ok |
36 |
Correct |
3 ms |
492 KB |
Ok |
37 |
Correct |
3 ms |
492 KB |
Ok |
38 |
Correct |
3 ms |
492 KB |
Ok |
39 |
Correct |
3 ms |
492 KB |
Ok |
40 |
Correct |
2 ms |
364 KB |
Ok |
41 |
Correct |
3 ms |
492 KB |
Ok |
42 |
Correct |
3 ms |
492 KB |
Ok |
43 |
Correct |
3 ms |
492 KB |
Ok |
44 |
Correct |
7 ms |
620 KB |
Ok |
45 |
Correct |
8 ms |
620 KB |
Ok |
46 |
Correct |
8 ms |
748 KB |
Ok |
47 |
Correct |
9 ms |
876 KB |
Ok |
48 |
Correct |
7 ms |
748 KB |
Ok |
49 |
Correct |
7 ms |
620 KB |
Ok |
50 |
Correct |
8 ms |
748 KB |
Ok |
51 |
Correct |
8 ms |
620 KB |
Ok |
52 |
Correct |
8 ms |
620 KB |
Ok |
53 |
Correct |
8 ms |
748 KB |
Ok |
54 |
Correct |
121 ms |
3308 KB |
Ok |
55 |
Correct |
142 ms |
3564 KB |
Ok |
56 |
Correct |
154 ms |
3692 KB |
Ok |
57 |
Correct |
97 ms |
2796 KB |
Ok |
58 |
Correct |
118 ms |
3180 KB |
Ok |
59 |
Correct |
104 ms |
2924 KB |
Ok |
60 |
Correct |
96 ms |
2796 KB |
Ok |
61 |
Correct |
95 ms |
2928 KB |
Ok |
62 |
Correct |
123 ms |
3416 KB |
Ok |
63 |
Correct |
107 ms |
2924 KB |
Ok |
64 |
Correct |
135 ms |
3564 KB |
Ok |
65 |
Correct |
120 ms |
3304 KB |
Ok |
66 |
Correct |
112 ms |
3052 KB |
Ok |
67 |
Correct |
108 ms |
2924 KB |
Ok |
68 |
Correct |
109 ms |
3052 KB |
Ok |
69 |
Correct |
284 ms |
10732 KB |
Ok |
70 |
Correct |
295 ms |
11372 KB |
Ok |
71 |
Correct |
264 ms |
10968 KB |
Ok |
72 |
Correct |
285 ms |
10732 KB |
Ok |
73 |
Correct |
266 ms |
11040 KB |
Ok |
74 |
Correct |
260 ms |
10604 KB |
Ok |
75 |
Correct |
288 ms |
10348 KB |
Ok |
76 |
Correct |
298 ms |
11116 KB |
Ok |
77 |
Correct |
255 ms |
10348 KB |
Ok |
78 |
Correct |
287 ms |
10860 KB |
Ok |
79 |
Correct |
283 ms |
10988 KB |
Ok |
80 |
Correct |
276 ms |
10988 KB |
Ok |
81 |
Correct |
298 ms |
10988 KB |
Ok |
82 |
Correct |
271 ms |
10988 KB |
Ok |
83 |
Correct |
258 ms |
10388 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
13 |
Correct |
1 ms |
364 KB |
Ok |
14 |
Correct |
1 ms |
364 KB |
Ok |
15 |
Correct |
1 ms |
364 KB |
Ok |
16 |
Correct |
1 ms |
364 KB |
Ok |
17 |
Correct |
1 ms |
364 KB |
Ok |
18 |
Correct |
4 ms |
492 KB |
Ok |
19 |
Correct |
20 ms |
1260 KB |
Ok |
20 |
Correct |
9 ms |
748 KB |
Ok |
21 |
Correct |
20 ms |
1344 KB |
Ok |
22 |
Correct |
12 ms |
876 KB |
Ok |
23 |
Correct |
1 ms |
364 KB |
Ok |
24 |
Correct |
1 ms |
364 KB |
Ok |
25 |
Correct |
1 ms |
364 KB |
Ok |
26 |
Correct |
1 ms |
364 KB |
Ok |
27 |
Correct |
1 ms |
364 KB |
Ok |
28 |
Correct |
1 ms |
364 KB |
Ok |
29 |
Correct |
1 ms |
364 KB |
Ok |
30 |
Correct |
1 ms |
364 KB |
Ok |
31 |
Correct |
1 ms |
364 KB |
Ok |
32 |
Correct |
1 ms |
364 KB |
Ok |
33 |
Correct |
1 ms |
364 KB |
Ok |
34 |
Correct |
1 ms |
364 KB |
Ok |
35 |
Correct |
1 ms |
364 KB |
Ok |
36 |
Correct |
1 ms |
364 KB |
Ok |
37 |
Correct |
1 ms |
364 KB |
Ok |
38 |
Correct |
1 ms |
364 KB |
Ok |
39 |
Correct |
219 ms |
10708 KB |
Ok |
40 |
Correct |
203 ms |
11112 KB |
Ok |
41 |
Correct |
398 ms |
14312 KB |
Ok |
42 |
Correct |
260 ms |
12520 KB |
Ok |
43 |
Correct |
133 ms |
6640 KB |
Ok |
44 |
Correct |
226 ms |
12592 KB |
Ok |
45 |
Correct |
3 ms |
492 KB |
Ok |
46 |
Correct |
3 ms |
492 KB |
Ok |
47 |
Correct |
3 ms |
492 KB |
Ok |
48 |
Correct |
3 ms |
492 KB |
Ok |
49 |
Correct |
3 ms |
492 KB |
Ok |
50 |
Correct |
3 ms |
492 KB |
Ok |
51 |
Correct |
2 ms |
364 KB |
Ok |
52 |
Correct |
3 ms |
492 KB |
Ok |
53 |
Correct |
3 ms |
492 KB |
Ok |
54 |
Correct |
3 ms |
492 KB |
Ok |
55 |
Correct |
7 ms |
620 KB |
Ok |
56 |
Correct |
8 ms |
620 KB |
Ok |
57 |
Correct |
8 ms |
748 KB |
Ok |
58 |
Correct |
9 ms |
876 KB |
Ok |
59 |
Correct |
7 ms |
748 KB |
Ok |
60 |
Correct |
7 ms |
620 KB |
Ok |
61 |
Correct |
8 ms |
748 KB |
Ok |
62 |
Correct |
8 ms |
620 KB |
Ok |
63 |
Correct |
8 ms |
620 KB |
Ok |
64 |
Correct |
8 ms |
748 KB |
Ok |
65 |
Correct |
121 ms |
3308 KB |
Ok |
66 |
Correct |
142 ms |
3564 KB |
Ok |
67 |
Correct |
154 ms |
3692 KB |
Ok |
68 |
Correct |
97 ms |
2796 KB |
Ok |
69 |
Correct |
118 ms |
3180 KB |
Ok |
70 |
Correct |
104 ms |
2924 KB |
Ok |
71 |
Correct |
96 ms |
2796 KB |
Ok |
72 |
Correct |
95 ms |
2928 KB |
Ok |
73 |
Correct |
123 ms |
3416 KB |
Ok |
74 |
Correct |
107 ms |
2924 KB |
Ok |
75 |
Correct |
135 ms |
3564 KB |
Ok |
76 |
Correct |
120 ms |
3304 KB |
Ok |
77 |
Correct |
112 ms |
3052 KB |
Ok |
78 |
Correct |
108 ms |
2924 KB |
Ok |
79 |
Correct |
109 ms |
3052 KB |
Ok |
80 |
Correct |
284 ms |
10732 KB |
Ok |
81 |
Correct |
295 ms |
11372 KB |
Ok |
82 |
Correct |
264 ms |
10968 KB |
Ok |
83 |
Correct |
285 ms |
10732 KB |
Ok |
84 |
Correct |
266 ms |
11040 KB |
Ok |
85 |
Correct |
260 ms |
10604 KB |
Ok |
86 |
Correct |
288 ms |
10348 KB |
Ok |
87 |
Correct |
298 ms |
11116 KB |
Ok |
88 |
Correct |
255 ms |
10348 KB |
Ok |
89 |
Correct |
287 ms |
10860 KB |
Ok |
90 |
Correct |
283 ms |
10988 KB |
Ok |
91 |
Correct |
276 ms |
10988 KB |
Ok |
92 |
Correct |
298 ms |
10988 KB |
Ok |
93 |
Correct |
271 ms |
10988 KB |
Ok |
94 |
Correct |
258 ms |
10388 KB |
Ok |
95 |
Correct |
291 ms |
7784 KB |
Ok |
96 |
Correct |
413 ms |
10448 KB |
Ok |
97 |
Correct |
373 ms |
9188 KB |
Ok |
98 |
Correct |
286 ms |
8164 KB |
Ok |
99 |
Correct |
354 ms |
8676 KB |
Ok |
100 |
Correct |
374 ms |
8708 KB |
Ok |
101 |
Correct |
383 ms |
9444 KB |
Ok |
102 |
Correct |
396 ms |
9256 KB |
Ok |
103 |
Correct |
366 ms |
9188 KB |
Ok |
104 |
Correct |
464 ms |
10596 KB |
Ok |
105 |
Correct |
403 ms |
10212 KB |
Ok |
106 |
Correct |
375 ms |
9956 KB |
Ok |
107 |
Correct |
392 ms |
9828 KB |
Ok |
108 |
Correct |
439 ms |
10468 KB |
Ok |
109 |
Correct |
392 ms |
10468 KB |
Ok |
110 |
Correct |
1322 ms |
42724 KB |
Ok |
111 |
Correct |
1412 ms |
45508 KB |
Ok |
112 |
Correct |
1318 ms |
45284 KB |
Ok |
113 |
Correct |
1387 ms |
44844 KB |
Ok |
114 |
Correct |
1366 ms |
46820 KB |
Ok |
115 |
Correct |
1414 ms |
44500 KB |
Ok |
116 |
Correct |
1398 ms |
45840 KB |
Ok |
117 |
Correct |
1411 ms |
44208 KB |
Ok |
118 |
Correct |
1268 ms |
44372 KB |
Ok |
119 |
Correct |
1405 ms |
44388 KB |
Ok |
120 |
Correct |
1291 ms |
44084 KB |
Ok |
121 |
Correct |
1301 ms |
42852 KB |
Ok |
122 |
Correct |
1361 ms |
45072 KB |
Ok |
123 |
Correct |
1423 ms |
45572 KB |
Ok |
124 |
Correct |
1376 ms |
43036 KB |
Ok |
125 |
Correct |
1292 ms |
27804 KB |
Ok |