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>
//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
using namespace std;
//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 gen(time(0));
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long
const int N = 1e6 + 100;
const int M = 31;
const int mod = 998244353;
const int inf = 1e9 + 7;
int a[N], n, m, u[N], can[N], fl;
vector < int > g[N];
void DFS(int v, int c)
{
if (!v) c = 0;
a[v] = c;
u[v] = 1;
for(auto to: g[v])
{
if (u[to]) fl = 1;
else DFS(to, c - 1);
}
}
bool good(int len)
{
for(int i = 0; i <= len; i++)
{
can[i] = 1;
g[i].clear();
}
for(int i = m; i <= len; i++)
{
g[i - m].pb(i);
can[i] = 0;
if (i - n >= 0)
{
g[i].pb(i - n);
can[i - n] = 0;
}
}
for(int i = 0; i <= len; i++)
u[i] = 0;
fl = 0;
for(int i = 0; i <= len; i++)
{
if (!can[i]) continue;
DFS(i, len);
}
if (fl) return 0;
for(int i = len; i >= 0; i--)
{
if (!u[i]) return 0;
if (i) a[i] -= a[i - 1];
}
return 1;
}
void solve()
{
cin >> n >> m;
int x = n, y = m;
if (n < m) swap(n, m);
if (n % m == 0)
{
cout << n - 1 << endl;
for(int i = 0; i < n - 1; i++)
{
if (x == max(x, y)) cout << "1 ";
else cout << "-1 ";
}
cout << endl;
return;
}
int l = n, r = 1e6, ans = -1;
while (l <= r)
{
int mid = (l + r) / 2;
if (good(mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
good(ans);
cout << ans << endl;
for(int i = 1; i <= ans; i++)
{
if (x > y) a[i] *= -1;
cout << a[i] << " ";
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int T;
cin >> T;
while (T--)
solve();
}
# | 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... |