This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define isz(a) (int)(a).size()
const int NM = 4e5, LIM = 1e6;
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];
}
void 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);
while (true){
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)]);
break;
}
}
}
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 res = N+M-__gcd(N, M)-1;
check(res);
cout << res << '\n';
for (int x : v) cout << x << ' ';
cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |