Submission #173968

# Submission time Handle Problem Language Result Execution time Memory
173968 2020-01-05T22:00:07 Z mcdx9524 Nice sequence (IZhO18_sequence) C++14
76 / 100
2000 ms 36476 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

int rand(int l, int r) {
    return uniform_int_distribution<int> (l, r) (rnd);
}

const int N = 5e5 + 7;

int n, m;
vector <int> p;
vector <int> ord;
vector <int> g[N];
bool used[N];
int col[N];

bool cycle(int v) {
    col[v] = 1;
    for (int u : g[v]) {
        if (col[u] == 0) {
            if (cycle(u)) {
                return true;
            }
        } else if (col[u] == 1) {
            return true;
        }
    }
    col[v] = 2;
    return false;
}

void dfs(int v) {
    if (used[v] == true) {
        return;
    }
    used[v] = true;
    for (int u : g[v]) {
        dfs(u);
    }
    ord.push_back(v);
}

bool build(int sz, bool kek) {
    ord.clear();
    for (int i = 0; i < N; i++) {
        g[i].clear();
        used[i] = false;
        col[i] = 0;
    }
    for (int i = 1; i <= sz; i++) {
        // p[i] - p[i - n] < 0
        // p[i] < p[i - n]
        if (i - n >= 0) {
            g[i - n].push_back(i);
        }
        // p[i] > p[i - m]
        if (i - m >= 0) {
            g[i].push_back(i - m);
        }
    }
    for (int i = 0; i <= sz; i++) {
        if (cycle(i)) {
            return false;
        }
    }
    if (!kek) return true;
    for (int i = 0; i <= sz; i++) {
        dfs(i);
    }
    p.resize(sz + 1, 0);
    int cur = sz;
    reverse(ord.begin(), ord.end());
    for (int x : ord) {
        p[x] = cur--;
    }
    for (int i = 0; i <= sz; i++) {
        if (!p[i]) {
            p[i] = cur--;
        }
    }
    return true;
}

