#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44
using namespace std;
const int INF = 5e8;
const int MAXN = (int) 2e5;
int n, m;
char vis[2 * MAXN + 1];
void dfs(int nod, bool &ok, int len) {
vis[nod] = 1;
if(nod + n <= len) {
if(vis[nod + n] == 0) {
dfs(nod + n, ok, len);
}
else if(vis[nod + n] == 1) {
ok = 0;
}
}
if(ok && nod - m >= 0) {
if(vis[nod - m] == 0) {
dfs(nod - m, ok, len);
}
else if(vis[nod - m] == 1) {
ok = 0;
}
}
vis[nod] = 2;
}
inline bool check(int len) {
int i;
for(i = 0; i <= len; i++) {
vis[i] = 0;
}
bool ok = 1;
for(i = 0; i <= len && ok; i++) {
if(vis[i] == 0) {
dfs(i, ok, len);
}
}
return ok;
}
inline int get() {
int res = 0;
for(int step = 1 << 18; step; step >>= 1) {
if(res + step <= n + m && check(res + step)) {
res += step;
}
}
return res;
}
vector <int> ord;
int sp[2 * MAXN + 1];
void dfs1(int nod, int sz) {
vis[nod] = 1;
if(nod + n <= sz && vis[nod + n] == 0) {
dfs1(nod + n, sz);
}
if(nod - m >= 0 && vis[nod - m] == 0) {
dfs1(nod - m, sz);
}
ord.push_back(nod);
}
inline void solve(int sz) {
int i;
for(i = 0; i <= sz; i++) {
vis[i] = 0;
}
ord.clear();
for(i = 0; i <= sz; i++) {
if(vis[i] == 0) {
dfs1(i, sz);
}
}
int val = -INF;
for(auto it : ord) {
if(it == 0) {
val = max(val, 0);
sp[it] = 0;
val++;
continue;
}
if(it + n <= sz) {
val = max(val, sp[it + n] + 1);
}
if(it - m >= 0) {
val = max(val, sp[it - m]);
}
sp[it] = val;
val++;
}
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int t, i, j;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while(t > 0) {
t--;
cin >> n >> m;
int mn = min(n, m), mx = max(n, m);
if(mx % mn == 0) {
cout << mx - 1 << "\n";
int sign = 1;
if(mn == n) {
sign = -1;
}
for(i = 1; i < mx; i++) {
cout << sign << " ";
}
if(mx > 1) {
cout << "\n";
}
continue;
}
int sz = get();
solve(sz);
cout << sz << "\n";
for(i = 1; i <= sz; i++) {
cout << sp[i] - sp[i - 1] << " ";
}
cout << "\n";
}
//cin.close();
//cout.close();
return 0;
}
Compilation message
sequence.cpp: In function 'int main()':
sequence.cpp:109:15: warning: unused variable 'j' [-Wunused-variable]
int t, i, j;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
376 KB |
Ok |
5 |
Correct |
2 ms |
376 KB |
Ok |
6 |
Correct |
2 ms |
376 KB |
Ok |
7 |
Correct |
2 ms |
376 KB |
Ok |
8 |
Correct |
2 ms |
376 KB |
Ok |
9 |
Correct |
2 ms |
376 KB |
Ok |
10 |
Correct |
2 ms |
376 KB |
Ok |
11 |
Correct |
2 ms |
376 KB |
Ok |
12 |
Correct |
2 ms |
376 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
380 KB |
Ok |
5 |
Correct |
2 ms |
376 KB |
Ok |
6 |
Correct |
3 ms |
632 KB |
Ok |
7 |
Correct |
11 ms |
1400 KB |
Ok |
8 |
Correct |
6 ms |
760 KB |
Ok |
9 |
Correct |
12 ms |
1628 KB |
Ok |
10 |
Correct |
8 ms |
1016 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
376 KB |
Ok |
5 |
Correct |
2 ms |
380 KB |
Ok |
6 |
Correct |
2 ms |
376 KB |
Ok |
7 |
Correct |
2 ms |
376 KB |
Ok |
8 |
Correct |
2 ms |
376 KB |
Ok |
9 |
Correct |
2 ms |
380 KB |
Ok |
10 |
Correct |
2 ms |
376 KB |
Ok |
11 |
Correct |
2 ms |
376 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
380 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
376 KB |
Ok |
5 |
Correct |
2 ms |
376 KB |
Ok |
6 |
Correct |
128 ms |
14040 KB |
Ok |
7 |
Correct |
119 ms |
13848 KB |
Ok |
8 |
Correct |
238 ms |
18284 KB |
Ok |
9 |
Correct |
169 ms |
16740 KB |
Ok |
10 |
Correct |
87 ms |
7568 KB |
Ok |
11 |
Correct |
160 ms |
16252 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
376 KB |
Ok |
5 |
Correct |
2 ms |
376 KB |
Ok |
6 |
Correct |
2 ms |
376 KB |
Ok |
7 |
Correct |
2 ms |
376 KB |
Ok |
8 |
Correct |
2 ms |
376 KB |
Ok |
9 |
Correct |
2 ms |
376 KB |
Ok |
10 |
Correct |
2 ms |
376 KB |
Ok |
11 |
Correct |
2 ms |
376 KB |
Ok |
12 |
Correct |
2 ms |
376 KB |
Ok |
13 |
Correct |
2 ms |
376 KB |
Ok |
14 |
Correct |
2 ms |
376 KB |
Ok |
15 |
Correct |
2 ms |
376 KB |
Ok |
16 |
Correct |
2 ms |
376 KB |
Ok |
17 |
Correct |
2 ms |
380 KB |
Ok |
18 |
Correct |
2 ms |
376 KB |
Ok |
19 |
Correct |
2 ms |
376 KB |
Ok |
20 |
Correct |
2 ms |
376 KB |
Ok |
21 |
Correct |
2 ms |
380 KB |
Ok |
22 |
Correct |
2 ms |
376 KB |
Ok |
23 |
Correct |
2 ms |
376 KB |
Ok |
24 |
Correct |
3 ms |
376 KB |
Ok |
25 |
Correct |
3 ms |
376 KB |
Ok |
26 |
Correct |
3 ms |
504 KB |
Ok |
27 |
Correct |
4 ms |
504 KB |
Ok |
28 |
Correct |
3 ms |
504 KB |
Ok |
29 |
Correct |
3 ms |
476 KB |
Ok |
30 |
Correct |
3 ms |
376 KB |
Ok |
31 |
Correct |
3 ms |
504 KB |
Ok |
32 |
Correct |
3 ms |
548 KB |
Ok |
33 |
Correct |
3 ms |
504 KB |
Ok |
34 |
Correct |
6 ms |
760 KB |
Ok |
35 |
Correct |
6 ms |
760 KB |
Ok |
36 |
Correct |
6 ms |
760 KB |
Ok |
37 |
Correct |
6 ms |
760 KB |
Ok |
38 |
Correct |
6 ms |
812 KB |
Ok |
39 |
Correct |
6 ms |
732 KB |
Ok |
40 |
Correct |
6 ms |
836 KB |
Ok |
41 |
Correct |
6 ms |
888 KB |
Ok |
42 |
Correct |
6 ms |
760 KB |
Ok |
43 |
Correct |
6 ms |
760 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
376 KB |
Ok |
5 |
Correct |
2 ms |
376 KB |
Ok |
6 |
Correct |
2 ms |
376 KB |
Ok |
7 |
Correct |
2 ms |
376 KB |
Ok |
8 |
Correct |
2 ms |
376 KB |
Ok |
9 |
Correct |
2 ms |
376 KB |
Ok |
10 |
Correct |
2 ms |
376 KB |
Ok |
11 |
Correct |
2 ms |
376 KB |
Ok |
12 |
Correct |
2 ms |
376 KB |
Ok |
13 |
Correct |
2 ms |
376 KB |
Ok |
14 |
Correct |
2 ms |
376 KB |
Ok |
15 |
Correct |
2 ms |
376 KB |
Ok |
16 |
Correct |
2 ms |
380 KB |
Ok |
17 |
Correct |
2 ms |
376 KB |
Ok |
18 |
Correct |
3 ms |
632 KB |
Ok |
19 |
Correct |
11 ms |
1400 KB |
Ok |
20 |
Correct |
6 ms |
760 KB |
Ok |
21 |
Correct |
12 ms |
1628 KB |
Ok |
22 |
Correct |
8 ms |
1016 KB |
Ok |
23 |
Correct |
2 ms |
376 KB |
Ok |
24 |
Correct |
2 ms |
376 KB |
Ok |
25 |
Correct |
2 ms |
376 KB |
Ok |
26 |
Correct |
2 ms |
376 KB |
Ok |
27 |
Correct |
2 ms |
380 KB |
Ok |
28 |
Correct |
2 ms |
376 KB |
Ok |
29 |
Correct |
2 ms |
376 KB |
Ok |
30 |
Correct |
2 ms |
376 KB |
Ok |
31 |
Correct |
2 ms |
380 KB |
Ok |
32 |
Correct |
2 ms |
376 KB |
Ok |
33 |
Correct |
2 ms |
376 KB |
Ok |
34 |
Correct |
3 ms |
376 KB |
Ok |
35 |
Correct |
3 ms |
376 KB |
Ok |
36 |
Correct |
3 ms |
504 KB |
Ok |
37 |
Correct |
4 ms |
504 KB |
Ok |
38 |
Correct |
3 ms |
504 KB |
Ok |
39 |
Correct |
3 ms |
476 KB |
Ok |
40 |
Correct |
3 ms |
376 KB |
Ok |
41 |
Correct |
3 ms |
504 KB |
Ok |
42 |
Correct |
3 ms |
548 KB |
Ok |
43 |
Correct |
3 ms |
504 KB |
Ok |
44 |
Correct |
6 ms |
760 KB |
Ok |
45 |
Correct |
6 ms |
760 KB |
Ok |
46 |
Correct |
6 ms |
760 KB |
Ok |
47 |
Correct |
6 ms |
760 KB |
Ok |
48 |
Correct |
6 ms |
812 KB |
Ok |
49 |
Correct |
6 ms |
732 KB |
Ok |
50 |
Correct |
6 ms |
836 KB |
Ok |
51 |
Correct |
6 ms |
888 KB |
Ok |
52 |
Correct |
6 ms |
760 KB |
Ok |
53 |
Correct |
6 ms |
760 KB |
Ok |
54 |
Correct |
75 ms |
3084 KB |
Ok |
55 |
Correct |
89 ms |
3300 KB |
Ok |
56 |
Correct |
83 ms |
3440 KB |
Ok |
57 |
Correct |
62 ms |
2676 KB |
Ok |
58 |
Correct |
77 ms |
2804 KB |
Ok |
59 |
Correct |
69 ms |
2800 KB |
Ok |
60 |
Correct |
59 ms |
2548 KB |
Ok |
61 |
Correct |
68 ms |
2760 KB |
Ok |
62 |
Correct |
95 ms |
3060 KB |
Ok |
63 |
Correct |
64 ms |
2804 KB |
Ok |
64 |
Correct |
90 ms |
3364 KB |
Ok |
65 |
Correct |
84 ms |
3016 KB |
Ok |
66 |
Correct |
71 ms |
2832 KB |
Ok |
67 |
Correct |
60 ms |
2800 KB |
Ok |
68 |
Correct |
78 ms |
2864 KB |
Ok |
69 |
Correct |
171 ms |
13044 KB |
Ok |
70 |
Correct |
165 ms |
14708 KB |
Ok |
71 |
Correct |
168 ms |
13912 KB |
Ok |
72 |
Correct |
178 ms |
13256 KB |
Ok |
73 |
Correct |
165 ms |
14068 KB |
Ok |
74 |
Correct |
166 ms |
13460 KB |
Ok |
75 |
Correct |
171 ms |
12532 KB |
Ok |
76 |
Correct |
175 ms |
14008 KB |
Ok |
77 |
Correct |
169 ms |
12660 KB |
Ok |
78 |
Correct |
182 ms |
13684 KB |
Ok |
79 |
Correct |
186 ms |
14068 KB |
Ok |
80 |
Correct |
175 ms |
14324 KB |
Ok |
81 |
Correct |
180 ms |
13688 KB |
Ok |
82 |
Correct |
169 ms |
14068 KB |
Ok |
83 |
Correct |
181 ms |
12536 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Ok |
2 |
Correct |
2 ms |
376 KB |
Ok |
3 |
Correct |
2 ms |
376 KB |
Ok |
4 |
Correct |
2 ms |
376 KB |
Ok |
5 |
Correct |
2 ms |
376 KB |
Ok |
6 |
Correct |
2 ms |
376 KB |
Ok |
7 |
Correct |
2 ms |
376 KB |
Ok |
8 |
Correct |
2 ms |
376 KB |
Ok |
9 |
Correct |
2 ms |
376 KB |
Ok |
10 |
Correct |
2 ms |
376 KB |
Ok |
11 |
Correct |
2 ms |
376 KB |
Ok |
12 |
Correct |
2 ms |
376 KB |
Ok |
13 |
Correct |
2 ms |
376 KB |
Ok |
14 |
Correct |
2 ms |
376 KB |
Ok |
15 |
Correct |
2 ms |
376 KB |
Ok |
16 |
Correct |
2 ms |
380 KB |
Ok |
17 |
Correct |
2 ms |
376 KB |
Ok |
18 |
Correct |
3 ms |
632 KB |
Ok |
19 |
Correct |
11 ms |
1400 KB |
Ok |
20 |
Correct |
6 ms |
760 KB |
Ok |
21 |
Correct |
12 ms |
1628 KB |
Ok |
22 |
Correct |
8 ms |
1016 KB |
Ok |
23 |
Correct |
2 ms |
376 KB |
Ok |
24 |
Correct |
2 ms |
376 KB |
Ok |
25 |
Correct |
2 ms |
376 KB |
Ok |
26 |
Correct |
2 ms |
376 KB |
Ok |
27 |
Correct |
2 ms |
380 KB |
Ok |
28 |
Correct |
2 ms |
376 KB |
Ok |
29 |
Correct |
2 ms |
376 KB |
Ok |
30 |
Correct |
2 ms |
376 KB |
Ok |
31 |
Correct |
2 ms |
380 KB |
Ok |
32 |
Correct |
2 ms |
376 KB |
Ok |
33 |
Correct |
2 ms |
376 KB |
Ok |
34 |
Correct |
1 ms |
380 KB |
Ok |
35 |
Correct |
2 ms |
376 KB |
Ok |
36 |
Correct |
2 ms |
376 KB |
Ok |
37 |
Correct |
2 ms |
376 KB |
Ok |
38 |
Correct |
2 ms |
376 KB |
Ok |
39 |
Correct |
128 ms |
14040 KB |
Ok |
40 |
Correct |
119 ms |
13848 KB |
Ok |
41 |
Correct |
238 ms |
18284 KB |
Ok |
42 |
Correct |
169 ms |
16740 KB |
Ok |
43 |
Correct |
87 ms |
7568 KB |
Ok |
44 |
Correct |
160 ms |
16252 KB |
Ok |
45 |
Correct |
3 ms |
376 KB |
Ok |
46 |
Correct |
3 ms |
376 KB |
Ok |
47 |
Correct |
3 ms |
504 KB |
Ok |
48 |
Correct |
4 ms |
504 KB |
Ok |
49 |
Correct |
3 ms |
504 KB |
Ok |
50 |
Correct |
3 ms |
476 KB |
Ok |
51 |
Correct |
3 ms |
376 KB |
Ok |
52 |
Correct |
3 ms |
504 KB |
Ok |
53 |
Correct |
3 ms |
548 KB |
Ok |
54 |
Correct |
3 ms |
504 KB |
Ok |
55 |
Correct |
6 ms |
760 KB |
Ok |
56 |
Correct |
6 ms |
760 KB |
Ok |
57 |
Correct |
6 ms |
760 KB |
Ok |
58 |
Correct |
6 ms |
760 KB |
Ok |
59 |
Correct |
6 ms |
812 KB |
Ok |
60 |
Correct |
6 ms |
732 KB |
Ok |
61 |
Correct |
6 ms |
836 KB |
Ok |
62 |
Correct |
6 ms |
888 KB |
Ok |
63 |
Correct |
6 ms |
760 KB |
Ok |
64 |
Correct |
6 ms |
760 KB |
Ok |
65 |
Correct |
75 ms |
3084 KB |
Ok |
66 |
Correct |
89 ms |
3300 KB |
Ok |
67 |
Correct |
83 ms |
3440 KB |
Ok |
68 |
Correct |
62 ms |
2676 KB |
Ok |
69 |
Correct |
77 ms |
2804 KB |
Ok |
70 |
Correct |
69 ms |
2800 KB |
Ok |
71 |
Correct |
59 ms |
2548 KB |
Ok |
72 |
Correct |
68 ms |
2760 KB |
Ok |
73 |
Correct |
95 ms |
3060 KB |
Ok |
74 |
Correct |
64 ms |
2804 KB |
Ok |
75 |
Correct |
90 ms |
3364 KB |
Ok |
76 |
Correct |
84 ms |
3016 KB |
Ok |
77 |
Correct |
71 ms |
2832 KB |
Ok |
78 |
Correct |
60 ms |
2800 KB |
Ok |
79 |
Correct |
78 ms |
2864 KB |
Ok |
80 |
Correct |
171 ms |
13044 KB |
Ok |
81 |
Correct |
165 ms |
14708 KB |
Ok |
82 |
Correct |
168 ms |
13912 KB |
Ok |
83 |
Correct |
178 ms |
13256 KB |
Ok |
84 |
Correct |
165 ms |
14068 KB |
Ok |
85 |
Correct |
166 ms |
13460 KB |
Ok |
86 |
Correct |
171 ms |
12532 KB |
Ok |
87 |
Correct |
175 ms |
14008 KB |
Ok |
88 |
Correct |
169 ms |
12660 KB |
Ok |
89 |
Correct |
182 ms |
13684 KB |
Ok |
90 |
Correct |
186 ms |
14068 KB |
Ok |
91 |
Correct |
175 ms |
14324 KB |
Ok |
92 |
Correct |
180 ms |
13688 KB |
Ok |
93 |
Correct |
169 ms |
14068 KB |
Ok |
94 |
Correct |
181 ms |
12536 KB |
Ok |
95 |
Correct |
158 ms |
7276 KB |
Ok |
96 |
Correct |
270 ms |
9720 KB |
Ok |
97 |
Correct |
276 ms |
8296 KB |
Ok |
98 |
Correct |
194 ms |
7656 KB |
Ok |
99 |
Correct |
244 ms |
8092 KB |
Ok |
100 |
Correct |
245 ms |
7788 KB |
Ok |
101 |
Correct |
246 ms |
8596 KB |
Ok |
102 |
Correct |
220 ms |
8428 KB |
Ok |
103 |
Correct |
236 ms |
8552 KB |
Ok |
104 |
Correct |
283 ms |
9672 KB |
Ok |
105 |
Correct |
288 ms |
9064 KB |
Ok |
106 |
Correct |
210 ms |
9652 KB |
Ok |
107 |
Correct |
258 ms |
9064 KB |
Ok |
108 |
Correct |
295 ms |
9616 KB |
Ok |
109 |
Correct |
259 ms |
9960 KB |
Ok |
110 |
Correct |
830 ms |
51560 KB |
Ok |
111 |
Correct |
1018 ms |
56436 KB |
Ok |
112 |
Correct |
946 ms |
56808 KB |
Ok |
113 |
Correct |
982 ms |
53616 KB |
Ok |
114 |
Correct |
892 ms |
57896 KB |
Ok |
115 |
Correct |
941 ms |
54176 KB |
Ok |
116 |
Correct |
941 ms |
57324 KB |
Ok |
117 |
Correct |
1012 ms |
52572 KB |
Ok |
118 |
Correct |
921 ms |
53816 KB |
Ok |
119 |
Correct |
1223 ms |
53220 KB |
Ok |
120 |
Correct |
879 ms |
52844 KB |
Ok |
121 |
Correct |
1135 ms |
51048 KB |
Ok |
122 |
Correct |
974 ms |
55888 KB |
Ok |
123 |
Correct |
1130 ms |
55660 KB |
Ok |
124 |
Correct |
869 ms |
51664 KB |
Ok |
125 |
Correct |
795 ms |
32488 KB |
Ok |