#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef double ldouble;
typedef long long llong;
typedef unsigned int uint;
const int maxN = 100001;
vector<int> nei[400001];
int a[400001];
bool used[400001];
void DFS(int v, vector<int>& order)
{
used[v] = true;
for (int to: nei[v])
if (!used[to])
DFS(to, order);
order.push_back(v);
}
bool Check(int n, int m, int len)
{
if (len >= n)
{
llong sum = 0;
for (int i = 1; i <= n; ++i)
sum += a[i];
if (sum >= 0) return false;
for (int i = 2; i + n - 1 <= len; ++i)
{
sum -= a[i - 1];
sum += a[i + n - 1];
if (sum >= 0) return false;
}
}
if (len >= m)
{
llong sum = 0;
for (int i = 1; i <= m; ++i)
sum += a[i];
if (sum <= 0) return false;
for (int i = 2; i + m - 1 <= len; ++i)
{
sum -= a[i - 1];
sum += a[i + m - 1];
if (sum <= 0) return false;
}
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
int ans = 0;
vector<int> ansArr;
int len = 0;
int i = 0;
do {
len = max(len, i += n);
while (i - m >= 0) i -= m;
} while (i);
int l = max(1, len - 1), r = len;
bool up = false;
int prevLen = 0;
while (l <= r)
{
int len = (l + r) / 2;
for (int i = 0; i <= len; ++i)
{
used[i] = false;
if (!up) vector<int>().swap(nei[i]);
}
for (int i = (up ? prevLen + 1 : 1); i <= len; ++i)
{
if (i - n >= 0)
nei[i].push_back(i - n);
if (i - m >= 0)
nei[i - m].push_back(i);
}
vector<int> order;
for (int i = 0; i <= len; ++i)
if (!used[i])
DFS(i, order);
reverse(order.begin(), order.end());
vector<int> pref(len + 1);
for (int i = 0; i < order.size(); ++i)
pref[order[i]] = i;
for (int i = 1; i < pref.size(); ++i)
pref[i] -= pref[0];
pref[0] = 0;
for (int i = 1; i <= len; ++i)
a[i] = pref[i] - pref[i - 1];
if (Check(n, m, len))
{
ans = len;
ansArr.resize(len);
for (int i = 0; i < len; ++i)
ansArr[i] = a[i + 1];
l = len + 1;
// up = true;
prevLen = len;
}
else
{
r = len - 1;
up = false;
}
}
cout << ans << endl;
for (int i = 0; i < ans; ++i)
cout << ansArr[i] << ' ';
cout << '\n';
}
}
Compilation message
sequence.cpp: In function 'int main()':
sequence.cpp:97:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < order.size(); ++i)
~~^~~~~~~~~~~~~~
sequence.cpp:99:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < pref.size(); ++i)
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Ok |
2 |
Correct |
10 ms |
9728 KB |
Ok |
3 |
Correct |
10 ms |
9728 KB |
Ok |
4 |
Correct |
10 ms |
9728 KB |
Ok |
5 |
Correct |
9 ms |
9688 KB |
Ok |
6 |
Correct |
10 ms |
9728 KB |
Ok |
7 |
Correct |
9 ms |
9728 KB |
Ok |
8 |
Correct |
10 ms |
9728 KB |
Ok |
9 |
Correct |
10 ms |
9728 KB |
Ok |
10 |
Correct |
11 ms |
9728 KB |
Ok |
11 |
Correct |
9 ms |
9728 KB |
Ok |
12 |
Correct |
9 ms |
9728 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Ok |
2 |
Correct |
12 ms |
9728 KB |
Ok |
3 |
Correct |
10 ms |
9728 KB |
Ok |
4 |
Correct |
9 ms |
9728 KB |
Ok |
5 |
Correct |
10 ms |
9728 KB |
Ok |
6 |
Correct |
13 ms |
9984 KB |
Ok |
7 |
Correct |
25 ms |
11064 KB |
Ok |
8 |
Correct |
18 ms |
10368 KB |
Ok |
9 |
Correct |
30 ms |
11268 KB |
Ok |
10 |
Correct |
21 ms |
10624 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Ok |
2 |
Correct |
9 ms |
9728 KB |
Ok |
3 |
Correct |
9 ms |
9728 KB |
Ok |
4 |
Correct |
11 ms |
9728 KB |
Ok |
5 |
Correct |
10 ms |
9856 KB |
Ok |
6 |
Correct |
9 ms |
9728 KB |
Ok |
7 |
Correct |
10 ms |
9728 KB |
Ok |
8 |
Correct |
10 ms |
9728 KB |
Ok |
9 |
Correct |
9 ms |
9728 KB |
Ok |
10 |
Correct |
9 ms |
9728 KB |
Ok |
11 |
Correct |
10 ms |
9728 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Ok |
2 |
Correct |
10 ms |
9728 KB |
Ok |
3 |
Correct |
9 ms |
9728 KB |
Ok |
4 |
Correct |
10 ms |
9728 KB |
Ok |
5 |
Correct |
10 ms |
9728 KB |
Ok |
6 |
Correct |
295 ms |
28140 KB |
Ok |
7 |
Correct |
234 ms |
30212 KB |
Ok |
8 |
Correct |
555 ms |
33428 KB |
Ok |
9 |
Correct |
348 ms |
32392 KB |
Ok |
10 |
Correct |
186 ms |
22480 KB |
Ok |
11 |
Correct |
358 ms |
34240 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Ok |
2 |
Correct |
10 ms |
9728 KB |
Ok |
3 |
Correct |
10 ms |
9728 KB |
Ok |
4 |
Correct |
10 ms |
9728 KB |
Ok |
5 |
Correct |
9 ms |
9688 KB |
Ok |
6 |
Correct |
10 ms |
9728 KB |
Ok |
7 |
Correct |
9 ms |
9728 KB |
Ok |
8 |
Correct |
10 ms |
9728 KB |
Ok |
9 |
Correct |
10 ms |
9728 KB |
Ok |
10 |
Correct |
11 ms |
9728 KB |
Ok |
11 |
Correct |
9 ms |
9728 KB |
Ok |
12 |
Correct |
9 ms |
9728 KB |
Ok |
13 |
Correct |
9 ms |
9728 KB |
Ok |
14 |
Correct |
9 ms |
9728 KB |
Ok |
15 |
Correct |
9 ms |
9728 KB |
Ok |
16 |
Correct |
11 ms |
9728 KB |
Ok |
17 |
Correct |
10 ms |
9856 KB |
Ok |
18 |
Correct |
9 ms |
9728 KB |
Ok |
19 |
Correct |
10 ms |
9728 KB |
Ok |
20 |
Correct |
10 ms |
9728 KB |
Ok |
21 |
Correct |
9 ms |
9728 KB |
Ok |
22 |
Correct |
9 ms |
9728 KB |
Ok |
23 |
Correct |
10 ms |
9728 KB |
Ok |
24 |
Correct |
12 ms |
9984 KB |
Ok |
25 |
Correct |
12 ms |
9984 KB |
Ok |
26 |
Correct |
13 ms |
9984 KB |
Ok |
27 |
Correct |
13 ms |
9984 KB |
Ok |
28 |
Correct |
12 ms |
9984 KB |
Ok |
29 |
Correct |
11 ms |
9984 KB |
Ok |
30 |
Correct |
12 ms |
9984 KB |
Ok |
31 |
Correct |
13 ms |
9984 KB |
Ok |
32 |
Correct |
12 ms |
9984 KB |
Ok |
33 |
Correct |
12 ms |
9984 KB |
Ok |
34 |
Correct |
17 ms |
10240 KB |
Ok |
35 |
Correct |
17 ms |
10240 KB |
Ok |
36 |
Correct |
16 ms |
10368 KB |
Ok |
37 |
Correct |
17 ms |
10240 KB |
Ok |
38 |
Correct |
17 ms |
10240 KB |
Ok |
39 |
Correct |
16 ms |
10240 KB |
Ok |
40 |
Correct |
17 ms |
10240 KB |
Ok |
41 |
Correct |
17 ms |
10240 KB |
Ok |
42 |
Correct |
17 ms |
10240 KB |
Ok |
43 |
Correct |
17 ms |
10368 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Ok |
2 |
Correct |
10 ms |
9728 KB |
Ok |
3 |
Correct |
10 ms |
9728 KB |
Ok |
4 |
Correct |
10 ms |
9728 KB |
Ok |
5 |
Correct |
9 ms |
9688 KB |
Ok |
6 |
Correct |
10 ms |
9728 KB |
Ok |
7 |
Correct |
9 ms |
9728 KB |
Ok |
8 |
Correct |
10 ms |
9728 KB |
Ok |
9 |
Correct |
10 ms |
9728 KB |
Ok |
10 |
Correct |
11 ms |
9728 KB |
Ok |
11 |
Correct |
9 ms |
9728 KB |
Ok |
12 |
Correct |
9 ms |
9728 KB |
Ok |
13 |
Correct |
10 ms |
9728 KB |
Ok |
14 |
Correct |
12 ms |
9728 KB |
Ok |
15 |
Correct |
10 ms |
9728 KB |
Ok |
16 |
Correct |
9 ms |
9728 KB |
Ok |
17 |
Correct |
10 ms |
9728 KB |
Ok |
18 |
Correct |
13 ms |
9984 KB |
Ok |
19 |
Correct |
25 ms |
11064 KB |
Ok |
20 |
Correct |
18 ms |
10368 KB |
Ok |
21 |
Correct |
30 ms |
11268 KB |
Ok |
22 |
Correct |
21 ms |
10624 KB |
Ok |
23 |
Correct |
9 ms |
9728 KB |
Ok |
24 |
Correct |
9 ms |
9728 KB |
Ok |
25 |
Correct |
9 ms |
9728 KB |
Ok |
26 |
Correct |
11 ms |
9728 KB |
Ok |
27 |
Correct |
10 ms |
9856 KB |
Ok |
28 |
Correct |
9 ms |
9728 KB |
Ok |
29 |
Correct |
10 ms |
9728 KB |
Ok |
30 |
Correct |
10 ms |
9728 KB |
Ok |
31 |
Correct |
9 ms |
9728 KB |
Ok |
32 |
Correct |
9 ms |
9728 KB |
Ok |
33 |
Correct |
10 ms |
9728 KB |
Ok |
34 |
Correct |
12 ms |
9984 KB |
Ok |
35 |
Correct |
12 ms |
9984 KB |
Ok |
36 |
Correct |
13 ms |
9984 KB |
Ok |
37 |
Correct |
13 ms |
9984 KB |
Ok |
38 |
Correct |
12 ms |
9984 KB |
Ok |
39 |
Correct |
11 ms |
9984 KB |
Ok |
40 |
Correct |
12 ms |
9984 KB |
Ok |
41 |
Correct |
13 ms |
9984 KB |
Ok |
42 |
Correct |
12 ms |
9984 KB |
Ok |
43 |
Correct |
12 ms |
9984 KB |
Ok |
44 |
Correct |
17 ms |
10240 KB |
Ok |
45 |
Correct |
17 ms |
10240 KB |
Ok |
46 |
Correct |
16 ms |
10368 KB |
Ok |
47 |
Correct |
17 ms |
10240 KB |
Ok |
48 |
Correct |
17 ms |
10240 KB |
Ok |
49 |
Correct |
16 ms |
10240 KB |
Ok |
50 |
Correct |
17 ms |
10240 KB |
Ok |
51 |
Correct |
17 ms |
10240 KB |
Ok |
52 |
Correct |
17 ms |
10240 KB |
Ok |
53 |
Correct |
17 ms |
10368 KB |
Ok |
54 |
Correct |
156 ms |
16728 KB |
Ok |
55 |
Correct |
191 ms |
16944 KB |
Ok |
56 |
Correct |
181 ms |
16800 KB |
Ok |
57 |
Correct |
122 ms |
15396 KB |
Ok |
58 |
Correct |
167 ms |
16464 KB |
Ok |
59 |
Correct |
165 ms |
16080 KB |
Ok |
60 |
Correct |
124 ms |
15720 KB |
Ok |
61 |
Correct |
125 ms |
15976 KB |
Ok |
62 |
Correct |
204 ms |
16780 KB |
Ok |
63 |
Correct |
149 ms |
16264 KB |
Ok |
64 |
Correct |
186 ms |
16644 KB |
Ok |
65 |
Correct |
173 ms |
16612 KB |
Ok |
66 |
Correct |
144 ms |
16204 KB |
Ok |
67 |
Correct |
123 ms |
16124 KB |
Ok |
68 |
Correct |
177 ms |
16480 KB |
Ok |
69 |
Correct |
500 ms |
26140 KB |
Ok |
70 |
Correct |
404 ms |
26240 KB |
Ok |
71 |
Correct |
389 ms |
25956 KB |
Ok |
72 |
Correct |
404 ms |
25764 KB |
Ok |
73 |
Correct |
405 ms |
25752 KB |
Ok |
74 |
Correct |
394 ms |
25372 KB |
Ok |
75 |
Correct |
381 ms |
25836 KB |
Ok |
76 |
Correct |
402 ms |
25980 KB |
Ok |
77 |
Correct |
380 ms |
26004 KB |
Ok |
78 |
Correct |
412 ms |
25900 KB |
Ok |
79 |
Correct |
419 ms |
26216 KB |
Ok |
80 |
Correct |
368 ms |
25464 KB |
Ok |
81 |
Correct |
408 ms |
26324 KB |
Ok |
82 |
Correct |
419 ms |
26028 KB |
Ok |
83 |
Correct |
404 ms |
25752 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Ok |
2 |
Correct |
10 ms |
9728 KB |
Ok |
3 |
Correct |
10 ms |
9728 KB |
Ok |
4 |
Correct |
10 ms |
9728 KB |
Ok |
5 |
Correct |
9 ms |
9688 KB |
Ok |
6 |
Correct |
10 ms |
9728 KB |
Ok |
7 |
Correct |
9 ms |
9728 KB |
Ok |
8 |
Correct |
10 ms |
9728 KB |
Ok |
9 |
Correct |
10 ms |
9728 KB |
Ok |
10 |
Correct |
11 ms |
9728 KB |
Ok |
11 |
Correct |
9 ms |
9728 KB |
Ok |
12 |
Correct |
9 ms |
9728 KB |
Ok |
13 |
Correct |
10 ms |
9728 KB |
Ok |
14 |
Correct |
12 ms |
9728 KB |
Ok |
15 |
Correct |
10 ms |
9728 KB |
Ok |
16 |
Correct |
9 ms |
9728 KB |
Ok |
17 |
Correct |
10 ms |
9728 KB |
Ok |
18 |
Correct |
13 ms |
9984 KB |
Ok |
19 |
Correct |
25 ms |
11064 KB |
Ok |
20 |
Correct |
18 ms |
10368 KB |
Ok |
21 |
Correct |
30 ms |
11268 KB |
Ok |
22 |
Correct |
21 ms |
10624 KB |
Ok |
23 |
Correct |
9 ms |
9728 KB |
Ok |
24 |
Correct |
9 ms |
9728 KB |
Ok |
25 |
Correct |
9 ms |
9728 KB |
Ok |
26 |
Correct |
11 ms |
9728 KB |
Ok |
27 |
Correct |
10 ms |
9856 KB |
Ok |
28 |
Correct |
9 ms |
9728 KB |
Ok |
29 |
Correct |
10 ms |
9728 KB |
Ok |
30 |
Correct |
10 ms |
9728 KB |
Ok |
31 |
Correct |
9 ms |
9728 KB |
Ok |
32 |
Correct |
9 ms |
9728 KB |
Ok |
33 |
Correct |
10 ms |
9728 KB |
Ok |
34 |
Correct |
10 ms |
9728 KB |
Ok |
35 |
Correct |
10 ms |
9728 KB |
Ok |
36 |
Correct |
9 ms |
9728 KB |
Ok |
37 |
Correct |
10 ms |
9728 KB |
Ok |
38 |
Correct |
10 ms |
9728 KB |
Ok |
39 |
Correct |
295 ms |
28140 KB |
Ok |
40 |
Correct |
234 ms |
30212 KB |
Ok |
41 |
Correct |
555 ms |
33428 KB |
Ok |
42 |
Correct |
348 ms |
32392 KB |
Ok |
43 |
Correct |
186 ms |
22480 KB |
Ok |
44 |
Correct |
358 ms |
34240 KB |
Ok |
45 |
Correct |
12 ms |
9984 KB |
Ok |
46 |
Correct |
12 ms |
9984 KB |
Ok |
47 |
Correct |
13 ms |
9984 KB |
Ok |
48 |
Correct |
13 ms |
9984 KB |
Ok |
49 |
Correct |
12 ms |
9984 KB |
Ok |
50 |
Correct |
11 ms |
9984 KB |
Ok |
51 |
Correct |
12 ms |
9984 KB |
Ok |
52 |
Correct |
13 ms |
9984 KB |
Ok |
53 |
Correct |
12 ms |
9984 KB |
Ok |
54 |
Correct |
12 ms |
9984 KB |
Ok |
55 |
Correct |
17 ms |
10240 KB |
Ok |
56 |
Correct |
17 ms |
10240 KB |
Ok |
57 |
Correct |
16 ms |
10368 KB |
Ok |
58 |
Correct |
17 ms |
10240 KB |
Ok |
59 |
Correct |
17 ms |
10240 KB |
Ok |
60 |
Correct |
16 ms |
10240 KB |
Ok |
61 |
Correct |
17 ms |
10240 KB |
Ok |
62 |
Correct |
17 ms |
10240 KB |
Ok |
63 |
Correct |
17 ms |
10240 KB |
Ok |
64 |
Correct |
17 ms |
10368 KB |
Ok |
65 |
Correct |
156 ms |
16728 KB |
Ok |
66 |
Correct |
191 ms |
16944 KB |
Ok |
67 |
Correct |
181 ms |
16800 KB |
Ok |
68 |
Correct |
122 ms |
15396 KB |
Ok |
69 |
Correct |
167 ms |
16464 KB |
Ok |
70 |
Correct |
165 ms |
16080 KB |
Ok |
71 |
Correct |
124 ms |
15720 KB |
Ok |
72 |
Correct |
125 ms |
15976 KB |
Ok |
73 |
Correct |
204 ms |
16780 KB |
Ok |
74 |
Correct |
149 ms |
16264 KB |
Ok |
75 |
Correct |
186 ms |
16644 KB |
Ok |
76 |
Correct |
173 ms |
16612 KB |
Ok |
77 |
Correct |
144 ms |
16204 KB |
Ok |
78 |
Correct |
123 ms |
16124 KB |
Ok |
79 |
Correct |
177 ms |
16480 KB |
Ok |
80 |
Correct |
500 ms |
26140 KB |
Ok |
81 |
Correct |
404 ms |
26240 KB |
Ok |
82 |
Correct |
389 ms |
25956 KB |
Ok |
83 |
Correct |
404 ms |
25764 KB |
Ok |
84 |
Correct |
405 ms |
25752 KB |
Ok |
85 |
Correct |
394 ms |
25372 KB |
Ok |
86 |
Correct |
381 ms |
25836 KB |
Ok |
87 |
Correct |
402 ms |
25980 KB |
Ok |
88 |
Correct |
380 ms |
26004 KB |
Ok |
89 |
Correct |
412 ms |
25900 KB |
Ok |
90 |
Correct |
419 ms |
26216 KB |
Ok |
91 |
Correct |
368 ms |
25464 KB |
Ok |
92 |
Correct |
408 ms |
26324 KB |
Ok |
93 |
Correct |
419 ms |
26028 KB |
Ok |
94 |
Correct |
404 ms |
25752 KB |
Ok |
95 |
Correct |
417 ms |
26408 KB |
Ok |
96 |
Correct |
796 ms |
36140 KB |
Ok |
97 |
Correct |
863 ms |
30356 KB |
Ok |
98 |
Correct |
417 ms |
29268 KB |
Ok |
99 |
Correct |
621 ms |
29496 KB |
Ok |
100 |
Correct |
725 ms |
30988 KB |
Ok |
101 |
Correct |
687 ms |
32872 KB |
Ok |
102 |
Correct |
602 ms |
31752 KB |
Ok |
103 |
Correct |
613 ms |
31884 KB |
Ok |
104 |
Correct |
863 ms |
34384 KB |
Ok |
105 |
Correct |
812 ms |
36532 KB |
Ok |
106 |
Correct |
507 ms |
33012 KB |
Ok |
107 |
Correct |
644 ms |
33404 KB |
Ok |
108 |
Correct |
941 ms |
35604 KB |
Ok |
109 |
Correct |
716 ms |
33944 KB |
Ok |
110 |
Execution timed out |
2078 ms |
65244 KB |
Time limit exceeded |
111 |
Halted |
0 ms |
0 KB |
- |