#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 l = max(n, m) - 1, r = max(n, m) + max(n, m) % min(n, m);
while (l <= r)
{
int len = (l + r) / 2;
for (int i = 0; i <= len; ++i)
{
used[i] = false;
vector<int>().swap(nei[i]);
}
for (int i = 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;
}
else
r = len - 1;
}
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:89:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < order.size(); ++i)
~~^~~~~~~~~~~~~~
sequence.cpp:91:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < pref.size(); ++i)
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Ok |
2 |
Correct |
10 ms |
9720 KB |
Ok |
3 |
Correct |
10 ms |
9720 KB |
Ok |
4 |
Correct |
10 ms |
9720 KB |
Ok |
5 |
Correct |
10 ms |
9720 KB |
Ok |
6 |
Correct |
10 ms |
9724 KB |
Ok |
7 |
Correct |
10 ms |
9720 KB |
Ok |
8 |
Correct |
10 ms |
9720 KB |
Ok |
9 |
Correct |
10 ms |
9720 KB |
Ok |
10 |
Correct |
10 ms |
9720 KB |
Ok |
11 |
Correct |
10 ms |
9720 KB |
Ok |
12 |
Correct |
10 ms |
9720 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Ok |
2 |
Correct |
10 ms |
9720 KB |
Ok |
3 |
Correct |
10 ms |
9720 KB |
Ok |
4 |
Correct |
10 ms |
9720 KB |
Ok |
5 |
Correct |
10 ms |
9720 KB |
Ok |
6 |
Correct |
14 ms |
9976 KB |
Ok |
7 |
Correct |
30 ms |
11008 KB |
Ok |
8 |
Correct |
19 ms |
10320 KB |
Ok |
9 |
Correct |
33 ms |
11300 KB |
Ok |
10 |
Correct |
25 ms |
10616 KB |
Ok |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Ok |
2 |
Correct |
10 ms |
9720 KB |
Ok |
3 |
Correct |
10 ms |
9720 KB |
Ok |
4 |
Correct |
10 ms |
9720 KB |
Ok |
5 |
Incorrect |
11 ms |
9848 KB |
Jury has the better answer : jans = 11, pans = 10 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
9720 KB |
Jury has the better answer : jans = 7, pans = 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Ok |
2 |
Correct |
10 ms |
9720 KB |
Ok |
3 |
Correct |
10 ms |
9720 KB |
Ok |
4 |
Correct |
10 ms |
9720 KB |
Ok |
5 |
Correct |
10 ms |
9720 KB |
Ok |
6 |
Correct |
10 ms |
9724 KB |
Ok |
7 |
Correct |
10 ms |
9720 KB |
Ok |
8 |
Correct |
10 ms |
9720 KB |
Ok |
9 |
Correct |
10 ms |
9720 KB |
Ok |
10 |
Correct |
10 ms |
9720 KB |
Ok |
11 |
Correct |
10 ms |
9720 KB |
Ok |
12 |
Correct |
10 ms |
9720 KB |
Ok |
13 |
Correct |
10 ms |
9720 KB |
Ok |
14 |
Correct |
10 ms |
9720 KB |
Ok |
15 |
Correct |
10 ms |
9720 KB |
Ok |
16 |
Correct |
10 ms |
9720 KB |
Ok |
17 |
Incorrect |
11 ms |
9848 KB |
Jury has the better answer : jans = 11, pans = 10 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Ok |
2 |
Correct |
10 ms |
9720 KB |
Ok |
3 |
Correct |
10 ms |
9720 KB |
Ok |
4 |
Correct |
10 ms |
9720 KB |
Ok |
5 |
Correct |
10 ms |
9720 KB |
Ok |
6 |
Correct |
10 ms |
9724 KB |
Ok |
7 |
Correct |
10 ms |
9720 KB |
Ok |
8 |
Correct |
10 ms |
9720 KB |
Ok |
9 |
Correct |
10 ms |
9720 KB |
Ok |
10 |
Correct |
10 ms |
9720 KB |
Ok |
11 |
Correct |
10 ms |
9720 KB |
Ok |
12 |
Correct |
10 ms |
9720 KB |
Ok |
13 |
Correct |
10 ms |
9720 KB |
Ok |
14 |
Correct |
10 ms |
9720 KB |
Ok |
15 |
Correct |
10 ms |
9720 KB |
Ok |
16 |
Correct |
10 ms |
9720 KB |
Ok |
17 |
Correct |
10 ms |
9720 KB |
Ok |
18 |
Correct |
14 ms |
9976 KB |
Ok |
19 |
Correct |
30 ms |
11008 KB |
Ok |
20 |
Correct |
19 ms |
10320 KB |
Ok |
21 |
Correct |
33 ms |
11300 KB |
Ok |
22 |
Correct |
25 ms |
10616 KB |
Ok |
23 |
Correct |
10 ms |
9720 KB |
Ok |
24 |
Correct |
10 ms |
9720 KB |
Ok |
25 |
Correct |
10 ms |
9720 KB |
Ok |
26 |
Correct |
10 ms |
9720 KB |
Ok |
27 |
Incorrect |
11 ms |
9848 KB |
Jury has the better answer : jans = 11, pans = 10 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Ok |
2 |
Correct |
10 ms |
9720 KB |
Ok |
3 |
Correct |
10 ms |
9720 KB |
Ok |
4 |
Correct |
10 ms |
9720 KB |
Ok |
5 |
Correct |
10 ms |
9720 KB |
Ok |
6 |
Correct |
10 ms |
9724 KB |
Ok |
7 |
Correct |
10 ms |
9720 KB |
Ok |
8 |
Correct |
10 ms |
9720 KB |
Ok |
9 |
Correct |
10 ms |
9720 KB |
Ok |
10 |
Correct |
10 ms |
9720 KB |
Ok |
11 |
Correct |
10 ms |
9720 KB |
Ok |
12 |
Correct |
10 ms |
9720 KB |
Ok |
13 |
Correct |
10 ms |
9720 KB |
Ok |
14 |
Correct |
10 ms |
9720 KB |
Ok |
15 |
Correct |
10 ms |
9720 KB |
Ok |
16 |
Correct |
10 ms |
9720 KB |
Ok |
17 |
Correct |
10 ms |
9720 KB |
Ok |
18 |
Correct |
14 ms |
9976 KB |
Ok |
19 |
Correct |
30 ms |
11008 KB |
Ok |
20 |
Correct |
19 ms |
10320 KB |
Ok |
21 |
Correct |
33 ms |
11300 KB |
Ok |
22 |
Correct |
25 ms |
10616 KB |
Ok |
23 |
Correct |
10 ms |
9720 KB |
Ok |
24 |
Correct |
10 ms |
9720 KB |
Ok |
25 |
Correct |
10 ms |
9720 KB |
Ok |
26 |
Correct |
10 ms |
9720 KB |
Ok |
27 |
Incorrect |
11 ms |
9848 KB |
Jury has the better answer : jans = 11, pans = 10 |
28 |
Halted |
0 ms |
0 KB |
- |