답안 #41662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
41662 2018-02-20T08:51:11 Z cheater2k Nice sequence (IZhO18_sequence) C++14
100 / 100
1843 ms 55828 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 5;

int n, m, sn, sm;
int nelement;
int f[N];
int deg[N];
vector<int> g[N];

bool check(int x) {
    // -m +n
    int add = sn, sub = sm;
    int cur = x;
    while(add || sub) {
        if (sub && cur >= m) --sub, cur -= m;
        else if (add && cur + n <= x) --add, cur += n;
        else return false;
    }
    return true;
}

void solve() {
    cin >> n >> m;
    int gcd = __gcd(n, m);
    sn = m / gcd, sm = n / gcd;

    int low = 0, high = 500000;
    while(low < high) {
        int mid = ((low + high + 1) >> 1);
        if (!check(mid)) low = mid;
        else high = mid - 1;
    }
    
    // build the sequence
    nelement = low;
    for (int i = 0; i <= nelement; ++i) g[i].clear(), deg[i] = 0;

    for (int i = 0; i <= nelement; ++i) {        
        // f[i] < f[i - n]
        if (i >= n) g[i - n].push_back(i), deg[i]++;
        // f[i] > f[i - m]
        if (i >= m) g[i].push_back(i - m), deg[i - m]++;
    }

    queue<int> q;
    for (int i = 0; i <= nelement; ++i) if (!deg[i]) q.push(i);
    int cnt = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        f[u] = cnt++;
        for (int &v : g[u]) {
            if (--deg[v] == 0) q.push(v);
        }
    }

    for (int i = 0; i <= nelement; ++i) f[i] = cnt - 1 - f[i];
    for (int i = 1; i <= nelement; ++i) f[i] -= f[0]; f[0] = 0;

    printf("%d\n", nelement);
    for (int i = 1; i <= nelement; ++i) printf("%d ", f[i] - f[i - 1]);
    printf("\n");
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int tt; cin >> tt;
    while(tt--) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Ok
