답안 #682671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682671 2023-01-16T17:49:01 Z Alcabel Nice sequence (IZhO18_sequence) C++17
100 / 100
1499 ms 52980 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
char vis[maxn + 1];
int n, m;
long long pref[maxn + 1];

bool dfsCycle(int v, int maxv) {
    // cerr << "v: " << v << '\n';
    vis[v] = 1;
    bool hasCycle = false;
    if (v >= n) {
        if (vis[v - n] == 1) {
            hasCycle = true;
        } else if (vis[v - n] == 0) {
            hasCycle = hasCycle || dfsCycle(v - n, maxv);
        }
    }
    if (v + m <= maxv) {
        if (vis[v + m] == 1) {
            hasCycle = true;
        } else if (vis[v + m] == 0) {
            hasCycle = hasCycle || dfsCycle(v + m, maxv);
        }
    }
    vis[v] = 2;
    return hasCycle;
}
vector<int> top;

void dfsTop(int v, int maxv) {
    vis[v] = true;
    if (v >= n && vis[v - n] == 0) {
        dfsTop(v - n, maxv);
    }
    if (v + m <= maxv && vis[v + m] == 0) {
        dfsTop(v + m, maxv);
    }
    top.emplace_back(v);
}

void solve() {
    cin >> n >> m;
    int left = 0, right = maxn + 1;
    while (right - left > 1) {
        int mid = left + (right - left) / 2;
        for (int i = 0; i <= mid; ++i) {
            vis[i] = 0;
        }
        bool able = true;
        // cerr << "mid: " << mid << '\n';
        for (int i = 0; i <= mid; ++i) {
            if (vis[i] == 0) {
                if (dfsCycle(i, mid)) {
                    able = false;
                    break;
                }
            }
        }
        if (able) {
            left = mid;
        } else {
            right = mid;
        }
    }
    if (left != 0) {
        top.clear();
        for (int i = 0; i <= left; ++i) {
            vis[i] = false, pref[i] = 0;
        }
        for (int i = 0; i <= left; ++i) {
            if (!vis[i]) {
                dfsTop(i, left);
            }
        }
        reverse(top.begin(), top.end());
        for (int i = 0; i < (int)top.size(); ++i) {
            pref[top[i]] = i;
        }
        long long change = -pref[0];
        for (int i = 0; i <= left; ++i) {
            pref[i] += change;
        }
        cout << left << '\n';
        for (int i = 1; i <= left; ++i) {
            cout << pref[i] - pref[i - 1] << ' ';
        }
    } else {
        cout << "0\n";
    }
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 1 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 1 ms 724 KB Ok
6 Correct 1 ms 724 KB Ok
7 Correct 1 ms 748 KB Ok
8 Correct 1 ms 724 KB Ok
9 Correct 1 ms 724 KB Ok
10 Correct 1 ms 724 KB Ok
11 Correct 1 ms 724 KB Ok
12 Correct 1 ms 724 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 2 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 2 ms 724 KB Ok
6 Correct 4 ms 980 KB Ok
7 Correct 16 ms 1780 KB Ok
8 Correct 8 ms 1184 KB Ok
9 Correct 19 ms 1892 KB Ok
10 Correct 11 ms 1396 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 1 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 1 ms 724 KB Ok
6 Correct 1 ms 708 KB Ok
7 Correct 1 ms 724 KB Ok
8 Correct 1 ms 724 KB Ok
9 Correct 1 ms 724 KB Ok
10 Correct 1 ms 724 KB Ok
11 Correct 2 ms 724 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 1 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 1 ms 724 KB Ok
6 Correct 207 ms 13488 KB Ok
7 Correct 193 ms 14612 KB Ok
8 Correct 343 ms 16600 KB Ok
9 Correct 254 ms 16452 KB Ok
10 Correct 143 ms 9320 KB Ok
11 Correct 287 ms 17276 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 1 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 1 ms 724 KB Ok
6 Correct 1 ms 724 KB Ok
7 Correct 1 ms 748 KB Ok
8 Correct 1 ms 724 KB Ok
9 Correct 1 ms 724 KB Ok
10 Correct 1 ms 724 KB Ok
11 Correct 1 ms 724 KB Ok
12 Correct 1 ms 724 KB Ok
13 Correct 1 ms 724 KB Ok
14 Correct 1 ms 724 KB Ok
15 Correct 1 ms 724 KB Ok
16 Correct 1 ms 724 KB Ok
17 Correct 1 ms 724 KB Ok
18 Correct 1 ms 708 KB Ok
19 Correct 1 ms 724 KB Ok
20 Correct 1 ms 724 KB Ok
21 Correct 1 ms 724 KB Ok
22 Correct 1 ms 724 KB Ok
23 Correct 2 ms 724 KB Ok
24 Correct 3 ms 852 KB Ok
25 Correct 3 ms 840 KB Ok
26 Correct 3 ms 852 KB Ok
27 Correct 3 ms 852 KB Ok
28 Correct 2 ms 840 KB Ok
29 Correct 2 ms 852 KB Ok
30 Correct 2 ms 852 KB Ok
31 Correct 3 ms 852 KB Ok
32 Correct 3 ms 840 KB Ok
33 Correct 3 ms 852 KB Ok
34 Correct 7 ms 1108 KB Ok
35 Correct 9 ms 1236 KB Ok
36 Correct 8 ms 1236 KB Ok
37 Correct 9 ms 1112 KB Ok
38 Correct 8 ms 1108 KB Ok
39 Correct 7 ms 1108 KB Ok
40 Correct 9 ms 1108 KB Ok
41 Correct 9 ms 1108 KB Ok
42 Correct 7 ms 1108 KB Ok
43 Correct 8 ms 1172 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 1 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 1 ms 724 KB Ok
6 Correct 1 ms 724 KB Ok
7 Correct 1 ms 748 KB Ok
8 Correct 1 ms 724 KB Ok
9 Correct 1 ms 724 KB Ok
10 Correct 1 ms 724 KB Ok
11 Correct 1 ms 724 KB Ok
12 Correct 1 ms 724 KB Ok
13 Correct 1 ms 724 KB Ok
14 Correct 1 ms 724 KB Ok
15 Correct 2 ms 724 KB Ok
16 Correct 1 ms 724 KB Ok
17 Correct 2 ms 724 KB Ok
18 Correct 4 ms 980 KB Ok
19 Correct 16 ms 1780 KB Ok
20 Correct 8 ms 1184 KB Ok
21 Correct 19 ms 1892 KB Ok
22 Correct 11 ms 1396 KB Ok
23 Correct 1 ms 724 KB Ok
24 Correct 1 ms 724 KB Ok
25 Correct 1 ms 724 KB Ok
26 Correct 1 ms 724 KB Ok
27 Correct 1 ms 724 KB Ok
28 Correct 1 ms 708 KB Ok
29 Correct 1 ms 724 KB Ok
30 Correct 1 ms 724 KB Ok
31 Correct 1 ms 724 KB Ok
32 Correct 1 ms 724 KB Ok
33 Correct 2 ms 724 KB Ok
34 Correct 3 ms 852 KB Ok
35 Correct 3 ms 840 KB Ok
36 Correct 3 ms 852 KB Ok
37 Correct 3 ms 852 KB Ok
38 Correct 2 ms 840 KB Ok
39 Correct 2 ms 852 KB Ok
40 Correct 2 ms 852 KB Ok
41 Correct 3 ms 852 KB Ok
42 Correct 3 ms 840 KB Ok
43 Correct 3 ms 852 KB Ok
44 Correct 7 ms 1108 KB Ok
45 Correct 9 ms 1236 KB Ok
46 Correct 8 ms 1236 KB Ok
47 Correct 9 ms 1112 KB Ok
48 Correct 8 ms 1108 KB Ok
49 Correct 7 ms 1108 KB Ok
50 Correct 9 ms 1108 KB Ok
51 Correct 9 ms 1108 KB Ok
52 Correct 7 ms 1108 KB Ok
53 Correct 8 ms 1172 KB Ok
54 Correct 72 ms 4172 KB Ok
55 Correct 83 ms 4556 KB Ok
56 Correct 93 ms 4672 KB Ok
57 Correct 65 ms 3648 KB Ok
58 Correct 82 ms 4436 KB Ok
59 Correct 77 ms 4228 KB Ok
60 Correct 72 ms 3864 KB Ok
61 Correct 68 ms 3844 KB Ok
62 Correct 96 ms 4840 KB Ok
63 Correct 73 ms 4044 KB Ok
64 Correct 92 ms 4500 KB Ok
65 Correct 84 ms 4396 KB Ok
66 Correct 76 ms 4044 KB Ok
67 Correct 62 ms 3776 KB Ok
68 Correct 80 ms 4340 KB Ok
69 Correct 241 ms 12964 KB Ok
70 Correct 303 ms 13300 KB Ok
71 Correct 215 ms 12920 KB Ok
72 Correct 248 ms 12884 KB Ok
73 Correct 252 ms 13112 KB Ok
74 Correct 231 ms 12768 KB Ok
75 Correct 204 ms 12820 KB Ok
76 Correct 256 ms 12968 KB Ok
77 Correct 191 ms 12780 KB Ok
78 Correct 275 ms 12812 KB Ok
79 Correct 271 ms 13248 KB Ok
80 Correct 231 ms 12860 KB Ok
81 Correct 253 ms 13164 KB Ok
82 Correct 262 ms 12908 KB Ok
83 Correct 229 ms 13044 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Ok
2 Correct 1 ms 724 KB Ok
3 Correct 1 ms 724 KB Ok
4 Correct 1 ms 724 KB Ok
5 Correct 1 ms 724 KB Ok
6 Correct 1 ms 724 KB Ok
7 Correct 1 ms 748 KB Ok
8 Correct 1 ms 724 KB Ok
9 Correct 1 ms 724 KB Ok
10 Correct 1 ms 724 KB Ok
11 Correct 1 ms 724 KB Ok
12 Correct 1 ms 724 KB Ok
13 Correct 1 ms 724 KB Ok
14 Correct 1 ms 724 KB Ok
15 Correct 2 ms 724 KB Ok
16 Correct 1 ms 724 KB Ok
17 Correct 2 ms 724 KB Ok
18 Correct 4 ms 980 KB Ok
19 Correct 16 ms 1780 KB Ok
20 Correct 8 ms 1184 KB Ok
21 Correct 19 ms 1892 KB Ok
22 Correct 11 ms 1396 KB Ok
23 Correct 1 ms 724 KB Ok
24 Correct 1 ms 724 KB Ok
25 Correct 1 ms 724 KB Ok
26 Correct 1 ms 724 KB Ok
27 Correct 1 ms 724 KB Ok
28 Correct 1 ms 708 KB Ok
29 Correct 1 ms 724 KB Ok
30 Correct 1 ms 724 KB Ok
31 Correct 1 ms 724 KB Ok
32 Correct 1 ms 724 KB Ok
33 Correct 2 ms 724 KB Ok
34 Correct 1 ms 724 KB Ok
35 Correct 1 ms 724 KB Ok
36 Correct 1 ms 724 KB Ok
37 Correct 1 ms 724 KB Ok
38 Correct 1 ms 724 KB Ok
39 Correct 207 ms 13488 KB Ok
40 Correct 193 ms 14612 KB Ok
41 Correct 343 ms 16600 KB Ok
42 Correct 254 ms 16452 KB Ok
43 Correct 143 ms 9320 KB Ok
44 Correct 287 ms 17276 KB Ok
45 Correct 3 ms 852 KB Ok
46 Correct 3 ms 840 KB Ok
47 Correct 3 ms 852 KB Ok
48 Correct 3 ms 852 KB Ok
49 Correct 2 ms 840 KB Ok
50 Correct 2 ms 852 KB Ok
51 Correct 2 ms 852 KB Ok
52 Correct 3 ms 852 KB Ok
53 Correct 3 ms 840 KB Ok
54 Correct 3 ms 852 KB Ok
55 Correct 7 ms 1108 KB Ok
56 Correct 9 ms 1236 KB Ok
57 Correct 8 ms 1236 KB Ok
58 Correct 9 ms 1112 KB Ok
59 Correct 8 ms 1108 KB Ok
60 Correct 7 ms 1108 KB Ok
61 Correct 9 ms 1108 KB Ok
62 Correct 9 ms 1108 KB Ok
63 Correct 7 ms 1108 KB Ok
64 Correct 8 ms 1172 KB Ok
65 Correct 72 ms 4172 KB Ok
66 Correct 83 ms 4556 KB Ok
67 Correct 93 ms 4672 KB Ok
68 Correct 65 ms 3648 KB Ok
69 Correct 82 ms 4436 KB Ok
70 Correct 77 ms 4228 KB Ok
71 Correct 72 ms 3864 KB Ok
72 Correct 68 ms 3844 KB Ok
73 Correct 96 ms 4840 KB Ok
74 Correct 73 ms 4044 KB Ok
75 Correct 92 ms 4500 KB Ok
76 Correct 84 ms 4396 KB Ok
77 Correct 76 ms 4044 KB Ok
78 Correct 62 ms 3776 KB Ok
79 Correct 80 ms 4340 KB Ok
80 Correct 241 ms 12964 KB Ok
81 Correct 303 ms 13300 KB Ok
82 Correct 215 ms 12920 KB Ok
83 Correct 248 ms 12884 KB Ok
84 Correct 252 ms 13112 KB Ok
85 Correct 231 ms 12768 KB Ok
86 Correct 204 ms 12820 KB Ok
87 Correct 256 ms 12968 KB Ok
88 Correct 191 ms 12780 KB Ok
89 Correct 275 ms 12812 KB Ok
90 Correct 271 ms 13248 KB Ok
91 Correct 231 ms 12860 KB Ok
92 Correct 253 ms 13164 KB Ok
93 Correct 262 ms 12908 KB Ok
94 Correct 229 ms 13044 KB Ok
95 Correct 189 ms 9512 KB Ok
96 Correct 316 ms 13524 KB Ok
97 Correct 279 ms 11972 KB Ok
98 Correct 221 ms 10520 KB Ok
99 Correct 249 ms 10944 KB Ok
100 Correct 272 ms 11780 KB Ok
101 Correct 263 ms 11864 KB Ok
102 Correct 232 ms 11708 KB Ok
103 Correct 235 ms 12224 KB Ok
104 Correct 275 ms 13116 KB Ok
105 Correct 271 ms 13260 KB Ok
106 Correct 238 ms 11968 KB Ok
107 Correct 272 ms 12528 KB Ok
108 Correct 300 ms 13780 KB Ok
109 Correct 266 ms 13076 KB Ok
110 Correct 1022 ms 50320 KB Ok
111 Correct 1421 ms 52180 KB Ok
112 Correct 1008 ms 52468 KB Ok
113 Correct 1178 ms 51004 KB Ok
114 Correct 1186 ms 52852 KB Ok
115 Correct 1344 ms 52420 KB Ok
116 Correct 1215 ms 52980 KB Ok
117 Correct 1332 ms 51428 KB Ok
118 Correct 1025 ms 52916 KB Ok
119 Correct 1499 ms 51048 KB Ok
120 Correct 1109 ms 51764 KB Ok
121 Correct 1064 ms 51144 KB Ok
122 Correct 1179 ms 51752 KB Ok
123 Correct 1372 ms 52248 KB Ok
124 Correct 854 ms 52928 KB Ok
125 Correct 1351 ms 34520 KB Ok