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
const int NM = 4e5, LIM = 1e6;
int T, N, M, k;
int sum[NM+5];
int cnt[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){
cnt[u]++;
if (u-N >= 0 && cnt[u-N] < 5){
sum[u-N] = sum[u]+1;
dfs(u-N);
}
if (u+M <= k && cnt[u+M] < 5){
sum[u+M] = sum[u]+1;
dfs(u+M);
}
}
signed main(){
// freopen("TEST.inp", "r", stdin);
// freopen("TEST.out", "w", stdout);
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++) cnt[i] = 0;
for (int i = 0; i <= k; i++)
if (cnt[i] < 5){
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 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... |