void solve() {
    cin >> n >> m;
    int l = max(n, m) - 1, r = n + m + 7;
    int ans = l;
    while (l <= r) {
        int md = rand(l, r);
        if (build(md, 0)) {
            l = md + 1;
            ans = md;
        } else {
            r = md - 1;
        }
    }
    build(ans, 1);
    cout << ans << '\n';
    for (int i = 1; i <= ans; i++) {
        cout << p[i] - p[i - 1] << ' ';
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 67 ms 14584 KB Ok
2 Correct 81 ms 14584 KB Ok
3 Correct 87 ms 14604 KB Ok
4 Correct 108 ms 14456 KB Ok
5 Correct 87 ms 14584 KB Ok
6 Correct 90 ms 14512 KB Ok
7 Correct 92 ms 14456 KB Ok
8 Correct 92 ms 14584 KB Ok
9 Correct 101 ms 14456 KB Ok
10 Correct 88 ms 14456 KB Ok
11 Correct 93 ms 14584 KB Ok
12 Correct 95 ms 14584 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 79 ms 14456 KB Ok
2 Correct 76 ms 14584 KB Ok
3 Correct 109 ms 14584 KB Ok
4 Correct 77 ms 14456 KB Ok
5 Correct 82 ms 14588 KB Ok
6 Correct 92 ms 14748 KB Ok
7 Correct 101 ms 15608 KB Ok
8 Correct 102 ms 15012 KB Ok
9 Correct 107 ms 15864 KB Ok
10 Correct 85 ms 15224 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 28 ms 14604 KB Ok
2 Correct 70 ms 14456 KB Ok
3 Correct 84 ms 14604 KB Ok
4 Correct 79 ms 14584 KB Ok
5 Correct 77 ms 14456 KB Ok
6 Correct 86 ms 14456 KB Ok
7 Correct 82 ms 14584 KB Ok
8 Correct 83 ms 14584 KB Ok
9 Correct 82 ms 14456 KB Ok
10 Correct 96 ms 14712 KB Ok
11 Correct 77 ms 14584 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 84 ms 14604 KB Ok
2 Correct 107 ms 14568 KB Ok
3 Correct 103 ms 14584 KB Ok
4 Correct 120 ms 14568 KB Ok
5 Correct 128 ms 14712 KB Ok
6 Correct 662 ms 29548 KB Ok
7 Correct 629 ms 33836 KB Ok
8 Correct 837 ms 36476 KB Ok
9 Correct 731 ms 33708 KB Ok
10 Correct 452 ms 24476 KB Ok
11 Correct 678 ms 32112 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 67 ms 14584 KB Ok
2 Correct 81 ms 14584 KB Ok
3 Correct 87 ms 14604 KB Ok
4 Correct 108 ms 14456 KB Ok
5 Correct 87 ms 14584 KB Ok
6 Correct 90 ms 14512 KB Ok
7 Correct 92 ms 14456 KB Ok
8 Correct 92 ms 14584 KB Ok
9 Correct 101 ms 14456 KB Ok
10 Correct 88 ms 14456 KB Ok
11 Correct 93 ms 14584 KB Ok
12 Correct 95 ms 14584 KB Ok
13 Correct 28 ms 14604 KB Ok
14 Correct 70 ms 14456 KB Ok
15 Correct 84 ms 14604 KB Ok
16 Correct 79 ms 14584 KB Ok
17 Correct 77 ms 14456 KB Ok
18 Correct 86 ms 14456 KB Ok
19 Correct 82 ms 14584 KB Ok
20 Correct 83 ms 14584 KB Ok
21 Correct 82 ms 14456 KB Ok
22 Correct 96 ms 14712 KB Ok
23 Correct 77 ms 14584 KB Ok
24 Correct 178 ms 14712 KB Ok
25 Correct 159 ms 14824 KB Ok
26 Correct 148 ms 14712 KB Ok
27 Correct 169 ms 14864 KB Ok
28 Correct 212 ms 14684 KB Ok
29 Correct 171 ms 14760 KB Ok
30 Correct 142 ms 14712 KB Ok
31 Correct 160 ms 14712 KB Ok
32 Correct 178 ms 14712 KB Ok
33 Correct 152 ms 14748 KB Ok
34 Correct 186 ms 15044 KB Ok
35 Correct 155 ms 14956 KB Ok
36 Correct 166 ms 14996 KB Ok
37 Correct 159 ms 14964 KB Ok
38 Correct 174 ms 15072 KB Ok
39 Correct 180 ms 14872 KB Ok
40 Correct 158 ms 15028 KB Ok
41 Correct 207 ms 15096 KB Ok
42 Correct 183 ms 14968 KB Ok
43 Correct 191 ms 15072 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 67 ms 14584 KB Ok
2 Correct 81 ms 14584 KB Ok
3 Correct 87 ms 14604 KB Ok
4 Correct 108 ms 14456 KB Ok
5 Correct 87 ms 14584 KB Ok
6 Correct 90 ms 14512 KB Ok
7 Correct 92 ms 14456 KB Ok
8 Correct 92 ms 14584 KB Ok
9 Correct 101 ms 14456 KB Ok
10 Correct 88 ms 14456 KB Ok
11 Correct 93 ms 14584 KB Ok
12 Correct 95 ms 14584 KB Ok
13 Correct 79 ms 14456 KB Ok
14 Correct 76 ms 14584 KB Ok
15 Correct 109 ms 14584 KB Ok
16 Correct 77 ms 14456 KB Ok
17 Correct 82 ms 14588 KB Ok
18 Correct 92 ms 14748 KB Ok
19 Correct 101 ms 15608 KB Ok
20 Correct 102 ms 15012 KB Ok
21 Correct 107 ms 15864 KB Ok
22 Correct 85 ms 15224 KB Ok
23 Correct 28 ms 14604 KB Ok
24 Correct 70 ms 14456 KB Ok
25 Correct 84 ms 14604 KB Ok
26 Correct 79 ms 14584 KB Ok
27 Correct 77 ms 14456 KB Ok
28 Correct 86 ms 14456 KB Ok
29 Correct 82 ms 14584 KB Ok
30 Correct 83 ms 14584 KB Ok
31 Correct 82 ms 14456 KB Ok
32 Correct 96 ms 14712 KB Ok
33 Correct 77 ms 14584 KB Ok
34 Correct 178 ms 14712 KB Ok
35 Correct 159 ms 14824 KB Ok
36 Correct 148 ms 14712 KB Ok
37 Correct 169 ms 14864 KB Ok
38 Correct 212 ms 14684 KB Ok
39 Correct 171 ms 14760 KB Ok
40 Correct 142 ms 14712 KB Ok
41 Correct 160 ms 14712 KB Ok
42 Correct 178 ms 14712 KB Ok
43 Correct 152 ms 14748 KB Ok
44 Correct 186 ms 15044 KB Ok
45 Correct 155 ms 14956 KB Ok
46 Correct 166 ms 14996 KB Ok
47 Correct 159 ms 14964 KB Ok
48 Correct 174 ms 15072 KB Ok
49 Correct 180 ms 14872 KB Ok
50 Correct 158 ms 15028 KB Ok
51 Correct 207 ms 15096 KB Ok
52 Correct 183 ms 14968 KB Ok
53 Correct 191 ms 15072 KB Ok
54 Correct 561 ms 19856 KB Ok
55 Correct 718 ms 20300 KB Ok
56 Correct 841 ms 20456 KB Ok
57 Correct 456 ms 19288 KB Ok
58 Correct 622 ms 19704 KB Ok
59 Correct 496 ms 19332 KB Ok
60 Correct 453 ms 19064 KB Ok
61 Correct 567 ms 19700 KB Ok
62 Correct 646 ms 20040 KB Ok
63 Correct 561 ms 19520 KB Ok
64 Correct 755 ms 20140 KB Ok
65 Correct 703 ms 19888 KB Ok
66 Correct 606 ms 19768 KB Ok
67 Correct 584 ms 19584 KB Ok
68 Correct 544 ms 19780 KB Ok
69 Correct 1342 ms 29224 KB Ok
70 Correct 1195 ms 29800 KB Ok
71 Correct 1173 ms 29032 KB Ok
72 Correct 1141 ms 29300 KB Ok
73 Correct 1234 ms 28636 KB Ok
74 Correct 1355 ms 28384 KB Ok
75 Correct 1328 ms 28536 KB Ok
76 Correct 1272 ms 29404 KB Ok
77 Correct 1225 ms 27876 KB Ok
78 Correct 1528 ms 28152 KB Ok
79 Correct 1182 ms 28892 KB Ok
80 Correct 1230 ms 29228 KB Ok
81 Correct 1303 ms 28304 KB Ok
82 Correct 1465 ms 28904 KB Ok
83 Correct 1260 ms 28392 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 67 ms 14584 KB Ok
2 Correct 81 ms 14584 KB Ok
3 Correct 87 ms 14604 KB Ok
4 Correct 108 ms 14456 KB Ok
5 Correct 87 ms 14584 KB Ok
6 Correct 90 ms 14512 KB Ok
7 Correct 92 ms 14456 KB Ok
8 Correct 92 ms 14584 KB Ok
9 Correct 101 ms 14456 KB Ok
10 Correct 88 ms 14456 KB Ok
11 Correct 93 ms 14584 KB Ok
12 Correct 95 ms 14584 KB Ok
13 Correct 79 ms 14456 KB Ok
14 Correct 76 ms 14584 KB Ok
15 Correct 109 ms 14584 KB Ok
16 Correct 77 ms 14456 KB Ok
17 Correct 82 ms 14588 KB Ok
18 Correct 92 ms 14748 KB Ok
19 Correct 101 ms 15608 KB Ok
20 Correct 102 ms 15012 KB Ok
21 Correct 107 ms 15864 KB Ok
22 Correct 85 ms 15224 KB Ok
23 Correct 28 ms 14604 KB Ok
24 Correct 70 ms 14456 KB Ok
25 Correct 84 ms 14604 KB Ok
26 Correct 79 ms 14584 KB Ok
27 Correct 77 ms 14456 KB Ok
28 Correct 86 ms 14456 KB Ok
29 Correct 82 ms 14584 KB Ok
30 Correct 83 ms 14584 KB Ok
31 Correct 82 ms 14456 KB Ok
32 Correct 96 ms 14712 KB Ok
33 Correct 77 ms 14584 KB Ok
34 Correct 84 ms 14604 KB Ok
35 Correct 107 ms 14568 KB Ok
36 Correct 103 ms 14584 KB Ok
37 Correct 120 ms 14568 KB Ok
38 Correct 128 ms 14712 KB Ok
39 Correct 662 ms 29548 KB Ok
40 Correct 629 ms 33836 KB Ok
41 Correct 837 ms 36476 KB Ok
42 Correct 731 ms 33708 KB Ok
43 Correct 452 ms 24476 KB Ok
44 Correct 678 ms 32112 KB Ok
45 Correct 178 ms 14712 KB Ok
46 Correct 159 ms 14824 KB Ok
47 Correct 148 ms 14712 KB Ok
48 Correct 169 ms 14864 KB Ok
49 Correct 212 ms 14684 KB Ok
50 Correct 171 ms 14760 KB Ok
51 Correct 142 ms 14712 KB Ok
52 Correct 160 ms 14712 KB Ok
53 Correct 178 ms 14712 KB Ok
54 Correct 152 ms 14748 KB Ok
55 Correct 186 ms 15044 KB Ok
56 Correct 155 ms 14956 KB Ok
57 Correct 166 ms 14996 KB Ok
58 Correct 159 ms 14964 KB Ok
59 Correct 174 ms 15072 KB Ok
60 Correct 180 ms 14872 KB Ok
61 Correct 158 ms 15028 KB Ok
62 Correct 207 ms 15096 KB Ok
63 Correct 183 ms 14968 KB Ok
64 Correct 191 ms 15072 KB Ok
65 Correct 561 ms 19856 KB Ok
66 Correct 718 ms 20300 KB Ok
67 Correct 841 ms 20456 KB Ok
68 Correct 456 ms 19288 KB Ok
69 Correct 622 ms 19704 KB Ok
70 Correct 496 ms 19332 KB Ok
71 Correct 453 ms 19064 KB Ok
72 Correct 567 ms 19700 KB Ok
73 Correct 646 ms 20040 KB Ok
74 Correct 561 ms 19520 KB Ok
75 Correct 755 ms 20140 KB Ok
76 Correct 703 ms 19888 KB Ok
77 Correct 606 ms 19768 KB Ok
78 Correct 584 ms 19584 KB Ok
79 Correct 544 ms 19780 KB Ok
80 Correct 1342 ms 29224 KB Ok
81 Correct 1195 ms 29800 KB Ok
82 Correct 1173 ms 29032 KB Ok
83 Correct 1141 ms 29300 KB Ok
84 Correct 1234 ms 28636 KB Ok
85 Correct 1355 ms 28384 KB Ok
86 Correct 1328 ms 28536 KB Ok
87 Correct 1272 ms 29404 KB Ok
88 Correct 1225 ms 27876 KB Ok
89 Correct 1528 ms 28152 KB Ok
90 Correct 1182 ms 28892 KB Ok
91 Correct 1230 ms 29228 KB Ok
92 Correct 1303 ms 28304 KB Ok
93 Correct 1465 ms 28904 KB Ok
94 Correct 1260 ms 28392 KB Ok
95 Correct 1102 ms 29200 KB Ok
96 Correct 1577 ms 34936 KB Ok
97 Correct 1670 ms 31600 KB Ok
98 Correct 956 ms 30972 KB Ok
99 Correct 1548 ms 31772 KB Ok
100 Correct 1832 ms 31492 KB Ok
101 Correct 1882 ms 32824 KB Ok
102 Correct 1504 ms 31368 KB Ok
103 Correct 1539 ms 32188 KB Ok
104 Correct 1943 ms 34420 KB Ok
105 Execution timed out 2067 ms 34780 KB Time limit exceeded
106 Halted 0 ms 0 KB -