Submission #909931

#TimeUsernameProblemLanguageResultExecution timeMemory
909931daoquanglinh2007Nice sequence (IZhO18_sequence)C++17
6 / 100
1 ms2396 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int NM = 4e5, LIM = 1e6; int T, N, M, k; int sum[NM+5]; bool vis[NM+5]; 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 dfs(int u){ vis[u] = 1; if (u+N <= k){ sum[u+N] = sum[u]+1; dfs(u+N); } if (u+M <= k){ sum[u+M] = sum[u]+1; dfs(u+M); } } 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; } k = N+M-__gcd(N, M)-1; while (true){ for (int i = 0; i <= k; i++) vis[i] = 0; for (int i = 0; i <= k; i++) if (!vis[i]){ sum[i] = get_rand(-LIM, LIM); dfs(i); } bool ok = 1; for (int i = 1; i <= k; i++) if (sum[i] == sum[i-1]) ok = 0; if (ok) break; } cout << k << '\n'; for (int i = 1; i <= k; i++){ cout << sum[i]-sum[i-1] << ' '; } 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...