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, k;
int sum[NM+5];
bool vis[NM+5];
void dfs(int u){
vis[u] = 1;
if (u-N >= 0){
sum[u-N] = sum[u]+LIM;
dfs(u-N);
}
if (u+M <= k){
sum[u+M] = sum[u]+LIM;
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;
memset(vis, 0, sizeof(vis));
for (int i = 0; i <= k; i++)
if (!vis[i]){
sum[i] = 0;
dfs(i);
}
cout << k << '\n';
for (int i = 1; i <= k; i++){
cout << (sum[i]-sum[i-1] == 0 ? 1 : 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... |