#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
char vis[maxn + 1];
int n, m;
long long pref[maxn + 1];
bool dfsCycle(int v, int maxv) {
// cerr << "v: " << v << '\n';
vis[v] = 1;
bool hasCycle = false;
if (v >= n) {
if (vis[v - n] == 1) {
hasCycle = true;
} else if (vis[v - n] == 0) {
hasCycle = hasCycle || dfsCycle(v - n, maxv);
}
}
if (v + m <= maxv) {
if (vis[v + m] == 1) {
hasCycle = true;
} else if (vis[v + m] == 0) {
hasCycle = hasCycle || dfsCycle(v + m, maxv);
}
}
vis[v] = 2;
return hasCycle;
}
vector<int> top;
void dfsTop(int v, int maxv) {
vis[v] = true;
if (v >= n && vis[v - n] == 0) {
dfsTop(v - n, maxv);
}
if (v + m <= maxv && vis[v + m] == 0) {
dfsTop(v + m, maxv);
}
top.emplace_back(v);
}
void solve() {
cin >> n >> m;
int left = 0, right = maxn + 1;
while (right - left > 1) {
int mid = left + (right - left) / 2;
for (int i = 0; i <= mid; ++i) {
vis[i] = 0;
}
bool able = true;
// cerr << "mid: " << mid << '\n';
for (int i = 0; i <= mid; ++i) {
if (vis[i] == 0) {
if (dfsCycle(i, mid)) {
able = false;
break;
}
}
}
if (able) {
left = mid;
} else {
right = mid;
}
}
if (left != 0) {
top.clear();
for (int i = 0; i <= left; ++i) {
vis[i] = false, pref[i] = 0;
}
for (int i = 0; i <= left; ++i) {
if (!vis[i]) {
dfsTop(i, left);
}
}
reverse(top.begin(), top.end());
for (const int& v : top) {
if (v >= n) {
pref[v - n] = max(pref[v - n], pref[v] + 1);
}
if (v + m <= left) {
pref[v + m] = max(pref[v + m], pref[v] + 1);
}
}
long long change = -pref[0];
for (int i = 0; i <= left; ++i) {
pref[i] += change;
}
assert(left >= 0);
for (int i = n; i <= left; ++i) {
assert(pref[i] - pref[i - n] < 0);
}
for (int i = 0; i + m <= left; ++i) {
assert(pref[i + m] - pref[i] > 0);
}
cout << left << '\n';
for (int i = 1; i <= left; ++i) {
cout << pref[i] - pref[i - 1] << ' ';
}
} else {
cout << "0\n";
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Ok |
2 |
Incorrect |
2 ms |
724 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
All the numbers must be nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Ok |
2 |
Correct |
1 ms |
724 KB |
Ok |
3 |
Incorrect |
1 ms |
724 KB |
All the numbers must be nonzero |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
All the numbers must be nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Ok |
2 |
Incorrect |
2 ms |
724 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Ok |
2 |
Incorrect |
2 ms |
724 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Ok |
2 |
Incorrect |
2 ms |
724 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |