답안 #211600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211600 2020-03-20T19:10:34 Z VEGAnn Nice sequence (IZhO18_sequence) C++14
100 / 100
1280 ms 46680 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define ft first
#define sd second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pll pair<ll, ll>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 400100;
vector<int> ts;
int n, m, len, mrk[N], pf[N];
bool cyc;

void dfs(int v){
    if (cyc) return;

    mrk[v] = 1;

    if (v >= m){
        if (mrk[v - m] == 0)
            dfs(v - m);
        else if (mrk[v - m] == 1){
            cyc = 1;
            return;
        }
    }

    if (v + n <= len){
        if (mrk[v + n] == 0)
            dfs(v + n);
        else if (mrk[v + n] == 1){
            cyc = 1;
            return;
        }
    }

    mrk[v] = 2;
}

bool ok(){
    fill(mrk, mrk + len + 1, 0);
    cyc = 0;

    for (int i = 0; i <= len && !cyc; i++)
        if (mrk[i] == 0)
            dfs(i);

    return !cyc;
}

void DFS(int v){
    mrk[v] = 1;

    if (v >= m){
        if (mrk[v - m] == 0)
            DFS(v - m);
    }

    if (v + n <= len){
        if (mrk[v + n] == 0)
            DFS(v + n);
    }

    ts.PB(v);
}


