#include <bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
using namespace std;
//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 gen(time(0));
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long
const int N = 1e6 + 100;
const int M = 31;
const int mod = 998244353;
const int inf = 1e9 + 7;
int a[N], n, m, u[N], fl, len;
vector < int > ve;
void DFS(int v, int c)
{
if (!v) c = 0;
a[v] = c;
u[v] = 1;
if (v >= n)
{
if (u[v - n]) fl = 1;
else DFS(v - n, c - 1);
}
if (v + m <= len)
{
if (u[v + m]) fl = 1;
else DFS(v + m, c - 1);
}
if (c > 0) ve.pb(v);
}
bool good(int d)
{
len = d;
for(int i = 0; i <= len; i++)
u[i] = 0;
fl = 0;
ve.clear();
for(int i = 0; i <= len; i++)
{
if (i >= m) continue;
if (i + n <= len) continue;
DFS(i, len);
}
if (fl) return 0;
for(int i = 0; i < sz(ve); i++)
a[ve[i]] = i + 1;
for(int i = len; i >= 0; i--)
{
if (!u[i]) return 0;
if (i) a[i] -= a[i - 1];
}
return 1;
}
void solve()
{
cin >> n >> m;
int x = n, y = m;
if (n < m) swap(n, m);
if (n % m == 0)
{
cout << n - 1 << endl;
for(int i = 0; i < n - 1; i++)
{
if (x == max(x, y)) cout << "1 ";
else cout << "-1 ";
}
cout << endl;
return;
}
int l = n, r = n + m, ans = -1;
while (l <= r)
{
int mid = (l + r) / 2;
if (good(mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
good(ans);
cout << ans << endl;
for(int i = 1; i <= ans; i++)
{
if (x > y) a[i] *= -1;
cout << a[i] << " ";
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int T;
cin >> T;
while (T--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Ok |
2 |
Correct |
0 ms |
328 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
0 ms |
332 KB |
Ok |
5 |
Correct |
1 ms |
332 KB |
Ok |
6 |
Correct |
0 ms |
332 KB |
Ok |
7 |
Correct |
1 ms |
332 KB |
Ok |
8 |
Correct |
1 ms |
328 KB |
Ok |
9 |
Correct |
0 ms |
332 KB |
Ok |
10 |
Correct |
1 ms |
204 KB |
Ok |
11 |
Correct |
0 ms |
332 KB |
Ok |
12 |
Correct |
1 ms |
332 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Ok |
2 |
Correct |
1 ms |
332 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
1 ms |
204 KB |
Ok |
5 |
Correct |
1 ms |
332 KB |
Ok |
6 |
Correct |
2 ms |
444 KB |
Ok |
7 |
Correct |
5 ms |
1092 KB |
Ok |
8 |
Correct |
3 ms |
708 KB |
Ok |
9 |
Correct |
6 ms |
1220 KB |
Ok |
10 |
Correct |
4 ms |
844 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Ok |
2 |
Correct |
1 ms |
204 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
1 ms |
328 KB |
Ok |
5 |
Correct |
1 ms |
332 KB |
Ok |
6 |
Correct |
0 ms |
332 KB |
Ok |
7 |
Correct |
1 ms |
332 KB |
Ok |
8 |
Correct |
1 ms |
204 KB |
Ok |
9 |
Correct |
1 ms |
332 KB |
Ok |
10 |
Correct |
0 ms |
328 KB |
Ok |
11 |
Correct |
1 ms |
332 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Ok |
2 |
Correct |
1 ms |
356 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
1 ms |
328 KB |
Ok |
5 |
Correct |
1 ms |
308 KB |
Ok |
6 |
Correct |
273 ms |
13580 KB |
Ok |
7 |
Correct |
268 ms |
14140 KB |
Ok |
8 |
Correct |
478 ms |
17468 KB |
Ok |
9 |
Correct |
331 ms |
16008 KB |
Ok |
10 |
Correct |
212 ms |
8884 KB |
Ok |
11 |
Correct |
338 ms |
16808 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Ok |
2 |
Correct |
0 ms |
328 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
0 ms |
332 KB |
Ok |
5 |
Correct |
1 ms |
332 KB |
Ok |
6 |
Correct |
0 ms |
332 KB |
Ok |
7 |
Correct |
1 ms |
332 KB |
Ok |
8 |
Correct |
1 ms |
328 KB |
Ok |
9 |
Correct |
0 ms |
332 KB |
Ok |
10 |
Correct |
1 ms |
204 KB |
Ok |
11 |
Correct |
0 ms |
332 KB |
Ok |
12 |
Correct |
1 ms |
332 KB |
Ok |
13 |
Correct |
0 ms |
328 KB |
Ok |
14 |
Correct |
1 ms |
204 KB |
Ok |
15 |
Correct |
1 ms |
332 KB |
Ok |
16 |
Correct |
1 ms |
328 KB |
Ok |
17 |
Correct |
1 ms |
332 KB |
Ok |
18 |
Correct |
0 ms |
332 KB |
Ok |
19 |
Correct |
1 ms |
332 KB |
Ok |
20 |
Correct |
1 ms |
204 KB |
Ok |
21 |
Correct |
1 ms |
332 KB |
Ok |
22 |
Correct |
0 ms |
328 KB |
Ok |
23 |
Correct |
1 ms |
332 KB |
Ok |
24 |
Correct |
2 ms |
336 KB |
Ok |
25 |
Correct |
3 ms |
336 KB |
Ok |
26 |
Correct |
3 ms |
328 KB |
Ok |
27 |
Correct |
4 ms |
332 KB |
Ok |
28 |
Correct |
2 ms |
324 KB |
Ok |
29 |
Correct |
2 ms |
324 KB |
Ok |
30 |
Correct |
3 ms |
332 KB |
Ok |
31 |
Correct |
3 ms |
332 KB |
Ok |
32 |
Correct |
3 ms |
332 KB |
Ok |
33 |
Correct |
4 ms |
316 KB |
Ok |
34 |
Correct |
11 ms |
716 KB |
Ok |
35 |
Correct |
7 ms |
716 KB |
Ok |
36 |
Correct |
8 ms |
716 KB |
Ok |
37 |
Correct |
8 ms |
680 KB |
Ok |
38 |
Correct |
7 ms |
716 KB |
Ok |
39 |
Correct |
7 ms |
716 KB |
Ok |
40 |
Correct |
8 ms |
716 KB |
Ok |
41 |
Correct |
8 ms |
716 KB |
Ok |
42 |
Correct |
8 ms |
716 KB |
Ok |
43 |
Correct |
7 ms |
700 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Ok |
2 |
Correct |
0 ms |
328 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
0 ms |
332 KB |
Ok |
5 |
Correct |
1 ms |
332 KB |
Ok |
6 |
Correct |
0 ms |
332 KB |
Ok |
7 |
Correct |
1 ms |
332 KB |
Ok |
8 |
Correct |
1 ms |
328 KB |
Ok |
9 |
Correct |
0 ms |
332 KB |
Ok |
10 |
Correct |
1 ms |
204 KB |
Ok |
11 |
Correct |
0 ms |
332 KB |
Ok |
12 |
Correct |
1 ms |
332 KB |
Ok |
13 |
Correct |
1 ms |
204 KB |
Ok |
14 |
Correct |
1 ms |
332 KB |
Ok |
15 |
Correct |
1 ms |
332 KB |
Ok |
16 |
Correct |
1 ms |
204 KB |
Ok |
17 |
Correct |
1 ms |
332 KB |
Ok |
18 |
Correct |
2 ms |
444 KB |
Ok |
19 |
Correct |
5 ms |
1092 KB |
Ok |
20 |
Correct |
3 ms |
708 KB |
Ok |
21 |
Correct |
6 ms |
1220 KB |
Ok |
22 |
Correct |
4 ms |
844 KB |
Ok |
23 |
Correct |
0 ms |
328 KB |
Ok |
24 |
Correct |
1 ms |
204 KB |
Ok |
25 |
Correct |
1 ms |
332 KB |
Ok |
26 |
Correct |
1 ms |
328 KB |
Ok |
27 |
Correct |
1 ms |
332 KB |
Ok |
28 |
Correct |
0 ms |
332 KB |
Ok |
29 |
Correct |
1 ms |
332 KB |
Ok |
30 |
Correct |
1 ms |
204 KB |
Ok |
31 |
Correct |
1 ms |
332 KB |
Ok |
32 |
Correct |
0 ms |
328 KB |
Ok |
33 |
Correct |
1 ms |
332 KB |
Ok |
34 |
Correct |
2 ms |
336 KB |
Ok |
35 |
Correct |
3 ms |
336 KB |
Ok |
36 |
Correct |
3 ms |
328 KB |
Ok |
37 |
Correct |
4 ms |
332 KB |
Ok |
38 |
Correct |
2 ms |
324 KB |
Ok |
39 |
Correct |
2 ms |
324 KB |
Ok |
40 |
Correct |
3 ms |
332 KB |
Ok |
41 |
Correct |
3 ms |
332 KB |
Ok |
42 |
Correct |
3 ms |
332 KB |
Ok |
43 |
Correct |
4 ms |
316 KB |
Ok |
44 |
Correct |
11 ms |
716 KB |
Ok |
45 |
Correct |
7 ms |
716 KB |
Ok |
46 |
Correct |
8 ms |
716 KB |
Ok |
47 |
Correct |
8 ms |
680 KB |
Ok |
48 |
Correct |
7 ms |
716 KB |
Ok |
49 |
Correct |
7 ms |
716 KB |
Ok |
50 |
Correct |
8 ms |
716 KB |
Ok |
51 |
Correct |
8 ms |
716 KB |
Ok |
52 |
Correct |
8 ms |
716 KB |
Ok |
53 |
Correct |
7 ms |
700 KB |
Ok |
54 |
Correct |
141 ms |
3560 KB |
Ok |
55 |
Correct |
208 ms |
4144 KB |
Ok |
56 |
Correct |
185 ms |
4324 KB |
Ok |
57 |
Correct |
120 ms |
2956 KB |
Ok |
58 |
Correct |
156 ms |
3524 KB |
Ok |
59 |
Correct |
141 ms |
3256 KB |
Ok |
60 |
Correct |
108 ms |
2884 KB |
Ok |
61 |
Correct |
129 ms |
3184 KB |
Ok |
62 |
Correct |
195 ms |
3692 KB |
Ok |
63 |
Correct |
130 ms |
3264 KB |
Ok |
64 |
Correct |
180 ms |
3812 KB |
Ok |
65 |
Correct |
166 ms |
3868 KB |
Ok |
66 |
Correct |
133 ms |
3288 KB |
Ok |
67 |
Correct |
114 ms |
3408 KB |
Ok |
68 |
Correct |
156 ms |
3376 KB |
Ok |
69 |
Correct |
337 ms |
12444 KB |
Ok |
70 |
Correct |
318 ms |
12836 KB |
Ok |
71 |
Correct |
334 ms |
12588 KB |
Ok |
72 |
Correct |
335 ms |
12348 KB |
Ok |
73 |
Correct |
312 ms |
12664 KB |
Ok |
74 |
Correct |
314 ms |
12584 KB |
Ok |
75 |
Correct |
326 ms |
12620 KB |
Ok |
76 |
Correct |
328 ms |
12568 KB |
Ok |
77 |
Correct |
315 ms |
12484 KB |
Ok |
78 |
Correct |
306 ms |
12292 KB |
Ok |
79 |
Correct |
357 ms |
12708 KB |
Ok |
80 |
Correct |
310 ms |
12788 KB |
Ok |
81 |
Correct |
314 ms |
12708 KB |
Ok |
82 |
Correct |
316 ms |
12592 KB |
Ok |
83 |
Correct |
323 ms |
12628 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Ok |
2 |
Correct |
0 ms |
328 KB |
Ok |
3 |
Correct |
1 ms |
332 KB |
Ok |
4 |
Correct |
0 ms |
332 KB |
Ok |
5 |
Correct |
1 ms |
332 KB |
Ok |
6 |
Correct |
0 ms |
332 KB |
Ok |
7 |
Correct |
1 ms |
332 KB |
Ok |
8 |
Correct |
1 ms |
328 KB |
Ok |
9 |
Correct |
0 ms |
332 KB |
Ok |
10 |
Correct |
1 ms |
204 KB |
Ok |
11 |
Correct |
0 ms |
332 KB |
Ok |
12 |
Correct |
1 ms |
332 KB |
Ok |
13 |
Correct |
1 ms |
204 KB |
Ok |
14 |
Correct |
1 ms |
332 KB |
Ok |
15 |
Correct |
1 ms |
332 KB |
Ok |
16 |
Correct |
1 ms |
204 KB |
Ok |
17 |
Correct |
1 ms |
332 KB |
Ok |
18 |
Correct |
2 ms |
444 KB |
Ok |
19 |
Correct |
5 ms |
1092 KB |
Ok |
20 |
Correct |
3 ms |
708 KB |
Ok |
21 |
Correct |
6 ms |
1220 KB |
Ok |
22 |
Correct |
4 ms |
844 KB |
Ok |
23 |
Correct |
0 ms |
328 KB |
Ok |
24 |
Correct |
1 ms |
204 KB |
Ok |
25 |
Correct |
1 ms |
332 KB |
Ok |
26 |
Correct |
1 ms |
328 KB |
Ok |
27 |
Correct |
1 ms |
332 KB |
Ok |
28 |
Correct |
0 ms |
332 KB |
Ok |
29 |
Correct |
1 ms |
332 KB |
Ok |
30 |
Correct |
1 ms |
204 KB |
Ok |
31 |
Correct |
1 ms |
332 KB |
Ok |
32 |
Correct |
0 ms |
328 KB |
Ok |
33 |
Correct |
1 ms |
332 KB |
Ok |
34 |
Correct |
1 ms |
208 KB |
Ok |
35 |
Correct |
1 ms |
356 KB |
Ok |
36 |
Correct |
1 ms |
332 KB |
Ok |
37 |
Correct |
1 ms |
328 KB |
Ok |
38 |
Correct |
1 ms |
308 KB |
Ok |
39 |
Correct |
273 ms |
13580 KB |
Ok |
40 |
Correct |
268 ms |
14140 KB |
Ok |
41 |
Correct |
478 ms |
17468 KB |
Ok |
42 |
Correct |
331 ms |
16008 KB |
Ok |
43 |
Correct |
212 ms |
8884 KB |
Ok |
44 |
Correct |
338 ms |
16808 KB |
Ok |
45 |
Correct |
2 ms |
336 KB |
Ok |
46 |
Correct |
3 ms |
336 KB |
Ok |
47 |
Correct |
3 ms |
328 KB |
Ok |
48 |
Correct |
4 ms |
332 KB |
Ok |
49 |
Correct |
2 ms |
324 KB |
Ok |
50 |
Correct |
2 ms |
324 KB |
Ok |
51 |
Correct |
3 ms |
332 KB |
Ok |
52 |
Correct |
3 ms |
332 KB |
Ok |
53 |
Correct |
3 ms |
332 KB |
Ok |
54 |
Correct |
4 ms |
316 KB |
Ok |
55 |
Correct |
11 ms |
716 KB |
Ok |
56 |
Correct |
7 ms |
716 KB |
Ok |
57 |
Correct |
8 ms |
716 KB |
Ok |
58 |
Correct |
8 ms |
680 KB |
Ok |
59 |
Correct |
7 ms |
716 KB |
Ok |
60 |
Correct |
7 ms |
716 KB |
Ok |
61 |
Correct |
8 ms |
716 KB |
Ok |
62 |
Correct |
8 ms |
716 KB |
Ok |
63 |
Correct |
8 ms |
716 KB |
Ok |
64 |
Correct |
7 ms |
700 KB |
Ok |
65 |
Correct |
141 ms |
3560 KB |
Ok |
66 |
Correct |
208 ms |
4144 KB |
Ok |
67 |
Correct |
185 ms |
4324 KB |
Ok |
68 |
Correct |
120 ms |
2956 KB |
Ok |
69 |
Correct |
156 ms |
3524 KB |
Ok |
70 |
Correct |
141 ms |
3256 KB |
Ok |
71 |
Correct |
108 ms |
2884 KB |
Ok |
72 |
Correct |
129 ms |
3184 KB |
Ok |
73 |
Correct |
195 ms |
3692 KB |
Ok |
74 |
Correct |
130 ms |
3264 KB |
Ok |
75 |
Correct |
180 ms |
3812 KB |
Ok |
76 |
Correct |
166 ms |
3868 KB |
Ok |
77 |
Correct |
133 ms |
3288 KB |
Ok |
78 |
Correct |
114 ms |
3408 KB |
Ok |
79 |
Correct |
156 ms |
3376 KB |
Ok |
80 |
Correct |
337 ms |
12444 KB |
Ok |
81 |
Correct |
318 ms |
12836 KB |
Ok |
82 |
Correct |
334 ms |
12588 KB |
Ok |
83 |
Correct |
335 ms |
12348 KB |
Ok |
84 |
Correct |
312 ms |
12664 KB |
Ok |
85 |
Correct |
314 ms |
12584 KB |
Ok |
86 |
Correct |
326 ms |
12620 KB |
Ok |
87 |
Correct |
328 ms |
12568 KB |
Ok |
88 |
Correct |
315 ms |
12484 KB |
Ok |
89 |
Correct |
306 ms |
12292 KB |
Ok |
90 |
Correct |
357 ms |
12708 KB |
Ok |
91 |
Correct |
310 ms |
12788 KB |
Ok |
92 |
Correct |
314 ms |
12708 KB |
Ok |
93 |
Correct |
316 ms |
12592 KB |
Ok |
94 |
Correct |
323 ms |
12628 KB |
Ok |
95 |
Correct |
243 ms |
8484 KB |
Ok |
96 |
Correct |
509 ms |
12716 KB |
Ok |
97 |
Correct |
565 ms |
10680 KB |
Ok |
98 |
Correct |
294 ms |
8828 KB |
Ok |
99 |
Correct |
426 ms |
10356 KB |
Ok |
100 |
Correct |
526 ms |
10500 KB |
Ok |
101 |
Correct |
510 ms |
10464 KB |
Ok |
102 |
Correct |
411 ms |
10296 KB |
Ok |
103 |
Correct |
449 ms |
10904 KB |
Ok |
104 |
Correct |
614 ms |
11896 KB |
Ok |
105 |
Correct |
578 ms |
11984 KB |
Ok |
106 |
Correct |
366 ms |
11192 KB |
Ok |
107 |
Correct |
484 ms |
10552 KB |
Ok |
108 |
Correct |
603 ms |
11844 KB |
Ok |
109 |
Correct |
476 ms |
12316 KB |
Ok |
110 |
Correct |
1459 ms |
50680 KB |
Ok |
111 |
Correct |
1417 ms |
51592 KB |
Ok |
112 |
Correct |
1520 ms |
53052 KB |
Ok |
113 |
Correct |
1421 ms |
50996 KB |
Ok |
114 |
Correct |
1494 ms |
53564 KB |
Ok |
115 |
Correct |
1448 ms |
51892 KB |
Ok |
116 |
Correct |
1453 ms |
52976 KB |
Ok |
117 |
Correct |
1395 ms |
50556 KB |
Ok |
118 |
Correct |
1525 ms |
52868 KB |
Ok |
119 |
Correct |
1496 ms |
50576 KB |
Ok |
120 |
Correct |
1448 ms |
51168 KB |
Ok |
121 |
Correct |
1453 ms |
51736 KB |
Ok |
122 |
Correct |
1419 ms |
51648 KB |
Ok |
123 |
Correct |
1413 ms |
51676 KB |
Ok |
124 |
Correct |
1513 ms |
53148 KB |
Ok |
125 |
Correct |
1937 ms |
34040 KB |
Ok |