답안 #1035744

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1035744 2024-07-26T14:26:34 Z phoenix Nice sequence (IZhO18_sequence) C++17
100 / 100
772 ms 72892 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 500500;

int n, m;

vector<int> g[N];
int color[N];
bool cycle;

void dfs(int s) {
    color[s] = 1;
    for (int to : g[s]) {
        if (!color[to]) dfs(to);
        cycle |= (color[to] == 1);
    }
    color[s] = 2;
}

void build(int len) {
    for (int i = 0; i <= len; i++) {
        g[i].clear();
        color[i] = 0;
    }
    for (int i = 0; i <= len - n; i++) {
        g[i].push_back(i + n);
    }
    for (int i = 0; i <= len - m; i++) {
        g[i + m].push_back(i);
    }
}

vector<int> top;
void topsort(int s) {
    color[s] = 1;
    for (int to : g[s]) if (!color[to]) {
        topsort(to);
    }
    top.push_back(s);
}

bool check(int len) {
    build(len);
    cycle = false;
    for (int i = 0; i <= len; i++) {
        if (color[i]) continue;
        dfs(i);
        if (cycle) return true;
    }
    return false;
}

void solve() {
    cin >> n >> m;
    int l = n + m - gcd(n, m) - 1, r = n + m - gcd(n, m);
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (check(mid))
            r = mid;
        else 
            l = mid;
    }
    int len = l;
    top.clear();
    build(len);
    for (int i = 0; i <= len; i++) if (!color[i])
        topsort(i);
    
    int arr[len + 1] = {};
    for (int i = 0; i <= len; i++) {
        arr[top[i]] = i;
    }
    cout << len << '\n';
    for (int i = 1; i <= len; i++)
        cout << arr[i] - arr[i - 1] << ' ';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t --> 0) {
        solve();
        cout << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12124 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12200 KB Ok
4 Correct 5 ms 12124 KB Ok
5 Correct 5 ms 12124 KB Ok
6 Correct 5 ms 12124 KB Ok
7 Correct 5 ms 12124 KB Ok
8 Correct 5 ms 12040 KB Ok
9 Correct 5 ms 12120 KB Ok
10 Correct 5 ms 12012 KB Ok
11 Correct 5 ms 11996 KB Ok
12 Correct 6 ms 12028 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12124 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12124 KB Ok
4 Correct 5 ms 12024 KB Ok
5 Correct 5 ms 12124 KB Ok
6 Correct 6 ms 12360 KB Ok
7 Correct 13 ms 13148 KB Ok
8 Correct 7 ms 12636 KB Ok
9 Correct 11 ms 13272 KB Ok
10 Correct 8 ms 12892 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12120 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12124 KB Ok
4 Correct 5 ms 12124 KB Ok
5 Correct 5 ms 12112 KB Ok
6 Correct 5 ms 12124 KB Ok
7 Correct 5 ms 12220 KB Ok
8 Correct 5 ms 12176 KB Ok
9 Correct 5 ms 12124 KB Ok
10 Correct 5 ms 12124 KB Ok
11 Correct 5 ms 12120 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12124 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12124 KB Ok
4 Correct 5 ms 12120 KB Ok
5 Correct 5 ms 12016 KB Ok
6 Correct 54 ms 24992 KB Ok
7 Correct 55 ms 31176 KB Ok
8 Correct 89 ms 34040 KB Ok
9 Correct 76 ms 31172 KB Ok
10 Correct 40 ms 21964 KB Ok
11 Correct 66 ms 27736 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12124 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12200 KB Ok
4 Correct 5 ms 12124 KB Ok
5 Correct 5 ms 12124 KB Ok
6 Correct 5 ms 12124 KB Ok
7 Correct 5 ms 12124 KB Ok
8 Correct 5 ms 12040 KB Ok
9 Correct 5 ms 12120 KB Ok
10 Correct 5 ms 12012 KB Ok
11 Correct 5 ms 11996 KB Ok
12 Correct 6 ms 12028 KB Ok
13 Correct 5 ms 12120 KB Ok
14 Correct 5 ms 12124 KB Ok
15 Correct 5 ms 12124 KB Ok
16 Correct 5 ms 12124 KB Ok
17 Correct 5 ms 12112 KB Ok
18 Correct 5 ms 12124 KB Ok
19 Correct 5 ms 12220 KB Ok
20 Correct 5 ms 12176 KB Ok
21 Correct 5 ms 12124 KB Ok
22 Correct 5 ms 12124 KB Ok
23 Correct 5 ms 12120 KB Ok
24 Correct 6 ms 12124 KB Ok
25 Correct 6 ms 12380 KB Ok
26 Correct 7 ms 12352 KB Ok
27 Correct 6 ms 12380 KB Ok
28 Correct 6 ms 12124 KB Ok
29 Correct 6 ms 12124 KB Ok
30 Correct 6 ms 12168 KB Ok
31 Correct 6 ms 12380 KB Ok
32 Correct 6 ms 12228 KB Ok
33 Correct 6 ms 12380 KB Ok
34 Correct 7 ms 12380 KB Ok
35 Correct 8 ms 12636 KB Ok
36 Correct 8 ms 12468 KB Ok
37 Correct 7 ms 12636 KB Ok
38 Correct 7 ms 12636 KB Ok
39 Correct 7 ms 12380 KB Ok
40 Correct 7 ms 12596 KB Ok
41 Correct 7 ms 12636 KB Ok
42 Correct 7 ms 12568 KB Ok
43 Correct 7 ms 12636 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12124 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12200 KB Ok
4 Correct 5 ms 12124 KB Ok
5 Correct 5 ms 12124 KB Ok
6 Correct 5 ms 12124 KB Ok
7 Correct 5 ms 12124 KB Ok
8 Correct 5 ms 12040 KB Ok
9 Correct 5 ms 12120 KB Ok
10 Correct 5 ms 12012 KB Ok
11 Correct 5 ms 11996 KB Ok
12 Correct 6 ms 12028 KB Ok
13 Correct 5 ms 12124 KB Ok
14 Correct 5 ms 12124 KB Ok
15 Correct 5 ms 12124 KB Ok
16 Correct 5 ms 12024 KB Ok
17 Correct 5 ms 12124 KB Ok
18 Correct 6 ms 12360 KB Ok
19 Correct 13 ms 13148 KB Ok
20 Correct 7 ms 12636 KB Ok
21 Correct 11 ms 13272 KB Ok
22 Correct 8 ms 12892 KB Ok
23 Correct 5 ms 12120 KB Ok
24 Correct 5 ms 12124 KB Ok
25 Correct 5 ms 12124 KB Ok
26 Correct 5 ms 12124 KB Ok
27 Correct 5 ms 12112 KB Ok
28 Correct 5 ms 12124 KB Ok
29 Correct 5 ms 12220 KB Ok
30 Correct 5 ms 12176 KB Ok
31 Correct 5 ms 12124 KB Ok
32 Correct 5 ms 12124 KB Ok
33 Correct 5 ms 12120 KB Ok
34 Correct 6 ms 12124 KB Ok
35 Correct 6 ms 12380 KB Ok
36 Correct 7 ms 12352 KB Ok
37 Correct 6 ms 12380 KB Ok
38 Correct 6 ms 12124 KB Ok
39 Correct 6 ms 12124 KB Ok
40 Correct 6 ms 12168 KB Ok
41 Correct 6 ms 12380 KB Ok
42 Correct 6 ms 12228 KB Ok
43 Correct 6 ms 12380 KB Ok
44 Correct 7 ms 12380 KB Ok
45 Correct 8 ms 12636 KB Ok
46 Correct 8 ms 12468 KB Ok
47 Correct 7 ms 12636 KB Ok
48 Correct 7 ms 12636 KB Ok
49 Correct 7 ms 12380 KB Ok
50 Correct 7 ms 12596 KB Ok
51 Correct 7 ms 12636 KB Ok
52 Correct 7 ms 12568 KB Ok
53 Correct 7 ms 12636 KB Ok
54 Correct 44 ms 17608 KB Ok
55 Correct 49 ms 18128 KB Ok
56 Correct 49 ms 18132 KB Ok
57 Correct 34 ms 17104 KB Ok
58 Correct 43 ms 17360 KB Ok
59 Correct 40 ms 17136 KB Ok
60 Correct 36 ms 16844 KB Ok
61 Correct 38 ms 17364 KB Ok
62 Correct 47 ms 17860 KB Ok
63 Correct 40 ms 17116 KB Ok
64 Correct 51 ms 18092 KB Ok
65 Correct 44 ms 17624 KB Ok
66 Correct 41 ms 17360 KB Ok
67 Correct 37 ms 17360 KB Ok
68 Correct 47 ms 17680 KB Ok
69 Correct 102 ms 26668 KB Ok
70 Correct 108 ms 27336 KB Ok
71 Correct 115 ms 26684 KB Ok
72 Correct 96 ms 26572 KB Ok
73 Correct 97 ms 26056 KB Ok
74 Correct 110 ms 25740 KB Ok
75 Correct 100 ms 25800 KB Ok
76 Correct 107 ms 26784 KB Ok
77 Correct 104 ms 25296 KB Ok
78 Correct 100 ms 25580 KB Ok
79 Correct 107 ms 26320 KB Ok
80 Correct 103 ms 26536 KB Ok
81 Correct 101 ms 25828 KB Ok
82 Correct 109 ms 26316 KB Ok
83 Correct 97 ms 25804 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12124 KB Ok
2 Correct 5 ms 12124 KB Ok
3 Correct 5 ms 12200 KB Ok
4 Correct 5 ms 12124 KB Ok
5 Correct 5 ms 12124 KB Ok
6 Correct 5 ms 12124 KB Ok
7 Correct 5 ms 12124 KB Ok
8 Correct 5 ms 12040 KB Ok
9 Correct 5 ms 12120 KB Ok
10 Correct 5 ms 12012 KB Ok
11 Correct 5 ms 11996 KB Ok
12 Correct 6 ms 12028 KB Ok
13 Correct 5 ms 12124 KB Ok
14 Correct 5 ms 12124 KB Ok
15 Correct 5 ms 12124 KB Ok
16 Correct 5 ms 12024 KB Ok
17 Correct 5 ms 12124 KB Ok
18 Correct 6 ms 12360 KB Ok
19 Correct 13 ms 13148 KB Ok
20 Correct 7 ms 12636 KB Ok
21 Correct 11 ms 13272 KB Ok
22 Correct 8 ms 12892 KB Ok
23 Correct 5 ms 12120 KB Ok
24 Correct 5 ms 12124 KB Ok
25 Correct 5 ms 12124 KB Ok
26 Correct 5 ms 12124 KB Ok
27 Correct 5 ms 12112 KB Ok
28 Correct 5 ms 12124 KB Ok
29 Correct 5 ms 12220 KB Ok
30 Correct 5 ms 12176 KB Ok
31 Correct 5 ms 12124 KB Ok
32 Correct 5 ms 12124 KB Ok
33 Correct 5 ms 12120 KB Ok
34 Correct 5 ms 12124 KB Ok
35 Correct 5 ms 12124 KB Ok
36 Correct 5 ms 12124 KB Ok
37 Correct 5 ms 12120 KB Ok
38 Correct 5 ms 12016 KB Ok
39 Correct 54 ms 24992 KB Ok
40 Correct 55 ms 31176 KB Ok
41 Correct 89 ms 34040 KB Ok
42 Correct 76 ms 31172 KB Ok
43 Correct 40 ms 21964 KB Ok
44 Correct 66 ms 27736 KB Ok
45 Correct 6 ms 12124 KB Ok
46 Correct 6 ms 12380 KB Ok
47 Correct 7 ms 12352 KB Ok
48 Correct 6 ms 12380 KB Ok
49 Correct 6 ms 12124 KB Ok
50 Correct 6 ms 12124 KB Ok
51 Correct 6 ms 12168 KB Ok
52 Correct 6 ms 12380 KB Ok
53 Correct 6 ms 12228 KB Ok
54 Correct 6 ms 12380 KB Ok
55 Correct 7 ms 12380 KB Ok
56 Correct 8 ms 12636 KB Ok
57 Correct 8 ms 12468 KB Ok
58 Correct 7 ms 12636 KB Ok
59 Correct 7 ms 12636 KB Ok
60 Correct 7 ms 12380 KB Ok
61 Correct 7 ms 12596 KB Ok
62 Correct 7 ms 12636 KB Ok
63 Correct 7 ms 12568 KB Ok
64 Correct 7 ms 12636 KB Ok
65 Correct 44 ms 17608 KB Ok
66 Correct 49 ms 18128 KB Ok
67 Correct 49 ms 18132 KB Ok
68 Correct 34 ms 17104 KB Ok
69 Correct 43 ms 17360 KB Ok
70 Correct 40 ms 17136 KB Ok
71 Correct 36 ms 16844 KB Ok
72 Correct 38 ms 17364 KB Ok
73 Correct 47 ms 17860 KB Ok
74 Correct 40 ms 17116 KB Ok
75 Correct 51 ms 18092 KB Ok
76 Correct 44 ms 17624 KB Ok
77 Correct 41 ms 17360 KB Ok
78 Correct 37 ms 17360 KB Ok
79 Correct 47 ms 17680 KB Ok
80 Correct 102 ms 26668 KB Ok
81 Correct 108 ms 27336 KB Ok
82 Correct 115 ms 26684 KB Ok
83 Correct 96 ms 26572 KB Ok
84 Correct 97 ms 26056 KB Ok
85 Correct 110 ms 25740 KB Ok
86 Correct 100 ms 25800 KB Ok
87 Correct 107 ms 26784 KB Ok
88 Correct 104 ms 25296 KB Ok
89 Correct 100 ms 25580 KB Ok
90 Correct 107 ms 26320 KB Ok
91 Correct 103 ms 26536 KB Ok
92 Correct 101 ms 25828 KB Ok
93 Correct 109 ms 26316 KB Ok
94 Correct 97 ms 25804 KB Ok
95 Correct 105 ms 26576 KB Ok
96 Correct 140 ms 32856 KB Ok
97 Correct 132 ms 29268 KB Ok
98 Correct 104 ms 29380 KB Ok
99 Correct 120 ms 28864 KB Ok
100 Correct 122 ms 28456 KB Ok
101 Correct 126 ms 31432 KB Ok
102 Correct 126 ms 29636 KB Ok
103 Correct 119 ms 30400 KB Ok
104 Correct 142 ms 32192 KB Ok
105 Correct 136 ms 32700 KB Ok
106 Correct 119 ms 32964 KB Ok
107 Correct 125 ms 31684 KB Ok
108 Correct 142 ms 32704 KB Ok
109 Correct 137 ms 33980 KB Ok
110 Correct 481 ms 70080 KB Ok
111 Correct 536 ms 72392 KB Ok
112 Correct 551 ms 68496 KB Ok
113 Correct 528 ms 72892 KB Ok
114 Correct 641 ms 72536 KB Ok
115 Correct 628 ms 69748 KB Ok
116 Correct 591 ms 72416 KB Ok
117 Correct 561 ms 71888 KB Ok
118 Correct 495 ms 72384 KB Ok
119 Correct 510 ms 71108 KB Ok
120 Correct 566 ms 71108 KB Ok
121 Correct 563 ms 71000 KB Ok
122 Correct 674 ms 70848 KB Ok
123 Correct 772 ms 72768 KB Ok
124 Correct 460 ms 66536 KB Ok
125 Correct 351 ms 56608 KB Ok