# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
633592 | fabijan_cikac | Nice sequence (IZhO18_sequence) | C++17 | 2001 ms | 48276 KiB |
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>
//#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long int
#define pb push_back
const int N = 400100;
const ll INF = 1e18;
int t, n, m; int a[N];
int v[N][2];
int u[N][2]; int tme;
void rec(int x){
if (u[x][0] != -1 && a[x] == 0) rec(u[x][0]);
if (u[x][1] != -1 && a[x] == 0) rec(u[x][1]);
a[x] = --tme;
}
int bio[N]; ll sz = 0;
bool dfs(int x){
if(bio[x]) return bio[x] == 1;
bio[x] = 1;
if(v[x][0] != -1 && dfs(v[x][0])) return 1;
if(v[x][1] != -1 && dfs(v[x][1])) return 1;
bio[x] = 2;
return 0;
}
void clear_it(int k){
for (int i = 0; i <= k; ++i){
bio[i] = 0;
v[i][0] = -1; u[i][0] = -1;
v[i][1] = -1; u[i][1] = -1;
}
}
bool good_seq(int k, int samo = 1){
if (k == 41381){
cout << "";
}
clear_it(k);
for (int i = n; i <= k; ++i){
v[i - n][0] = i; u[i][0] = (i - n);
}
for (int i = m; i <= k; ++i){
v[i][1] = (i - m); u[i - m][1] = i;
}
for (int i = 0; i <= k; ++i) a[i] = 0;
for (int i = 0; i <= k; ++i){
if (!bio[i]){
if (dfs(i)) return 0;
}
}
for (int i = 0; i <= k; ++i){
if (!bio[i]) return 0;
}
if(samo) return 1;
for (int i = 0; i <= k; ++i){
if (a[i] == 0) rec(i);
}
for (int i = 1; i <= k; ++i) a[i] -= a[0];
a[0] = 0; return 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
scanf("%d", &t); memset(v, -1, sizeof(v));
memset(u, -1, sizeof(u));
while (t--){
if (t < 0) break;
scanf("%d%d", &n, &m);
int l = 0; int r = n + m + 1;
while (l < r){
//printf("%d %d\n", l, r);
int mid = (l + r + 1) / 2;
if (good_seq(mid))
l = mid;
else r = mid - 1;
}
good_seq(l, 0);
printf("%d\n", l);
for (int i = 1; i <= l; ++i)
printf("%d ", a[i] - a[i - 1]);
printf("\n");
}
return 0;
}
Compilation message (stderr)
# | 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... |