답안 #1114222

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114222 2024-11-18T11:38:13 Z MisterReaper Nice sequence (IZhO18_sequence) C++17
100 / 100
1099 ms 56376 KB
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

constexpr int INF = int(1E9) - 5;
constexpr int C = 1000;

void print(bool s, std::vector<int> x) {
    int n = int(x.size());
    std::cout << n << '\n';
    for (int i = 0; i < n; ++i) {
        std::cout << (s ? -1 : +1) * x[i] << " \n"[i == n - 1];
    }
}

void solve() {
    int N, M;
    std::cin >> N >> M;

    bool swapped = false;
    if (N < M) {
        swapped = true;
        std::swap(N, M);
    }

    if (N % M == 0) {
        std::vector<int> p(N - 1);
        for (int i = 0; i < N - 1; ++i) {
            p[i] = 1;
        }
        print(swapped, p);
    } else {
        std::vector<std::vector<int>> adj;
        std::vector<int> pre, ans, ein;
        auto check = [&](int n, bool save) -> std::pair<bool, std::vector<int>> {
            adj.assign(n + 1, {});
            pre.assign(n + 1, 0);
            ans.assign(n, 0);
            ein.assign(n + 1, 0);
            for (int i = 0; i <= n; ++i) {
                if (i - N >= 0) {
                    adj[i].emplace_back(i - N);
                    ++ein[i - N];
                } 
                if (i + M <= n) {
                    adj[i].emplace_back(i + M);
                    ++ein[i + M];
                }
            }

            int cur = 0;
            std::queue<int> que;
            for (int i = 0; i <= n; ++i) {
                if (ein[i] == 0) {
                    pre[i] = (++cur) * C;
                    que.emplace(i);
                }
            }
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto u : adj[v]) {
                    --ein[u];
                    pre[u] = std::max(pre[u], pre[v] + 1);
                    if (ein[u] == 0) {
                        que.emplace(u);
                    }
                }
            }
            for (int i = n; i >= 0; --i) {
                if (pre[i] == 0) {
                    return {false, {}};
                }
                pre[i] -= pre[0];
            }
            if (!save) {
                return {true, {}};
            }

            for (int i = 0; i < n; ++i) {
                ans[i] = pre[i + 1] - pre[i];
            }
            return {true, ans};
        };

        // int lo = 0, hi = N + M;
        // while (lo < hi) {
        //     int mid = (lo + hi + 1) >> 1;
        //     if (check(mid, false).first) {
        //         lo = mid;
        //     } else {
        //         hi = mid - 1;
        //     }
        // }
        int lo = N + M - std::gcd(N, M) - 1;
        print(swapped, check(lo, true).second);
    }
    
    return;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int TT = 1; std::cin >> TT;
    for (int i = 1; i <= TT; ++i) {
        solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 348 KB Ok
3 Correct 1 ms 504 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 1 ms 508 KB Ok
7 Correct 1 ms 336 KB Ok
8 Correct 1 ms 336 KB Ok
9 Correct 1 ms 336 KB Ok
10 Correct 1 ms 336 KB Ok
11 Correct 1 ms 336 KB Ok
12 Correct 1 ms 336 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 336 KB Ok
3 Correct 1 ms 336 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 2 ms 592 KB Ok
7 Correct 7 ms 1160 KB Ok
8 Correct 4 ms 848 KB Ok
9 Correct 8 ms 1552 KB Ok
10 Correct 5 ms 968 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 336 KB Ok
3 Correct 1 ms 336 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 1 ms 336 KB Ok
7 Correct 1 ms 336 KB Ok
8 Correct 1 ms 336 KB Ok
9 Correct 1 ms 336 KB Ok
10 Correct 1 ms 336 KB Ok
11 Correct 1 ms 336 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 336 KB Ok
3 Correct 1 ms 336 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 82 ms 14820 KB Ok
7 Correct 68 ms 15136 KB Ok
8 Correct 158 ms 19656 KB Ok
9 Correct 119 ms 17512 KB Ok
10 Correct 85 ms 10564 KB Ok
11 Correct 109 ms 18204 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 348 KB Ok
3 Correct 1 ms 504 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 1 ms 508 KB Ok
7 Correct 1 ms 336 KB Ok
8 Correct 1 ms 336 KB Ok
9 Correct 1 ms 336 KB Ok
10 Correct 1 ms 336 KB Ok
11 Correct 1 ms 336 KB Ok
12 Correct 1 ms 336 KB Ok
13 Correct 1 ms 336 KB Ok
14 Correct 1 ms 336 KB Ok
15 Correct 1 ms 336 KB Ok
16 Correct 1 ms 336 KB Ok
17 Correct 1 ms 336 KB Ok
18 Correct 1 ms 336 KB Ok
19 Correct 1 ms 336 KB Ok
20 Correct 1 ms 336 KB Ok
21 Correct 1 ms 336 KB Ok
22 Correct 1 ms 336 KB Ok
23 Correct 1 ms 336 KB Ok
24 Correct 2 ms 760 KB Ok
25 Correct 4 ms 592 KB Ok
26 Correct 2 ms 592 KB Ok
27 Correct 2 ms 592 KB Ok
28 Correct 2 ms 592 KB Ok
29 Correct 2 ms 592 KB Ok
30 Correct 2 ms 592 KB Ok
31 Correct 3 ms 592 KB Ok
32 Correct 3 ms 592 KB Ok
33 Correct 2 ms 592 KB Ok
34 Correct 5 ms 848 KB Ok
35 Correct 5 ms 1016 KB Ok
36 Correct 5 ms 848 KB Ok
37 Correct 5 ms 848 KB Ok
38 Correct 4 ms 848 KB Ok
39 Correct 4 ms 848 KB Ok
40 Correct 4 ms 848 KB Ok
41 Correct 5 ms 848 KB Ok
42 Correct 5 ms 848 KB Ok
43 Correct 4 ms 848 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 348 KB Ok
3 Correct 1 ms 504 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 1 ms 508 KB Ok
7 Correct 1 ms 336 KB Ok
8 Correct 1 ms 336 KB Ok
9 Correct 1 ms 336 KB Ok
10 Correct 1 ms 336 KB Ok
11 Correct 1 ms 336 KB Ok
12 Correct 1 ms 336 KB Ok
13 Correct 1 ms 336 KB Ok
14 Correct 1 ms 336 KB Ok
15 Correct 1 ms 336 KB Ok
16 Correct 1 ms 336 KB Ok
17 Correct 1 ms 336 KB Ok
18 Correct 2 ms 592 KB Ok
19 Correct 7 ms 1160 KB Ok
20 Correct 4 ms 848 KB Ok
21 Correct 8 ms 1552 KB Ok
22 Correct 5 ms 968 KB Ok
23 Correct 1 ms 336 KB Ok
24 Correct 1 ms 336 KB Ok
25 Correct 1 ms 336 KB Ok
26 Correct 1 ms 336 KB Ok
27 Correct 1 ms 336 KB Ok
28 Correct 1 ms 336 KB Ok
29 Correct 1 ms 336 KB Ok
30 Correct 1 ms 336 KB Ok
31 Correct 1 ms 336 KB Ok
32 Correct 1 ms 336 KB Ok
33 Correct 1 ms 336 KB Ok
34 Correct 2 ms 760 KB Ok
35 Correct 4 ms 592 KB Ok
36 Correct 2 ms 592 KB Ok
37 Correct 2 ms 592 KB Ok
38 Correct 2 ms 592 KB Ok
39 Correct 2 ms 592 KB Ok
40 Correct 2 ms 592 KB Ok
41 Correct 3 ms 592 KB Ok
42 Correct 3 ms 592 KB Ok
43 Correct 2 ms 592 KB Ok
44 Correct 5 ms 848 KB Ok
45 Correct 5 ms 1016 KB Ok
46 Correct 5 ms 848 KB Ok
47 Correct 5 ms 848 KB Ok
48 Correct 4 ms 848 KB Ok
49 Correct 4 ms 848 KB Ok
50 Correct 4 ms 848 KB Ok
51 Correct 5 ms 848 KB Ok
52 Correct 5 ms 848 KB Ok
53 Correct 4 ms 848 KB Ok
54 Correct 95 ms 10140 KB Ok
55 Correct 86 ms 12540 KB Ok
56 Correct 74 ms 10496 KB Ok
57 Correct 53 ms 8952 KB Ok
58 Correct 69 ms 10740 KB Ok
59 Correct 61 ms 9712 KB Ok
60 Correct 66 ms 8844 KB Ok
61 Correct 63 ms 9648 KB Ok
62 Correct 79 ms 11332 KB Ok
63 Correct 57 ms 9276 KB Ok
64 Correct 92 ms 10220 KB Ok
65 Correct 71 ms 10492 KB Ok
66 Correct 79 ms 10016 KB Ok
67 Correct 50 ms 8848 KB Ok
68 Correct 64 ms 11152 KB Ok
69 Correct 128 ms 13496 KB Ok
70 Correct 135 ms 13832 KB Ok
71 Correct 118 ms 13380 KB Ok
72 Correct 129 ms 13312 KB Ok
73 Correct 119 ms 13568 KB Ok
74 Correct 110 ms 13320 KB Ok
75 Correct 130 ms 12932 KB Ok
76 Correct 132 ms 13572 KB Ok
77 Correct 109 ms 13340 KB Ok
78 Correct 126 ms 13404 KB Ok
79 Correct 122 ms 13604 KB Ok
80 Correct 123 ms 13376 KB Ok
81 Correct 140 ms 13600 KB Ok
82 Correct 129 ms 13556 KB Ok
83 Correct 133 ms 13476 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Ok
2 Correct 1 ms 348 KB Ok
3 Correct 1 ms 504 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 336 KB Ok
6 Correct 1 ms 508 KB Ok
7 Correct 1 ms 336 KB Ok
8 Correct 1 ms 336 KB Ok
9 Correct 1 ms 336 KB Ok
10 Correct 1 ms 336 KB Ok
11 Correct 1 ms 336 KB Ok
12 Correct 1 ms 336 KB Ok
13 Correct 1 ms 336 KB Ok
14 Correct 1 ms 336 KB Ok
15 Correct 1 ms 336 KB Ok
16 Correct 1 ms 336 KB Ok
17 Correct 1 ms 336 KB Ok
18 Correct 2 ms 592 KB Ok
19 Correct 7 ms 1160 KB Ok
20 Correct 4 ms 848 KB Ok
21 Correct 8 ms 1552 KB Ok
22 Correct 5 ms 968 KB Ok
23 Correct 1 ms 336 KB Ok
24 Correct 1 ms 336 KB Ok
25 Correct 1 ms 336 KB Ok
26 Correct 1 ms 336 KB Ok
27 Correct 1 ms 336 KB Ok
28 Correct 1 ms 336 KB Ok
29 Correct 1 ms 336 KB Ok
30 Correct 1 ms 336 KB Ok
31 Correct 1 ms 336 KB Ok
32 Correct 1 ms 336 KB Ok
33 Correct 1 ms 336 KB Ok
34 Correct 1 ms 336 KB Ok
35 Correct 1 ms 336 KB Ok
36 Correct 1 ms 336 KB Ok
37 Correct 1 ms 336 KB Ok
38 Correct 1 ms 336 KB Ok
39 Correct 82 ms 14820 KB Ok
40 Correct 68 ms 15136 KB Ok
41 Correct 158 ms 19656 KB Ok
42 Correct 119 ms 17512 KB Ok
43 Correct 85 ms 10564 KB Ok
44 Correct 109 ms 18204 KB Ok
45 Correct 2 ms 760 KB Ok
46 Correct 4 ms 592 KB Ok
47 Correct 2 ms 592 KB Ok
48 Correct 2 ms 592 KB Ok
49 Correct 2 ms 592 KB Ok
50 Correct 2 ms 592 KB Ok
51 Correct 2 ms 592 KB Ok
52 Correct 3 ms 592 KB Ok
53 Correct 3 ms 592 KB Ok
54 Correct 2 ms 592 KB Ok
55 Correct 5 ms 848 KB Ok
56 Correct 5 ms 1016 KB Ok
57 Correct 5 ms 848 KB Ok
58 Correct 5 ms 848 KB Ok
59 Correct 4 ms 848 KB Ok
60 Correct 4 ms 848 KB Ok
61 Correct 4 ms 848 KB Ok
62 Correct 5 ms 848 KB Ok
63 Correct 5 ms 848 KB Ok
64 Correct 4 ms 848 KB Ok
65 Correct 95 ms 10140 KB Ok
66 Correct 86 ms 12540 KB Ok
67 Correct 74 ms 10496 KB Ok
68 Correct 53 ms 8952 KB Ok
69 Correct 69 ms 10740 KB Ok
70 Correct 61 ms 9712 KB Ok
71 Correct 66 ms 8844 KB Ok
72 Correct 63 ms 9648 KB Ok
73 Correct 79 ms 11332 KB Ok
74 Correct 57 ms 9276 KB Ok
75 Correct 92 ms 10220 KB Ok
76 Correct 71 ms 10492 KB Ok
77 Correct 79 ms 10016 KB Ok
78 Correct 50 ms 8848 KB Ok
79 Correct 64 ms 11152 KB Ok
80 Correct 128 ms 13496 KB Ok
81 Correct 135 ms 13832 KB Ok
82 Correct 118 ms 13380 KB Ok
83 Correct 129 ms 13312 KB Ok
84 Correct 119 ms 13568 KB Ok
85 Correct 110 ms 13320 KB Ok
86 Correct 130 ms 12932 KB Ok
87 Correct 132 ms 13572 KB Ok
88 Correct 109 ms 13340 KB Ok
89 Correct 126 ms 13404 KB Ok
90 Correct 122 ms 13604 KB Ok
91 Correct 123 ms 13376 KB Ok
92 Correct 140 ms 13600 KB Ok
93 Correct 129 ms 13556 KB Ok
94 Correct 133 ms 13476 KB Ok
95 Correct 132 ms 23040 KB Ok
96 Correct 257 ms 36352 KB Ok
97 Correct 246 ms 32008 KB Ok
98 Correct 154 ms 24996 KB Ok
99 Correct 204 ms 29016 KB Ok
100 Correct 226 ms 30460 KB Ok
101 Correct 228 ms 31208 KB Ok
102 Correct 192 ms 30692 KB Ok
103 Correct 210 ms 32192 KB Ok
104 Correct 251 ms 36020 KB Ok
105 Correct 247 ms 37304 KB Ok
106 Correct 176 ms 30848 KB Ok
107 Correct 227 ms 35572 KB Ok
108 Correct 249 ms 38272 KB Ok
109 Correct 221 ms 34380 KB Ok
110 Correct 791 ms 52248 KB Ok
111 Correct 1031 ms 56152 KB Ok
112 Correct 855 ms 54072 KB Ok
113 Correct 1038 ms 55196 KB Ok
114 Correct 1099 ms 56008 KB Ok
115 Correct 923 ms 54844 KB Ok
116 Correct 867 ms 55332 KB Ok
117 Correct 816 ms 55364 KB Ok
118 Correct 739 ms 54848 KB Ok
119 Correct 876 ms 55192 KB Ok
120 Correct 780 ms 54240 KB Ok
121 Correct 814 ms 53164 KB Ok
122 Correct 857 ms 54652 KB Ok
123 Correct 885 ms 56376 KB Ok
124 Correct 608 ms 52100 KB Ok
125 Correct 401 ms 38680 KB Ok