#include <bits/stdc++.h>
using namespace std;
#define int long long
#define isz(a) (int)(a).size()
const int NM = 4e5, LIM = 1e9;
int T, N, M;
int parent[NM+5], sz[NM+5], f[NM+5];
vector <int> v;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_rand(int l, int r){
int tmp = rng()%(r-l+1);
if (tmp < 0) tmp += r-l+1;
return l + tmp;
}
void make_set(int v){
parent[v] = v;
sz[v] = 1;
}
int find_set(int v){
return parent[v] == v ? v : parent[v] = find_set(parent[v]);
}
void union_sets(int u, int v){
u = find_set(u);
v = find_set(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
parent[v] = u;
sz[u] += sz[v];
}
bool check(int k){
for (int i = 1; i <= k; i++) make_set(i);
for (int i = 1; i+N <= k; i++) union_sets(i, i+N);
for (int i = 1; i+M <= k; i++) union_sets(i, i+M);
for (int i = 1; i <= 5000; i++){
for (int j = 1; j <= k; j++)
if (find_set(j) == j){
while (true){
f[j] = get_rand(-LIM, LIM);
if (f[j] != 0) break;
}
}
int sum1 = 0, sum2 = 0;
for (int j = 1; j <= k; j++){
if (j <= N) sum1 += f[find_set(j)];
if (j <= M) sum2 += f[find_set(j)];
}
if ((k < N || sum1 < 0) && (k < M || sum2 > 0)){
v.clear();
for (int j = 1; j <= k; j++) v.push_back(f[find_set(j)]);
return 1;
}
}
return 0;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--){
cin >> N >> M;
if (N%M == 0){
cout << N-1 << '\n';
for (int i = 1; i < N; i++) cout << 1 << ' ';
cout << '\n';
continue;
}
if (M%N == 0){
cout << M-1 << '\n';
for (int i = 1; i < M; i++) cout << -1 << ' ';
cout << '\n';
continue;
}
int l = max(N, M)-1, r = N+M-2, res = -1;
while (l <= r){
int mid = (l+r)/2;
if (check(mid)){
res = mid;
l = mid+1;
}
else r = mid-1;
}
check(res);
cout << res << '\n';
for (int x : v) cout << x << ' ';
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Ok |
2 |
Correct |
0 ms |
348 KB |
Ok |
3 |
Correct |
0 ms |
348 KB |
Ok |
4 |
Correct |
0 ms |
348 KB |
Ok |
5 |
Correct |
0 ms |
348 KB |
Ok |
6 |
Correct |
0 ms |
464 KB |
Ok |
7 |
Correct |
1 ms |
344 KB |
Ok |
8 |
Correct |
1 ms |
348 KB |
Ok |
9 |
Correct |
1 ms |
344 KB |
Ok |
10 |
Correct |
1 ms |
344 KB |
Ok |
11 |
Correct |
0 ms |
344 KB |
Ok |
12 |
Correct |
1 ms |
348 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Ok |
2 |
Correct |
1 ms |
4444 KB |
Ok |
3 |
Correct |
1 ms |
4444 KB |
Ok |
4 |
Correct |
1 ms |
4444 KB |
Ok |
5 |
Correct |
1 ms |
4444 KB |
Ok |
6 |
Incorrect |
136 ms |
4600 KB |
Jury has the better answer : jans = 1729, pans = 1728 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Ok |
2 |
Correct |
1 ms |
348 KB |
Ok |
3 |
Correct |
2 ms |
4444 KB |
Ok |
4 |
Correct |
1 ms |
4444 KB |
Ok |
5 |
Correct |
2 ms |
4444 KB |
Ok |
6 |
Correct |
1 ms |
4692 KB |
Ok |
7 |
Correct |
3 ms |
4692 KB |
Ok |
8 |
Correct |
1 ms |
4444 KB |
Ok |
9 |
Correct |
2 ms |
4532 KB |
Ok |
10 |
Correct |
2 ms |
4444 KB |
Ok |
11 |
Correct |
3 ms |
4444 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Ok |
2 |
Correct |
3 ms |
4444 KB |
Ok |
3 |
Correct |
6 ms |
4576 KB |
Ok |
4 |
Correct |
9 ms |
4444 KB |
Ok |
5 |
Incorrect |
13 ms |
4444 KB |
Jury has the better answer : jans = 45, pans = 44 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Ok |
2 |
Correct |
0 ms |
348 KB |
Ok |
3 |
Correct |
0 ms |
348 KB |
Ok |
4 |
Correct |
0 ms |
348 KB |
Ok |
5 |
Correct |
0 ms |
348 KB |
Ok |
6 |
Correct |
0 ms |
464 KB |
Ok |
7 |
Correct |
1 ms |
344 KB |
Ok |
8 |
Correct |
1 ms |
348 KB |
Ok |
9 |
Correct |
1 ms |
344 KB |
Ok |
10 |
Correct |
1 ms |
344 KB |
Ok |
11 |
Correct |
0 ms |
344 KB |
Ok |
12 |
Correct |
1 ms |
348 KB |
Ok |
13 |
Correct |
1 ms |
4440 KB |
Ok |
14 |
Correct |
1 ms |
348 KB |
Ok |
15 |
Correct |
2 ms |
4444 KB |
Ok |
16 |
Correct |
1 ms |
4444 KB |
Ok |
17 |
Correct |
2 ms |
4444 KB |
Ok |
18 |
Correct |
1 ms |
4692 KB |
Ok |
19 |
Correct |
3 ms |
4692 KB |
Ok |
20 |
Correct |
1 ms |
4444 KB |
Ok |
21 |
Correct |
2 ms |
4532 KB |
Ok |
22 |
Correct |
2 ms |
4444 KB |
Ok |
23 |
Correct |
3 ms |
4444 KB |
Ok |
24 |
Correct |
975 ms |
4624 KB |
Ok |
25 |
Incorrect |
966 ms |
4588 KB |
Jury has the better answer : jans = 3077, pans = 3062 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Ok |
2 |
Correct |
0 ms |
348 KB |
Ok |
3 |
Correct |
0 ms |
348 KB |
Ok |
4 |
Correct |
0 ms |
348 KB |
Ok |
5 |
Correct |
0 ms |
348 KB |
Ok |
6 |
Correct |
0 ms |
464 KB |
Ok |
7 |
Correct |
1 ms |
344 KB |
Ok |
8 |
Correct |
1 ms |
348 KB |
Ok |
9 |
Correct |
1 ms |
344 KB |
Ok |
10 |
Correct |
1 ms |
344 KB |
Ok |
11 |
Correct |
0 ms |
344 KB |
Ok |
12 |
Correct |
1 ms |
348 KB |
Ok |
13 |
Correct |
1 ms |
4444 KB |
Ok |
14 |
Correct |
1 ms |
4444 KB |
Ok |
15 |
Correct |
1 ms |
4444 KB |
Ok |
16 |
Correct |
1 ms |
4444 KB |
Ok |
17 |
Correct |
1 ms |
4444 KB |
Ok |
18 |
Incorrect |
136 ms |
4600 KB |
Jury has the better answer : jans = 1729, pans = 1728 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Ok |
2 |
Correct |
0 ms |
348 KB |
Ok |
3 |
Correct |
0 ms |
348 KB |
Ok |
4 |
Correct |
0 ms |
348 KB |
Ok |
5 |
Correct |
0 ms |
348 KB |
Ok |
6 |
Correct |
0 ms |
464 KB |
Ok |
7 |
Correct |
1 ms |
344 KB |
Ok |
8 |
Correct |
1 ms |
348 KB |
Ok |
9 |
Correct |
1 ms |
344 KB |
Ok |
10 |
Correct |
1 ms |
344 KB |
Ok |
11 |
Correct |
0 ms |
344 KB |
Ok |
12 |
Correct |
1 ms |
348 KB |
Ok |
13 |
Correct |
1 ms |
4444 KB |
Ok |
14 |
Correct |
1 ms |
4444 KB |
Ok |
15 |
Correct |
1 ms |
4444 KB |
Ok |
16 |
Correct |
1 ms |
4444 KB |
Ok |
17 |
Correct |
1 ms |
4444 KB |
Ok |
18 |
Incorrect |
136 ms |
4600 KB |
Jury has the better answer : jans = 1729, pans = 1728 |
19 |
Halted |
0 ms |
0 KB |
- |