//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4,avx2,fma,avx")
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define unit unsigned int
#define ld double
#define F first
#define S second
// chrono::system_clock::now().time_since_epoch().count());
using namespace std;
const int N = 4e5 + 100, IINF = 1e7, LOG = 19, K = 1 << 10;
const ll INF = 1e18 + 214;
int usd[N];
bool cycle(int v, int &n, int &m, int &sz){
if (usd[v] == sz)
return false;
usd[v] = sz;
bool fl = true;
if (v + m <= sz)
fl &= cycle(v + m, n, m, sz);
if (v - n >= 0)
fl &= cycle(v - n, n, m, sz);
return fl;
}
bool can(int n, int m, int sz){
return cycle(0, n, m, sz);
}
vector<int> top;
void dfs(int v, vector<vector<int>> &g){
if (usd[v])
return;
usd[v] = 1;
for (auto i : g[v])
dfs(i, g);
top.push_back(v);
}
int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
inline void solve(){
int n, m;
cin >> n >> m;
int start = -1;
if (n > m)
swap(n, m), start *= -1;
int l = n + m - 1 - gcd(n, m);
cout << l << '\n';
vector<vector<int>> g(l + 1);
for (int i = 0; i <= l; i ++){
if (i + m <= l)
g[i].push_back(i + m);
if (i - n >= 0)
g[i].push_back(i - n);
}
for (int i = 0; i <= l; i ++)
usd[i] = 0;
top.clear();
for (int i = 0; i <= l; i ++){
if (usd[i])
continue;
dfs(i, g);
}
//cout << "why" << '\n';
reverse(top.begin(), top.end());
vector<int> pref(l + 1);
for (int i = 1; i <= l; i ++)
pref[i] = pref[i - 1] + l + 1;
for (auto i: top){
for (auto j : g[i])
pref[j] = min(pref[j], pref[i] - 1);
}
for (int i = 1; i <= l; i ++)
cout << (pref[i] - pref[i - 1]) * start << ' ';
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while(T--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
0 ms |
212 KB |
Ok |
3 |
Correct |
0 ms |
212 KB |
Ok |
4 |
Correct |
1 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
340 KB |
Ok |
6 |
Correct |
1 ms |
212 KB |
Ok |
7 |
Correct |
0 ms |
212 KB |
Ok |
8 |
Correct |
1 ms |
212 KB |
Ok |
9 |
Correct |
1 ms |
340 KB |
Ok |
10 |
Correct |
1 ms |
212 KB |
Ok |
11 |
Correct |
0 ms |
212 KB |
Ok |
12 |
Correct |
1 ms |
212 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
1 ms |
212 KB |
Ok |
3 |
Correct |
0 ms |
212 KB |
Ok |
4 |
Correct |
1 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
212 KB |
Ok |
6 |
Correct |
3 ms |
596 KB |
Ok |
7 |
Correct |
16 ms |
1656 KB |
Ok |
8 |
Correct |
7 ms |
980 KB |
Ok |
9 |
Correct |
16 ms |
1900 KB |
Ok |
10 |
Correct |
9 ms |
1276 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
0 ms |
212 KB |
Ok |
3 |
Correct |
0 ms |
212 KB |
Ok |
4 |
Correct |
0 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
212 KB |
Ok |
6 |
Correct |
0 ms |
212 KB |
Ok |
7 |
Correct |
1 ms |
212 KB |
Ok |
8 |
Correct |
1 ms |
212 KB |
Ok |
9 |
Correct |
1 ms |
212 KB |
Ok |
10 |
Correct |
1 ms |
212 KB |
Ok |
11 |
Correct |
1 ms |
212 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
0 ms |
212 KB |
Ok |
3 |
Correct |
1 ms |
212 KB |
Ok |
4 |
Correct |
1 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
212 KB |
Ok |
6 |
Incorrect |
129 ms |
22500 KB |
Absolute value of 2147319183 bigger than 1000000 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
0 ms |
212 KB |
Ok |
3 |
Correct |
0 ms |
212 KB |
Ok |
4 |
Correct |
1 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
340 KB |
Ok |
6 |
Correct |
1 ms |
212 KB |
Ok |
7 |
Correct |
0 ms |
212 KB |
Ok |
8 |
Correct |
1 ms |
212 KB |
Ok |
9 |
Correct |
1 ms |
340 KB |
Ok |
10 |
Correct |
1 ms |
212 KB |
Ok |
11 |
Correct |
0 ms |
212 KB |
Ok |
12 |
Correct |
1 ms |
212 KB |
Ok |
13 |
Correct |
1 ms |
212 KB |
Ok |
14 |
Correct |
0 ms |
212 KB |
Ok |
15 |
Correct |
0 ms |
212 KB |
Ok |
16 |
Correct |
0 ms |
212 KB |
Ok |
17 |
Correct |
1 ms |
212 KB |
Ok |
18 |
Correct |
0 ms |
212 KB |
Ok |
19 |
Correct |
1 ms |
212 KB |
Ok |
20 |
Correct |
1 ms |
212 KB |
Ok |
21 |
Correct |
1 ms |
212 KB |
Ok |
22 |
Correct |
1 ms |
212 KB |
Ok |
23 |
Correct |
1 ms |
212 KB |
Ok |
24 |
Correct |
3 ms |
596 KB |
Ok |
25 |
Correct |
3 ms |
596 KB |
Ok |
26 |
Correct |
3 ms |
596 KB |
Ok |
27 |
Correct |
2 ms |
596 KB |
Ok |
28 |
Correct |
2 ms |
468 KB |
Ok |
29 |
Correct |
2 ms |
468 KB |
Ok |
30 |
Correct |
2 ms |
468 KB |
Ok |
31 |
Correct |
3 ms |
572 KB |
Ok |
32 |
Correct |
3 ms |
596 KB |
Ok |
33 |
Correct |
3 ms |
596 KB |
Ok |
34 |
Correct |
6 ms |
852 KB |
Ok |
35 |
Correct |
6 ms |
852 KB |
Ok |
36 |
Correct |
7 ms |
852 KB |
Ok |
37 |
Correct |
8 ms |
852 KB |
Ok |
38 |
Correct |
7 ms |
840 KB |
Ok |
39 |
Correct |
6 ms |
852 KB |
Ok |
40 |
Correct |
7 ms |
852 KB |
Ok |
41 |
Correct |
6 ms |
844 KB |
Ok |
42 |
Correct |
6 ms |
852 KB |
Ok |
43 |
Correct |
6 ms |
852 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
0 ms |
212 KB |
Ok |
3 |
Correct |
0 ms |
212 KB |
Ok |
4 |
Correct |
1 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
340 KB |
Ok |
6 |
Correct |
1 ms |
212 KB |
Ok |
7 |
Correct |
0 ms |
212 KB |
Ok |
8 |
Correct |
1 ms |
212 KB |
Ok |
9 |
Correct |
1 ms |
340 KB |
Ok |
10 |
Correct |
1 ms |
212 KB |
Ok |
11 |
Correct |
0 ms |
212 KB |
Ok |
12 |
Correct |
1 ms |
212 KB |
Ok |
13 |
Correct |
1 ms |
212 KB |
Ok |
14 |
Correct |
1 ms |
212 KB |
Ok |
15 |
Correct |
0 ms |
212 KB |
Ok |
16 |
Correct |
1 ms |
212 KB |
Ok |
17 |
Correct |
1 ms |
212 KB |
Ok |
18 |
Correct |
3 ms |
596 KB |
Ok |
19 |
Correct |
16 ms |
1656 KB |
Ok |
20 |
Correct |
7 ms |
980 KB |
Ok |
21 |
Correct |
16 ms |
1900 KB |
Ok |
22 |
Correct |
9 ms |
1276 KB |
Ok |
23 |
Correct |
1 ms |
212 KB |
Ok |
24 |
Correct |
0 ms |
212 KB |
Ok |
25 |
Correct |
0 ms |
212 KB |
Ok |
26 |
Correct |
0 ms |
212 KB |
Ok |
27 |
Correct |
1 ms |
212 KB |
Ok |
28 |
Correct |
0 ms |
212 KB |
Ok |
29 |
Correct |
1 ms |
212 KB |
Ok |
30 |
Correct |
1 ms |
212 KB |
Ok |
31 |
Correct |
1 ms |
212 KB |
Ok |
32 |
Correct |
1 ms |
212 KB |
Ok |
33 |
Correct |
1 ms |
212 KB |
Ok |
34 |
Correct |
3 ms |
596 KB |
Ok |
35 |
Correct |
3 ms |
596 KB |
Ok |
36 |
Correct |
3 ms |
596 KB |
Ok |
37 |
Correct |
2 ms |
596 KB |
Ok |
38 |
Correct |
2 ms |
468 KB |
Ok |
39 |
Correct |
2 ms |
468 KB |
Ok |
40 |
Correct |
2 ms |
468 KB |
Ok |
41 |
Correct |
3 ms |
572 KB |
Ok |
42 |
Correct |
3 ms |
596 KB |
Ok |
43 |
Correct |
3 ms |
596 KB |
Ok |
44 |
Correct |
6 ms |
852 KB |
Ok |
45 |
Correct |
6 ms |
852 KB |
Ok |
46 |
Correct |
7 ms |
852 KB |
Ok |
47 |
Correct |
8 ms |
852 KB |
Ok |
48 |
Correct |
7 ms |
840 KB |
Ok |
49 |
Correct |
6 ms |
852 KB |
Ok |
50 |
Correct |
7 ms |
852 KB |
Ok |
51 |
Correct |
6 ms |
844 KB |
Ok |
52 |
Correct |
6 ms |
852 KB |
Ok |
53 |
Correct |
6 ms |
852 KB |
Ok |
54 |
Incorrect |
94 ms |
10828 KB |
Absolute value of -1374399087 bigger than 1000000 |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Ok |
2 |
Correct |
0 ms |
212 KB |
Ok |
3 |
Correct |
0 ms |
212 KB |
Ok |
4 |
Correct |
1 ms |
212 KB |
Ok |
5 |
Correct |
1 ms |
340 KB |
Ok |
6 |
Correct |
1 ms |
212 KB |
Ok |
7 |
Correct |
0 ms |
212 KB |
Ok |
8 |
Correct |
1 ms |
212 KB |
Ok |
9 |
Correct |
1 ms |
340 KB |
Ok |
10 |
Correct |
1 ms |
212 KB |
Ok |
11 |
Correct |
0 ms |
212 KB |
Ok |
12 |
Correct |
1 ms |
212 KB |
Ok |
13 |
Correct |
1 ms |
212 KB |
Ok |
14 |
Correct |
1 ms |
212 KB |
Ok |
15 |
Correct |
0 ms |
212 KB |
Ok |
16 |
Correct |
1 ms |
212 KB |
Ok |
17 |
Correct |
1 ms |
212 KB |
Ok |
18 |
Correct |
3 ms |
596 KB |
Ok |
19 |
Correct |
16 ms |
1656 KB |
Ok |
20 |
Correct |
7 ms |
980 KB |
Ok |
21 |
Correct |
16 ms |
1900 KB |
Ok |
22 |
Correct |
9 ms |
1276 KB |
Ok |
23 |
Correct |
1 ms |
212 KB |
Ok |
24 |
Correct |
0 ms |
212 KB |
Ok |
25 |
Correct |
0 ms |
212 KB |
Ok |
26 |
Correct |
0 ms |
212 KB |
Ok |
27 |
Correct |
1 ms |
212 KB |
Ok |
28 |
Correct |
0 ms |
212 KB |
Ok |
29 |
Correct |
1 ms |
212 KB |
Ok |
30 |
Correct |
1 ms |
212 KB |
Ok |
31 |
Correct |
1 ms |
212 KB |
Ok |
32 |
Correct |
1 ms |
212 KB |
Ok |
33 |
Correct |
1 ms |
212 KB |
Ok |
34 |
Correct |
1 ms |
212 KB |
Ok |
35 |
Correct |
0 ms |
212 KB |
Ok |
36 |
Correct |
1 ms |
212 KB |
Ok |
37 |
Correct |
1 ms |
212 KB |
Ok |
38 |
Correct |
1 ms |
212 KB |
Ok |
39 |
Incorrect |
129 ms |
22500 KB |
Absolute value of 2147319183 bigger than 1000000 |
40 |
Halted |
0 ms |
0 KB |
- |