Submission #909898

#TimeUsernameProblemLanguageResultExecution timeMemory
909898daoquanglinh2007Nice sequence (IZhO18_sequence)C++17
20 / 100
975 ms4692 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...