#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], can[N], fl;
vector < int > g[N], ve;
void DFS(int v, int c)
{
if (!v) c = 0;
a[v] = c;
u[v] = 1;
for(auto to: g[v])
{
if (u[to]) fl = 1;
else DFS(to, c - 1);
}
if (c > 0) ve.pb(v);
}
bool good(int len)
{
for(int i = 0; i <= len; i++)
{
can[i] = 1;
g[i].clear();
}
for(int i = m; i <= len; i++)
{
g[i - m].pb(i);
can[i] = 0;
if (i - n >= 0)
{
g[i].pb(i - n);
can[i - n] = 0;
}
}
for(int i = 0; i <= len; i++)
u[i] = 0;
fl = 0;
ve.clear();
for(int i = 0; i <= len; i++)
{
if (!can[i]) 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();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23884 KB |
Ok |
2 |
Correct |
13 ms |
23808 KB |
Ok |
3 |
Correct |
11 ms |
23720 KB |
Ok |
4 |
Correct |
12 ms |
23720 KB |
Ok |
5 |
Correct |
12 ms |
23756 KB |
Ok |
6 |
Correct |
12 ms |
23756 KB |
Ok |
7 |
Correct |
12 ms |
23756 KB |
Ok |
8 |
Correct |
11 ms |
23756 KB |
Ok |
9 |
Correct |
12 ms |
23756 KB |
Ok |
10 |
Correct |
12 ms |
23756 KB |
Ok |
11 |
Correct |
12 ms |
23812 KB |
Ok |
12 |
Correct |
12 ms |
23704 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Ok |
2 |
Correct |
12 ms |
23756 KB |
Ok |
3 |
Correct |
11 ms |
23820 KB |
Ok |
4 |
Correct |
15 ms |
23752 KB |
Ok |
5 |
Correct |
12 ms |
23772 KB |
Ok |
6 |
Correct |
13 ms |
23972 KB |
Ok |
7 |
Correct |
20 ms |
25072 KB |
Ok |
8 |
Correct |
15 ms |
24396 KB |
Ok |
9 |
Correct |
20 ms |
25260 KB |
Ok |
10 |
Correct |
20 ms |
24780 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Ok |
2 |
Correct |
12 ms |
23740 KB |
Ok |
3 |
Correct |
12 ms |
23756 KB |
Ok |
4 |
Correct |
12 ms |
23704 KB |
Ok |
5 |
Correct |
13 ms |
23796 KB |
Ok |
6 |
Correct |
12 ms |
23756 KB |
Ok |
7 |
Correct |
12 ms |
23764 KB |
Ok |
8 |
Correct |
12 ms |
23712 KB |
Ok |
9 |
Correct |
12 ms |
23756 KB |
Ok |
10 |
Correct |
12 ms |
23760 KB |
Ok |
11 |
Correct |
12 ms |
23708 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Ok |
2 |
Correct |
12 ms |
23756 KB |
Ok |
3 |
Correct |
12 ms |
23756 KB |
Ok |
4 |
Correct |
12 ms |
23784 KB |
Ok |
5 |
Correct |
12 ms |
23756 KB |
Ok |
6 |
Correct |
571 ms |
45184 KB |
Ok |
7 |
Correct |
414 ms |
47520 KB |
Ok |
8 |
Correct |
975 ms |
50628 KB |
Ok |
9 |
Correct |
611 ms |
47852 KB |
Ok |
10 |
Correct |
350 ms |
37920 KB |
Ok |
11 |
Correct |
630 ms |
50204 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23884 KB |
Ok |
2 |
Correct |
13 ms |
23808 KB |
Ok |
3 |
Correct |
11 ms |
23720 KB |
Ok |
4 |
Correct |
12 ms |
23720 KB |
Ok |
5 |
Correct |
12 ms |
23756 KB |
Ok |
6 |
Correct |
12 ms |
23756 KB |
Ok |
7 |
Correct |
12 ms |
23756 KB |
Ok |
8 |
Correct |
11 ms |
23756 KB |
Ok |
9 |
Correct |
12 ms |
23756 KB |
Ok |
10 |
Correct |
12 ms |
23756 KB |
Ok |
11 |
Correct |
12 ms |
23812 KB |
Ok |
12 |
Correct |
12 ms |
23704 KB |
Ok |
13 |
Correct |
12 ms |
23756 KB |
Ok |
14 |
Correct |
12 ms |
23740 KB |
Ok |
15 |
Correct |
12 ms |
23756 KB |
Ok |
16 |
Correct |
12 ms |
23704 KB |
Ok |
17 |
Correct |
13 ms |
23796 KB |
Ok |
18 |
Correct |
12 ms |
23756 KB |
Ok |
19 |
Correct |
12 ms |
23764 KB |
Ok |
20 |
Correct |
12 ms |
23712 KB |
Ok |
21 |
Correct |
12 ms |
23756 KB |
Ok |
22 |
Correct |
12 ms |
23760 KB |
Ok |
23 |
Correct |
12 ms |
23708 KB |
Ok |
24 |
Correct |
13 ms |
24012 KB |
Ok |
25 |
Correct |
18 ms |
24004 KB |
Ok |
26 |
Correct |
15 ms |
24012 KB |
Ok |
27 |
Correct |
14 ms |
24008 KB |
Ok |
28 |
Correct |
14 ms |
23984 KB |
Ok |
29 |
Correct |
13 ms |
23884 KB |
Ok |
30 |
Correct |
13 ms |
23952 KB |
Ok |
31 |
Correct |
15 ms |
24012 KB |
Ok |
32 |
Correct |
15 ms |
23996 KB |
Ok |
33 |
Correct |
15 ms |
24012 KB |
Ok |
34 |
Correct |
22 ms |
24268 KB |
Ok |
35 |
Correct |
21 ms |
24388 KB |
Ok |
36 |
Correct |
24 ms |
24364 KB |
Ok |
37 |
Correct |
22 ms |
24396 KB |
Ok |
38 |
Correct |
24 ms |
24396 KB |
Ok |
39 |
Correct |
27 ms |
24356 KB |
Ok |
40 |
Correct |
28 ms |
24280 KB |
Ok |
41 |
Correct |
23 ms |
24376 KB |
Ok |
42 |
Correct |
28 ms |
24388 KB |
Ok |
43 |
Correct |
22 ms |
24328 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23884 KB |
Ok |
2 |
Correct |
13 ms |
23808 KB |
Ok |
3 |
Correct |
11 ms |
23720 KB |
Ok |
4 |
Correct |
12 ms |
23720 KB |
Ok |
5 |
Correct |
12 ms |
23756 KB |
Ok |
6 |
Correct |
12 ms |
23756 KB |
Ok |
7 |
Correct |
12 ms |
23756 KB |
Ok |
8 |
Correct |
11 ms |
23756 KB |
Ok |
9 |
Correct |
12 ms |
23756 KB |
Ok |
10 |
Correct |
12 ms |
23756 KB |
Ok |
11 |
Correct |
12 ms |
23812 KB |
Ok |
12 |
Correct |
12 ms |
23704 KB |
Ok |
13 |
Correct |
12 ms |
23756 KB |
Ok |
14 |
Correct |
12 ms |
23756 KB |
Ok |
15 |
Correct |
11 ms |
23820 KB |
Ok |
16 |
Correct |
15 ms |
23752 KB |
Ok |
17 |
Correct |
12 ms |
23772 KB |
Ok |
18 |
Correct |
13 ms |
23972 KB |
Ok |
19 |
Correct |
20 ms |
25072 KB |
Ok |
20 |
Correct |
15 ms |
24396 KB |
Ok |
21 |
Correct |
20 ms |
25260 KB |
Ok |
22 |
Correct |
20 ms |
24780 KB |
Ok |
23 |
Correct |
12 ms |
23756 KB |
Ok |
24 |
Correct |
12 ms |
23740 KB |
Ok |
25 |
Correct |
12 ms |
23756 KB |
Ok |
26 |
Correct |
12 ms |
23704 KB |
Ok |
27 |
Correct |
13 ms |
23796 KB |
Ok |
28 |
Correct |
12 ms |
23756 KB |
Ok |
29 |
Correct |
12 ms |
23764 KB |
Ok |
30 |
Correct |
12 ms |
23712 KB |
Ok |
31 |
Correct |
12 ms |
23756 KB |
Ok |
32 |
Correct |
12 ms |
23760 KB |
Ok |
33 |
Correct |
12 ms |
23708 KB |
Ok |
34 |
Correct |
13 ms |
24012 KB |
Ok |
35 |
Correct |
18 ms |
24004 KB |
Ok |
36 |
Correct |
15 ms |
24012 KB |
Ok |
37 |
Correct |
14 ms |
24008 KB |
Ok |
38 |
Correct |
14 ms |
23984 KB |
Ok |
39 |
Correct |
13 ms |
23884 KB |
Ok |
40 |
Correct |
13 ms |
23952 KB |
Ok |
41 |
Correct |
15 ms |
24012 KB |
Ok |
42 |
Correct |
15 ms |
23996 KB |
Ok |
43 |
Correct |
15 ms |
24012 KB |
Ok |
44 |
Correct |
22 ms |
24268 KB |
Ok |
45 |
Correct |
21 ms |
24388 KB |
Ok |
46 |
Correct |
24 ms |
24364 KB |
Ok |
47 |
Correct |
22 ms |
24396 KB |
Ok |
48 |
Correct |
24 ms |
24396 KB |
Ok |
49 |
Correct |
27 ms |
24356 KB |
Ok |
50 |
Correct |
28 ms |
24280 KB |
Ok |
51 |
Correct |
23 ms |
24376 KB |
Ok |
52 |
Correct |
28 ms |
24388 KB |
Ok |
53 |
Correct |
22 ms |
24328 KB |
Ok |
54 |
Correct |
285 ms |
30196 KB |
Ok |
55 |
Correct |
426 ms |
30748 KB |
Ok |
56 |
Correct |
379 ms |
31068 KB |
Ok |
57 |
Correct |
229 ms |
29264 KB |
Ok |
58 |
Correct |
320 ms |
30168 KB |
Ok |
59 |
Correct |
272 ms |
29704 KB |
Ok |
60 |
Correct |
210 ms |
29172 KB |
Ok |
61 |
Correct |
242 ms |
29756 KB |
Ok |
62 |
Correct |
381 ms |
30352 KB |
Ok |
63 |
Correct |
259 ms |
29464 KB |
Ok |
64 |
Correct |
362 ms |
30432 KB |
Ok |
65 |
Correct |
339 ms |
30600 KB |
Ok |
66 |
Correct |
263 ms |
29884 KB |
Ok |
67 |
Correct |
202 ms |
29736 KB |
Ok |
68 |
Correct |
282 ms |
29952 KB |
Ok |
69 |
Correct |
969 ms |
40876 KB |
Ok |
70 |
Correct |
984 ms |
41396 KB |
Ok |
71 |
Correct |
1072 ms |
41032 KB |
Ok |
72 |
Correct |
920 ms |
40600 KB |
Ok |
73 |
Correct |
1020 ms |
41208 KB |
Ok |
74 |
Correct |
988 ms |
41104 KB |
Ok |
75 |
Correct |
1012 ms |
41168 KB |
Ok |
76 |
Correct |
1025 ms |
40932 KB |
Ok |
77 |
Correct |
1028 ms |
41036 KB |
Ok |
78 |
Correct |
1031 ms |
40652 KB |
Ok |
79 |
Correct |
1109 ms |
41136 KB |
Ok |
80 |
Correct |
1131 ms |
41276 KB |
Ok |
81 |
Correct |
1178 ms |
41116 KB |
Ok |
82 |
Correct |
1240 ms |
40996 KB |
Ok |
83 |
Correct |
1100 ms |
41064 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23884 KB |
Ok |
2 |
Correct |
13 ms |
23808 KB |
Ok |
3 |
Correct |
11 ms |
23720 KB |
Ok |
4 |
Correct |
12 ms |
23720 KB |
Ok |
5 |
Correct |
12 ms |
23756 KB |
Ok |
6 |
Correct |
12 ms |
23756 KB |
Ok |
7 |
Correct |
12 ms |
23756 KB |
Ok |
8 |
Correct |
11 ms |
23756 KB |
Ok |
9 |
Correct |
12 ms |
23756 KB |
Ok |
10 |
Correct |
12 ms |
23756 KB |
Ok |
11 |
Correct |
12 ms |
23812 KB |
Ok |
12 |
Correct |
12 ms |
23704 KB |
Ok |
13 |
Correct |
12 ms |
23756 KB |
Ok |
14 |
Correct |
12 ms |
23756 KB |
Ok |
15 |
Correct |
11 ms |
23820 KB |
Ok |
16 |
Correct |
15 ms |
23752 KB |
Ok |
17 |
Correct |
12 ms |
23772 KB |
Ok |
18 |
Correct |
13 ms |
23972 KB |
Ok |
19 |
Correct |
20 ms |
25072 KB |
Ok |
20 |
Correct |
15 ms |
24396 KB |
Ok |
21 |
Correct |
20 ms |
25260 KB |
Ok |
22 |
Correct |
20 ms |
24780 KB |
Ok |
23 |
Correct |
12 ms |
23756 KB |
Ok |
24 |
Correct |
12 ms |
23740 KB |
Ok |
25 |
Correct |
12 ms |
23756 KB |
Ok |
26 |
Correct |
12 ms |
23704 KB |
Ok |
27 |
Correct |
13 ms |
23796 KB |
Ok |
28 |
Correct |
12 ms |
23756 KB |
Ok |
29 |
Correct |
12 ms |
23764 KB |
Ok |
30 |
Correct |
12 ms |
23712 KB |
Ok |
31 |
Correct |
12 ms |
23756 KB |
Ok |
32 |
Correct |
12 ms |
23760 KB |
Ok |
33 |
Correct |
12 ms |
23708 KB |
Ok |
34 |
Correct |
12 ms |
23756 KB |
Ok |
35 |
Correct |
12 ms |
23756 KB |
Ok |
36 |
Correct |
12 ms |
23756 KB |
Ok |
37 |
Correct |
12 ms |
23784 KB |
Ok |
38 |
Correct |
12 ms |
23756 KB |
Ok |
39 |
Correct |
571 ms |
45184 KB |
Ok |
40 |
Correct |
414 ms |
47520 KB |
Ok |
41 |
Correct |
975 ms |
50628 KB |
Ok |
42 |
Correct |
611 ms |
47852 KB |
Ok |
43 |
Correct |
350 ms |
37920 KB |
Ok |
44 |
Correct |
630 ms |
50204 KB |
Ok |
45 |
Correct |
13 ms |
24012 KB |
Ok |
46 |
Correct |
18 ms |
24004 KB |
Ok |
47 |
Correct |
15 ms |
24012 KB |
Ok |
48 |
Correct |
14 ms |
24008 KB |
Ok |
49 |
Correct |
14 ms |
23984 KB |
Ok |
50 |
Correct |
13 ms |
23884 KB |
Ok |
51 |
Correct |
13 ms |
23952 KB |
Ok |
52 |
Correct |
15 ms |
24012 KB |
Ok |
53 |
Correct |
15 ms |
23996 KB |
Ok |
54 |
Correct |
15 ms |
24012 KB |
Ok |
55 |
Correct |
22 ms |
24268 KB |
Ok |
56 |
Correct |
21 ms |
24388 KB |
Ok |
57 |
Correct |
24 ms |
24364 KB |
Ok |
58 |
Correct |
22 ms |
24396 KB |
Ok |
59 |
Correct |
24 ms |
24396 KB |
Ok |
60 |
Correct |
27 ms |
24356 KB |
Ok |
61 |
Correct |
28 ms |
24280 KB |
Ok |
62 |
Correct |
23 ms |
24376 KB |
Ok |
63 |
Correct |
28 ms |
24388 KB |
Ok |
64 |
Correct |
22 ms |
24328 KB |
Ok |
65 |
Correct |
285 ms |
30196 KB |
Ok |
66 |
Correct |
426 ms |
30748 KB |
Ok |
67 |
Correct |
379 ms |
31068 KB |
Ok |
68 |
Correct |
229 ms |
29264 KB |
Ok |
69 |
Correct |
320 ms |
30168 KB |
Ok |
70 |
Correct |
272 ms |
29704 KB |
Ok |
71 |
Correct |
210 ms |
29172 KB |
Ok |
72 |
Correct |
242 ms |
29756 KB |
Ok |
73 |
Correct |
381 ms |
30352 KB |
Ok |
74 |
Correct |
259 ms |
29464 KB |
Ok |
75 |
Correct |
362 ms |
30432 KB |
Ok |
76 |
Correct |
339 ms |
30600 KB |
Ok |
77 |
Correct |
263 ms |
29884 KB |
Ok |
78 |
Correct |
202 ms |
29736 KB |
Ok |
79 |
Correct |
282 ms |
29952 KB |
Ok |
80 |
Correct |
969 ms |
40876 KB |
Ok |
81 |
Correct |
984 ms |
41396 KB |
Ok |
82 |
Correct |
1072 ms |
41032 KB |
Ok |
83 |
Correct |
920 ms |
40600 KB |
Ok |
84 |
Correct |
1020 ms |
41208 KB |
Ok |
85 |
Correct |
988 ms |
41104 KB |
Ok |
86 |
Correct |
1012 ms |
41168 KB |
Ok |
87 |
Correct |
1025 ms |
40932 KB |
Ok |
88 |
Correct |
1028 ms |
41036 KB |
Ok |
89 |
Correct |
1031 ms |
40652 KB |
Ok |
90 |
Correct |
1109 ms |
41136 KB |
Ok |
91 |
Correct |
1131 ms |
41276 KB |
Ok |
92 |
Correct |
1178 ms |
41116 KB |
Ok |
93 |
Correct |
1240 ms |
40996 KB |
Ok |
94 |
Correct |
1100 ms |
41064 KB |
Ok |
95 |
Correct |
683 ms |
40356 KB |
Ok |
96 |
Correct |
1380 ms |
48868 KB |
Ok |
97 |
Correct |
1319 ms |
44264 KB |
Ok |
98 |
Correct |
642 ms |
43092 KB |
Ok |
99 |
Correct |
923 ms |
44036 KB |
Ok |
100 |
Correct |
1251 ms |
43136 KB |
Ok |
101 |
Correct |
1124 ms |
45760 KB |
Ok |
102 |
Correct |
1209 ms |
43908 KB |
Ok |
103 |
Correct |
1467 ms |
45392 KB |
Ok |
104 |
Correct |
1497 ms |
47040 KB |
Ok |
105 |
Correct |
1358 ms |
47692 KB |
Ok |
106 |
Correct |
1236 ms |
47408 KB |
Ok |
107 |
Correct |
1060 ms |
45824 KB |
Ok |
108 |
Correct |
1688 ms |
46992 KB |
Ok |
109 |
Correct |
1347 ms |
48852 KB |
Ok |
110 |
Execution timed out |
2077 ms |
60280 KB |
Time limit exceeded |
111 |
Halted |
0 ms |
0 KB |
- |