int main(){
#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#else
    ios_base::sync_with_stdio(0); cin.tie(0);
#endif // _LOCAL

    int qq; cin >> qq;

    for (; qq; qq--){
        cin >> n >> m;

        int l = 0, r = (n + m) * 2;

        while (l < r){
            int md = (l + r + 1) >> 1;
            len = md;

            if (ok())
                l = md;
            else r = md - 1;
        }

        cout << l << '\n';
        len = l;
        fill(mrk, mrk + len + 1, 0);
        fill(pf, pf + len + 1, -1);
        ts.clear();

        for (int i = 0; i <= len; i++)
            if (!mrk[i])
                DFS(i);

        int it = -len;

        for (int v : ts){
            int mx = it;
            it++;

            if (v >= m)
                mx = max(mx, pf[v - m]);

            if (v + n <= len)
                mx = max(mx, pf[v + n]);

            pf[v] = mx + 1;
        }

        for (int i = 1; i <= len; i++)
            cout << pf[i] - pf[i - 1] << " ";
        cout << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 4 ms 384 KB Ok
4 Correct 4 ms 384 KB Ok
5 Correct 4 ms 384 KB Ok
6 Correct 5 ms 384 KB Ok
7 Correct 5 ms 512 KB Ok
8 Correct 4 ms 384 KB Ok
9 Correct 4 ms 384 KB Ok
10 Correct 4 ms 384 KB Ok
11 Correct 4 ms 384 KB Ok
12 Correct 4 ms 384 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 304 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 4 ms 384 KB Ok
4 Correct 4 ms 384 KB Ok
5 Correct 5 ms 384 KB Ok
6 Correct 7 ms 512 KB Ok
7 Correct 21 ms 1152 KB Ok
8 Correct 12 ms 768 KB Ok
9 Correct 21 ms 1280 KB Ok
10 Correct 15 ms 896 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 4 ms 384 KB Ok
4 Correct 4 ms 384 KB Ok
5 Correct 4 ms 384 KB Ok
6 Correct 4 ms 384 KB Ok
7 Correct 4 ms 384 KB Ok
8 Correct 4 ms 384 KB Ok
9 Correct 4 ms 384 KB Ok
10 Correct 5 ms 384 KB Ok
11 Correct 4 ms 384 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 5 ms 308 KB Ok
4 Correct 5 ms 384 KB Ok
5 Correct 4 ms 384 KB Ok
6 Correct 175 ms 10604 KB Ok
7 Correct 188 ms 11120 KB Ok
8 Correct 360 ms 14196 KB Ok
9 Correct 232 ms 12400 KB Ok
10 Correct 118 ms 6388 KB Ok
11 Correct 198 ms 12528 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 4 ms 384 KB Ok
4 Correct 4 ms 384 KB Ok
5 Correct 4 ms 384 KB Ok
6 Correct 5 ms 384 KB Ok
7 Correct 5 ms 512 KB Ok
8 Correct 4 ms 384 KB Ok
9 Correct 4 ms 384 KB Ok
10 Correct 4 ms 384 KB Ok
11 Correct 4 ms 384 KB Ok
12 Correct 4 ms 384 KB Ok
13 Correct 4 ms 384 KB Ok
14 Correct 4 ms 384 KB Ok
15 Correct 4 ms 384 KB Ok
16 Correct 4 ms 384 KB Ok
17 Correct 4 ms 384 KB Ok
18 Correct 4 ms 384 KB Ok
19 Correct 4 ms 384 KB Ok
20 Correct 4 ms 384 KB Ok
21 Correct 4 ms 384 KB Ok
22 Correct 5 ms 384 KB Ok
23 Correct 4 ms 384 KB Ok
24 Correct 7 ms 512 KB Ok
25 Correct 7 ms 384 KB Ok
26 Correct 7 ms 384 KB Ok
27 Correct 7 ms 384 KB Ok
28 Correct 6 ms 512 KB Ok
29 Correct 6 ms 384 KB Ok
30 Correct 6 ms 512 KB Ok
31 Correct 6 ms 384 KB Ok
32 Correct 7 ms 512 KB Ok
33 Correct 6 ms 384 KB Ok
34 Correct 10 ms 640 KB Ok
35 Correct 11 ms 640 KB Ok
36 Correct 10 ms 640 KB Ok
37 Correct 11 ms 640 KB Ok
38 Correct 10 ms 640 KB Ok
39 Correct 10 ms 640 KB Ok
40 Correct 11 ms 640 KB Ok
41 Correct 11 ms 768 KB Ok
42 Correct 10 ms 640 KB Ok
43 Correct 11 ms 640 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 4 ms 384 KB Ok
4 Correct 4 ms 384 KB Ok
5 Correct 4 ms 384 KB Ok
6 Correct 5 ms 384 KB Ok
7 Correct 5 ms 512 KB Ok
8 Correct 4 ms 384 KB Ok
9 Correct 4 ms 384 KB Ok
10 Correct 4 ms 384 KB Ok
11 Correct 4 ms 384 KB Ok
12 Correct 4 ms 384 KB Ok
13 Correct 5 ms 304 KB Ok
14 Correct 4 ms 384 KB Ok
15 Correct 4 ms 384 KB Ok
16 Correct 4 ms 384 KB Ok
17 Correct 5 ms 384 KB Ok
18 Correct 7 ms 512 KB Ok
19 Correct 21 ms 1152 KB Ok
20 Correct 12 ms 768 KB Ok
21 Correct 21 ms 1280 KB Ok
22 Correct 15 ms 896 KB Ok
23 Correct 4 ms 384 KB Ok
24 Correct 4 ms 384 KB Ok
25 Correct 4 ms 384 KB Ok
26 Correct 4 ms 384 KB Ok
27 Correct 4 ms 384 KB Ok
28 Correct 4 ms 384 KB Ok
29 Correct 4 ms 384 KB Ok
30 Correct 4 ms 384 KB Ok
31 Correct 4 ms 384 KB Ok
32 Correct 5 ms 384 KB Ok
33 Correct 4 ms 384 KB Ok
34 Correct 7 ms 512 KB Ok
35 Correct 7 ms 384 KB Ok
36 Correct 7 ms 384 KB Ok
37 Correct 7 ms 384 KB Ok
38 Correct 6 ms 512 KB Ok
39 Correct 6 ms 384 KB Ok
40 Correct 6 ms 512 KB Ok
41 Correct 6 ms 384 KB Ok
42 Correct 7 ms 512 KB Ok
43 Correct 6 ms 384 KB Ok
44 Correct 10 ms 640 KB Ok
45 Correct 11 ms 640 KB Ok
46 Correct 10 ms 640 KB Ok
47 Correct 11 ms 640 KB Ok
48 Correct 10 ms 640 KB Ok
49 Correct 10 ms 640 KB Ok
50 Correct 11 ms 640 KB Ok
51 Correct 11 ms 768 KB Ok
52 Correct 10 ms 640 KB Ok
53 Correct 11 ms 640 KB Ok
54 Correct 105 ms 3216 KB Ok
55 Correct 118 ms 3568 KB Ok
56 Correct 119 ms 3572 KB Ok
57 Correct 87 ms 2804 KB Ok
58 Correct 107 ms 3060 KB Ok
59 Correct 91 ms 2932 KB Ok
60 Correct 87 ms 2676 KB Ok
61 Correct 86 ms 2932 KB Ok
62 Correct 112 ms 3312 KB Ok
63 Correct 94 ms 2932 KB Ok
64 Correct 117 ms 3572 KB Ok
65 Correct 108 ms 3188 KB Ok
66 Correct 96 ms 3060 KB Ok
67 Correct 94 ms 2932 KB Ok
68 Correct 96 ms 3188 KB Ok
69 Correct 245 ms 10736 KB Ok
70 Correct 268 ms 11316 KB Ok
71 Correct 240 ms 10868 KB Ok
72 Correct 257 ms 10740 KB Ok
73 Correct 241 ms 10936 KB Ok
74 Correct 234 ms 10608 KB Ok
75 Correct 259 ms 10228 KB Ok
76 Correct 272 ms 11168 KB Ok
77 Correct 229 ms 10228 KB Ok
78 Correct 260 ms 10740 KB Ok
79 Correct 256 ms 11252 KB Ok
80 Correct 246 ms 10996 KB Ok
81 Correct 268 ms 10868 KB Ok
82 Correct 246 ms 10996 KB Ok
83 Correct 251 ms 10356 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Ok
2 Correct 4 ms 384 KB Ok
3 Correct 4 ms 384 KB Ok
4 Correct 4 ms 384 KB Ok
5 Correct 4 ms 384 KB Ok
6 Correct 5 ms 384 KB Ok
7 Correct 5 ms 512 KB Ok
8 Correct 4 ms 384 KB Ok
9 Correct 4 ms 384 KB Ok
10 Correct 4 ms 384 KB Ok
11 Correct 4 ms 384 KB Ok
12 Correct 4 ms 384 KB Ok
13 Correct 5 ms 304 KB Ok
14 Correct 4 ms 384 KB Ok
15 Correct 4 ms 384 KB Ok
16 Correct 4 ms 384 KB Ok
17 Correct 5 ms 384 KB Ok
18 Correct 7 ms 512 KB Ok
19 Correct 21 ms 1152 KB Ok
20 Correct 12 ms 768 KB Ok
21 Correct 21 ms 1280 KB Ok
22 Correct 15 ms 896 KB Ok
23 Correct 4 ms 384 KB Ok
24 Correct 4 ms 384 KB Ok
25 Correct 4 ms 384 KB Ok
26 Correct 4 ms 384 KB Ok
27 Correct 4 ms 384 KB Ok
28 Correct 4 ms 384 KB Ok
29 Correct 4 ms 384 KB Ok
30 Correct 4 ms 384 KB Ok
31 Correct 4 ms 384 KB Ok
32 Correct 5 ms 384 KB Ok
33 Correct 4 ms 384 KB Ok
34 Correct 4 ms 384 KB Ok
35 Correct 4 ms 384 KB Ok
36 Correct 5 ms 308 KB Ok
37 Correct 5 ms 384 KB Ok
38 Correct 4 ms 384 KB Ok
39 Correct 175 ms 10604 KB Ok
40 Correct 188 ms 11120 KB Ok
41 Correct 360 ms 14196 KB Ok
42 Correct 232 ms 12400 KB Ok
43 Correct 118 ms 6388 KB Ok
44 Correct 198 ms 12528 KB Ok
45 Correct 7 ms 512 KB Ok
46 Correct 7 ms 384 KB Ok
47 Correct 7 ms 384 KB Ok
48 Correct 7 ms 384 KB Ok
49 Correct 6 ms 512 KB Ok
50 Correct 6 ms 384 KB Ok
51 Correct 6 ms 512 KB Ok
52 Correct 6 ms 384 KB Ok
53 Correct 7 ms 512 KB Ok
54 Correct 6 ms 384 KB Ok
55 Correct 10 ms 640 KB Ok
56 Correct 11 ms 640 KB Ok
57 Correct 10 ms 640 KB Ok
58 Correct 11 ms 640 KB Ok
59 Correct 10 ms 640 KB Ok
60 Correct 10 ms 640 KB Ok
61 Correct 11 ms 640 KB Ok
62 Correct 11 ms 768 KB Ok
63 Correct 10 ms 640 KB Ok
64 Correct 11 ms 640 KB Ok
65 Correct 105 ms 3216 KB Ok
66 Correct 118 ms 3568 KB Ok
67 Correct 119 ms 3572 KB Ok
68 Correct 87 ms 2804 KB Ok
69 Correct 107 ms 3060 KB Ok
70 Correct 91 ms 2932 KB Ok
71 Correct 87 ms 2676 KB Ok
72 Correct 86 ms 2932 KB Ok
73 Correct 112 ms 3312 KB Ok
74 Correct 94 ms 2932 KB Ok
75 Correct 117 ms 3572 KB Ok
76 Correct 108 ms 3188 KB Ok
77 Correct 96 ms 3060 KB Ok
78 Correct 94 ms 2932 KB Ok
79 Correct 96 ms 3188 KB Ok
80 Correct 245 ms 10736 KB Ok
81 Correct 268 ms 11316 KB Ok
82 Correct 240 ms 10868 KB Ok
83 Correct 257 ms 10740 KB Ok
84 Correct 241 ms 10936 KB Ok
85 Correct 234 ms 10608 KB Ok
86 Correct 259 ms 10228 KB Ok
87 Correct 272 ms 11168 KB Ok
88 Correct 229 ms 10228 KB Ok
89 Correct 260 ms 10740 KB Ok
90 Correct 256 ms 11252 KB Ok
91 Correct 246 ms 10996 KB Ok
92 Correct 268 ms 10868 KB Ok
93 Correct 246 ms 10996 KB Ok
94 Correct 251 ms 10356 KB Ok
95 Correct 248 ms 7792 KB Ok
96 Correct 405 ms 10328 KB Ok
97 Correct 343 ms 9192 KB Ok
98 Correct 259 ms 8164 KB Ok
99 Correct 311 ms 8740 KB Ok
100 Correct 327 ms 8552 KB Ok
101 Correct 339 ms 9504 KB Ok
102 Correct 333 ms 9192 KB Ok
103 Correct 334 ms 9156 KB Ok
104 Correct 393 ms 10600 KB Ok
105 Correct 353 ms 10088 KB Ok
106 Correct 314 ms 9960 KB Ok
107 Correct 345 ms 9832 KB Ok
108 Correct 387 ms 10472 KB Ok
109 Correct 341 ms 10472 KB Ok
110 Correct 1147 ms 42728 KB Ok
111 Correct 1247 ms 45544 KB Ok
112 Correct 1134 ms 45408 KB Ok
113 Correct 1238 ms 44676 KB Ok
114 Correct 1241 ms 46680 KB Ok
115 Correct 1194 ms 44396 KB Ok
116 Correct 1206 ms 45800 KB Ok
117 Correct 1217 ms 44392 KB Ok
118 Correct 1101 ms 44264 KB Ok
119 Correct 1219 ms 44392 KB Ok
120 Correct 1104 ms 44008 KB Ok
121 Correct 1134 ms 42728 KB Ok
122 Correct 1184 ms 45160 KB Ok
123 Correct 1229 ms 45548 KB Ok
124 Correct 1130 ms 42984 KB Ok
125 Correct 1280 ms 27752 KB Ok