#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define ft first
#define sd second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pll pair<ll, ll>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 200100;
vector<int> ts;
int n, m, len, mrk[N], pf[N];
bool cyc;
void dfs(int v){
if (cyc) return;
mrk[v] = 1;
if (v >= m){
if (mrk[v - m] == 0)
dfs(v - m);
else if (mrk[v - m] == 1){
cyc = 1;
return;
}
}
if (v + n <= len){
if (mrk[v + n] == 0)
dfs(v + n);
else if (mrk[v + n] == 1){
cyc = 1;
return;
}
}
mrk[v] = 2;
}
bool ok(){
fill(mrk, mrk + len + 1, 0);
cyc = 0;
for (int i = 0; i <= len && !cyc; i++)
if (mrk[i] == 0)
dfs(i);
return !cyc;
}
void DFS(int v){
mrk[v] = 1;
if (v >= m){
if (mrk[v - m] == 0)
DFS(v - m);
}
if (v + n <= len){
if (mrk[v + n] == 0)
DFS(v + n);
}
ts.PB(v);
}
int main(){
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif // _LOCAL
int qq; cin >> qq;
for (; qq; qq--){
cin >> n >> m;
int l = 0, r = (n + m) * 2;
while (l < r){
int md = (l + r + 1) >> 1;
len = md;
if (ok())
l = md;
else r = md - 1;
}
cout << l << '\n';
len = l;
fill(mrk, mrk + len + 1, 0);
fill(pf, pf + len + 1, -1);
ts.clear();
for (int i = 0; i <= len; i++)
if (!mrk[i])
DFS(i);
for (int v : ts){
int mx = -1;
if (v >= m)
mx = max(mx, pf[v - m]);
if (v + n <= len)
mx = max(mx, pf[v + n]);
pf[v] = mx + 1;
}
for (int i = 1; i <= len; i++)
cout << pf[i] - pf[i - 1] << " ";
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Ok |
2 |
Incorrect |
5 ms |
384 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
All the numbers must be nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Ok |
2 |
Correct |
5 ms |
384 KB |
Ok |
3 |
Incorrect |
4 ms |
384 KB |
All the numbers must be nonzero |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
All the numbers must be nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Ok |
2 |
Incorrect |
5 ms |
384 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Ok |
2 |
Incorrect |
5 ms |
384 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Ok |
2 |
Incorrect |
5 ms |
384 KB |
All the numbers must be nonzero |
3 |
Halted |
0 ms |
0 KB |
- |