2 Correct 12 ms 12388 KB Ok
3 Correct 12 ms 12388 KB Ok
4 Correct 12 ms 12388 KB Ok
5 Correct 12 ms 12388 KB Ok
6 Correct 13 ms 12392 KB Ok
7 Correct 12 ms 12396 KB Ok
8 Correct 11 ms 12396 KB Ok
9 Correct 11 ms 12508 KB Ok
10 Correct 12 ms 12508 KB Ok
11 Correct 11 ms 12508 KB Ok
12 Correct 12 ms 12540 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12540 KB Ok
2 Correct 14 ms 12548 KB Ok
3 Correct 12 ms 12548 KB Ok
4 Correct 12 ms 12556 KB Ok
5 Correct 12 ms 12556 KB Ok
6 Correct 15 ms 12564 KB Ok
7 Correct 24 ms 13208 KB Ok
8 Correct 17 ms 13208 KB Ok
9 Correct 38 ms 13376 KB Ok
10 Correct 20 ms 13376 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 13376 KB Ok
2 Correct 11 ms 13376 KB Ok
3 Correct 11 ms 13376 KB Ok
4 Correct 11 ms 13376 KB Ok
5 Correct 11 ms 13376 KB Ok
6 Correct 12 ms 13376 KB Ok
7 Correct 12 ms 13376 KB Ok
8 Correct 11 ms 13376 KB Ok
9 Correct 12 ms 13376 KB Ok
10 Correct 12 ms 13376 KB Ok
11 Correct 12 ms 13376 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 13376 KB Ok
2 Correct 13 ms 13376 KB Ok
3 Correct 12 ms 13376 KB Ok
4 Correct 13 ms 13376 KB Ok
5 Correct 14 ms 13376 KB Ok
6 Correct 139 ms 22100 KB Ok
7 Correct 126 ms 22428 KB Ok
8 Correct 207 ms 24536 KB Ok
9 Correct 174 ms 24536 KB Ok
10 Correct 107 ms 24536 KB Ok
11 Correct 165 ms 24536 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Ok
2 Correct 12 ms 12388 KB Ok
3 Correct 12 ms 12388 KB Ok
4 Correct 12 ms 12388 KB Ok
5 Correct 12 ms 12388 KB Ok
6 Correct 13 ms 12392 KB Ok
7 Correct 12 ms 12396 KB Ok
8 Correct 11 ms 12396 KB Ok
9 Correct 11 ms 12508 KB Ok
10 Correct 12 ms 12508 KB Ok
11 Correct 11 ms 12508 KB Ok
12 Correct 12 ms 12540 KB Ok
13 Correct 12 ms 13376 KB Ok
14 Correct 11 ms 13376 KB Ok
15 Correct 11 ms 13376 KB Ok
16 Correct 11 ms 13376 KB Ok
17 Correct 11 ms 13376 KB Ok
18 Correct 12 ms 13376 KB Ok
19 Correct 12 ms 13376 KB Ok
20 Correct 11 ms 13376 KB Ok
21 Correct 12 ms 13376 KB Ok
22 Correct 12 ms 13376 KB Ok
23 Correct 12 ms 13376 KB Ok
24 Correct 14 ms 24536 KB Ok
25 Correct 14 ms 24536 KB Ok
26 Correct 14 ms 24536 KB Ok
27 Correct 14 ms 24536 KB Ok
28 Correct 13 ms 24536 KB Ok
29 Correct 15 ms 24536 KB Ok
30 Correct 15 ms 24536 KB Ok
31 Correct 14 ms 24536 KB Ok
32 Correct 14 ms 24536 KB Ok
33 Correct 16 ms 24536 KB Ok
34 Correct 19 ms 24536 KB Ok
35 Correct 18 ms 24536 KB Ok
36 Correct 24 ms 24536 KB Ok
37 Correct 17 ms 24536 KB Ok
38 Correct 16 ms 24536 KB Ok
39 Correct 16 ms 24536 KB Ok
40 Correct 21 ms 24536 KB Ok
41 Correct 16 ms 24536 KB Ok
42 Correct 17 ms 24536 KB Ok
43 Correct 18 ms 24536 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Ok
2 Correct 12 ms 12388 KB Ok
3 Correct 12 ms 12388 KB Ok
4 Correct 12 ms 12388 KB Ok
5 Correct 12 ms 12388 KB Ok
6 Correct 13 ms 12392 KB Ok
7 Correct 12 ms 12396 KB Ok
8 Correct 11 ms 12396 KB Ok
9 Correct 11 ms 12508 KB Ok
10 Correct 12 ms 12508 KB Ok
11 Correct 11 ms 12508 KB Ok
12 Correct 12 ms 12540 KB Ok
13 Correct 12 ms 12540 KB Ok
14 Correct 14 ms 12548 KB Ok
15 Correct 12 ms 12548 KB Ok
16 Correct 12 ms 12556 KB Ok
17 Correct 12 ms 12556 KB Ok
18 Correct 15 ms 12564 KB Ok
19 Correct 24 ms 13208 KB Ok
20 Correct 17 ms 13208 KB Ok
21 Correct 38 ms 13376 KB Ok
22 Correct 20 ms 13376 KB Ok
23 Correct 12 ms 13376 KB Ok
24 Correct 11 ms 13376 KB Ok
25 Correct 11 ms 13376 KB Ok
26 Correct 11 ms 13376 KB Ok
27 Correct 11 ms 13376 KB Ok
28 Correct 12 ms 13376 KB Ok
29 Correct 12 ms 13376 KB Ok
30 Correct 11 ms 13376 KB Ok
31 Correct 12 ms 13376 KB Ok
32 Correct 12 ms 13376 KB Ok
33 Correct 12 ms 13376 KB Ok
34 Correct 14 ms 24536 KB Ok
35 Correct 14 ms 24536 KB Ok
36 Correct 14 ms 24536 KB Ok
37 Correct 14 ms 24536 KB Ok
38 Correct 13 ms 24536 KB Ok
39 Correct 15 ms 24536 KB Ok
40 Correct 15 ms 24536 KB Ok
41 Correct 14 ms 24536 KB Ok
42 Correct 14 ms 24536 KB Ok
43 Correct 16 ms 24536 KB Ok
44 Correct 19 ms 24536 KB Ok
45 Correct 18 ms 24536 KB Ok
46 Correct 24 ms 24536 KB Ok
47 Correct 17 ms 24536 KB Ok
48 Correct 16 ms 24536 KB Ok
49 Correct 16 ms 24536 KB Ok
50 Correct 21 ms 24536 KB Ok
51 Correct 16 ms 24536 KB Ok
52 Correct 17 ms 24536 KB Ok
53 Correct 18 ms 24536 KB Ok
54 Correct 91 ms 24536 KB Ok
55 Correct 97 ms 24536 KB Ok
56 Correct 105 ms 24536 KB Ok
57 Correct 82 ms 24536 KB Ok
58 Correct 106 ms 24536 KB Ok
59 Correct 88 ms 24536 KB Ok
60 Correct 88 ms 24536 KB Ok
61 Correct 88 ms 24536 KB Ok
62 Correct 110 ms 24536 KB Ok
63 Correct 90 ms 24536 KB Ok
64 Correct 113 ms 24536 KB Ok
65 Correct 109 ms 24536 KB Ok
66 Correct 96 ms 24536 KB Ok
67 Correct 94 ms 24536 KB Ok
68 Correct 89 ms 24536 KB Ok
69 Correct 226 ms 24536 KB Ok
70 Correct 200 ms 24536 KB Ok
71 Correct 237 ms 24536 KB Ok
72 Correct 227 ms 24536 KB Ok
73 Correct 168 ms 24536 KB Ok
74 Correct 159 ms 24536 KB Ok
75 Correct 168 ms 24536 KB Ok
76 Correct 202 ms 24536 KB Ok
77 Correct 160 ms 24536 KB Ok
78 Correct 209 ms 24536 KB Ok
79 Correct 216 ms 24536 KB Ok
80 Correct 191 ms 24536 KB Ok
81 Correct 242 ms 24536 KB Ok
82 Correct 195 ms 24536 KB Ok
83 Correct 196 ms 24536 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Ok
2 Correct 12 ms 12388 KB Ok
3 Correct 12 ms 12388 KB Ok
4 Correct 12 ms 12388 KB Ok
5 Correct 12 ms 12388 KB Ok
6 Correct 13 ms 12392 KB Ok
7 Correct 12 ms 12396 KB Ok
8 Correct 11 ms 12396 KB Ok
9 Correct 11 ms 12508 KB Ok
10 Correct 12 ms 12508 KB Ok
11 Correct 11 ms 12508 KB Ok
12 Correct 12 ms 12540 KB Ok
13 Correct 12 ms 12540 KB Ok
14 Correct 14 ms 12548 KB Ok
15 Correct 12 ms 12548 KB Ok
16 Correct 12 ms 12556 KB Ok
17 Correct 12 ms 12556 KB Ok
18 Correct 15 ms 12564 KB Ok
19 Correct 24 ms 13208 KB Ok
20 Correct 17 ms 13208 KB Ok
21 Correct 38 ms 13376 KB Ok
22 Correct 20 ms 13376 KB Ok
23 Correct 12 ms 13376 KB Ok
24 Correct 11 ms 13376 KB Ok
25 Correct 11 ms 13376 KB Ok
26 Correct 11 ms 13376 KB Ok
27 Correct 11 ms 13376 KB Ok
28 Correct 12 ms 13376 KB Ok
29 Correct 12 ms 13376 KB Ok
30 Correct 11 ms 13376 KB Ok
31 Correct 12 ms 13376 KB Ok
32 Correct 12 ms 13376 KB Ok
33 Correct 12 ms 13376 KB Ok
34 Correct 11 ms 13376 KB Ok
35 Correct 13 ms 13376 KB Ok
36 Correct 12 ms 13376 KB Ok
37 Correct 13 ms 13376 KB Ok
38 Correct 14 ms 13376 KB Ok
39 Correct 139 ms 22100 KB Ok
40 Correct 126 ms 22428 KB Ok
41 Correct 207 ms 24536 KB Ok
42 Correct 174 ms 24536 KB Ok
43 Correct 107 ms 24536 KB Ok
44 Correct 165 ms 24536 KB Ok
45 Correct 14 ms 24536 KB Ok
46 Correct 14 ms 24536 KB Ok
47 Correct 14 ms 24536 KB Ok
48 Correct 14 ms 24536 KB Ok
49 Correct 13 ms 24536 KB Ok
50 Correct 15 ms 24536 KB Ok
51 Correct 15 ms 24536 KB Ok
52 Correct 14 ms 24536 KB Ok
53 Correct 14 ms 24536 KB Ok
54 Correct 16 ms 24536 KB Ok
55 Correct 19 ms 24536 KB Ok
56 Correct 18 ms 24536 KB Ok
57 Correct 24 ms 24536 KB Ok
58 Correct 17 ms 24536 KB Ok
59 Correct 16 ms 24536 KB Ok
60 Correct 16 ms 24536 KB Ok
61 Correct 21 ms 24536 KB Ok
62 Correct 16 ms 24536 KB Ok
63 Correct 17 ms 24536 KB Ok
64 Correct 18 ms 24536 KB Ok
65 Correct 91 ms 24536 KB Ok
66 Correct 97 ms 24536 KB Ok
67 Correct 105 ms 24536 KB Ok
68 Correct 82 ms 24536 KB Ok
69 Correct 106 ms 24536 KB Ok
70 Correct 88 ms 24536 KB Ok
71 Correct 88 ms 24536 KB Ok
72 Correct 88 ms 24536 KB Ok
73 Correct 110 ms 24536 KB Ok
74 Correct 90 ms 24536 KB Ok
75 Correct 113 ms 24536 KB Ok
76 Correct 109 ms 24536 KB Ok
77 Correct 96 ms 24536 KB Ok
78 Correct 94 ms 24536 KB Ok
79 Correct 89 ms 24536 KB Ok
80 Correct 226 ms 24536 KB Ok
81 Correct 200 ms 24536 KB Ok
82 Correct 237 ms 24536 KB Ok
83 Correct 227 ms 24536 KB Ok
84 Correct 168 ms 24536 KB Ok
85 Correct 159 ms 24536 KB Ok
86 Correct 168 ms 24536 KB Ok
87 Correct 202 ms 24536 KB Ok
88 Correct 160 ms 24536 KB Ok
89 Correct 209 ms 24536 KB Ok
90 Correct 216 ms 24536 KB Ok
91 Correct 191 ms 24536 KB Ok
92 Correct 242 ms 24536 KB Ok
93 Correct 195 ms 24536 KB Ok
94 Correct 196 ms 24536 KB Ok
95 Correct 234 ms 27664 KB Ok
96 Correct 297 ms 34848 KB Ok
97 Correct 331 ms 34848 KB Ok
98 Correct 245 ms 34848 KB Ok
99 Correct 273 ms 34848 KB Ok
100 Correct 263 ms 34848 KB Ok
101 Correct 275 ms 34848 KB Ok
102 Correct 271 ms 34848 KB Ok
103 Correct 287 ms 34848 KB Ok
104 Correct 317 ms 34848 KB Ok
105 Correct 308 ms 34848 KB Ok
106 Correct 256 ms 34848 KB Ok
107 Correct 280 ms 34848 KB Ok
108 Correct 363 ms 34848 KB Ok
109 Correct 323 ms 34848 KB Ok
110 Correct 999 ms 48080 KB Ok
111 Correct 1635 ms 55824 KB Ok
112 Correct 1341 ms 55824 KB Ok
113 Correct 1379 ms 55824 KB Ok
114 Correct 1244 ms 55824 KB Ok
115 Correct 1592 ms 55824 KB Ok
116 Correct 1347 ms 55824 KB Ok
117 Correct 1330 ms 55824 KB Ok
118 Correct 1360 ms 55824 KB Ok
119 Correct 1711 ms 55824 KB Ok
120 Correct 1363 ms 55824 KB Ok
121 Correct 1367 ms 55824 KB Ok
122 Correct 1484 ms 55824 KB Ok
123 Correct 1843 ms 55828 KB Ok
124 Correct 1062 ms 55828 KB Ok
125 Correct 596 ms 55828 KB Ok