답안 #344177

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
344177 2021-01-05T08:24:33 Z VEGAnn Nice sequence (IZhO18_sequence) C++14
100 / 100
498 ms 44628 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 = n + m - __gcd(n, m) - 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 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 1 ms 364 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 2 ms 492 KB Ok
7 Correct 10 ms 1132 KB Ok
8 Correct 5 ms 620 KB Ok
9 Correct 11 ms 1132 KB Ok
10 Correct 10 ms 876 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 90 ms 7912 KB Ok
7 Correct 80 ms 11112 KB Ok
8 Correct 150 ms 14084 KB Ok
9 Correct 117 ms 12392 KB Ok
10 Correct 60 ms 5868 KB Ok
11 Correct 102 ms 9320 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 1 ms 364 KB Ok
13 Correct 1 ms 364 KB Ok
14 Correct 1 ms 364 KB Ok
15 Correct 1 ms 364 KB Ok
16 Correct 1 ms 364 KB Ok
17 Correct 1 ms 364 KB Ok
18 Correct 1 ms 364 KB Ok
19 Correct 1 ms 364 KB Ok
20 Correct 1 ms 364 KB Ok
21 Correct 1 ms 364 KB Ok
22 Correct 1 ms 364 KB Ok
23 Correct 1 ms 364 KB Ok
24 Correct 2 ms 492 KB Ok
25 Correct 2 ms 492 KB Ok
26 Correct 2 ms 492 KB Ok
27 Correct 2 ms 492 KB Ok
28 Correct 2 ms 492 KB Ok
29 Correct 2 ms 364 KB Ok
30 Correct 2 ms 364 KB Ok
31 Correct 2 ms 492 KB Ok
32 Correct 2 ms 492 KB Ok
33 Correct 2 ms 492 KB Ok
34 Correct 4 ms 620 KB Ok
35 Correct 4 ms 620 KB Ok
36 Correct 4 ms 620 KB Ok
37 Correct 4 ms 620 KB Ok
38 Correct 4 ms 620 KB Ok
39 Correct 4 ms 620 KB Ok
40 Correct 5 ms 620 KB Ok
41 Correct 4 ms 620 KB Ok
42 Correct 4 ms 620 KB Ok
43 Correct 4 ms 620 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 1 ms 364 KB Ok
13 Correct 1 ms 364 KB Ok
14 Correct 1 ms 364 KB Ok
15 Correct 1 ms 364 KB Ok
16 Correct 1 ms 364 KB Ok
17 Correct 1 ms 364 KB Ok
18 Correct 2 ms 492 KB Ok
19 Correct 10 ms 1132 KB Ok
20 Correct 5 ms 620 KB Ok
21 Correct 11 ms 1132 KB Ok
22 Correct 10 ms 876 KB Ok
23 Correct 1 ms 364 KB Ok
24 Correct 1 ms 364 KB Ok
25 Correct 1 ms 364 KB Ok
26 Correct 1 ms 364 KB Ok
27 Correct 1 ms 364 KB Ok
28 Correct 1 ms 364 KB Ok
29 Correct 1 ms 364 KB Ok
30 Correct 1 ms 364 KB Ok
31 Correct 1 ms 364 KB Ok
32 Correct 1 ms 364 KB Ok
33 Correct 1 ms 364 KB Ok
34 Correct 2 ms 492 KB Ok
35 Correct 2 ms 492 KB Ok
36 Correct 2 ms 492 KB Ok
37 Correct 2 ms 492 KB Ok
38 Correct 2 ms 492 KB Ok
39 Correct 2 ms 364 KB Ok
40 Correct 2 ms 364 KB Ok
41 Correct 2 ms 492 KB Ok
42 Correct 2 ms 492 KB Ok
43 Correct 2 ms 492 KB Ok
44 Correct 4 ms 620 KB Ok
45 Correct 4 ms 620 KB Ok
46 Correct 4 ms 620 KB Ok
47 Correct 4 ms 620 KB Ok
48 Correct 4 ms 620 KB Ok
49 Correct 4 ms 620 KB Ok
50 Correct 5 ms 620 KB Ok
51 Correct 4 ms 620 KB Ok
52 Correct 4 ms 620 KB Ok
53 Correct 4 ms 620 KB Ok
54 Correct 64 ms 3220 KB Ok
55 Correct 76 ms 3564 KB Ok
56 Correct 72 ms 3564 KB Ok
57 Correct 54 ms 2796 KB Ok
58 Correct 69 ms 3052 KB Ok
59 Correct 62 ms 2924 KB Ok
60 Correct 58 ms 2668 KB Ok
61 Correct 55 ms 2924 KB Ok
62 Correct 73 ms 3364 KB Ok
63 Correct 67 ms 2924 KB Ok
64 Correct 71 ms 3564 KB Ok
65 Correct 67 ms 3180 KB Ok
66 Correct 62 ms 3180 KB Ok
67 Correct 54 ms 2924 KB Ok
68 Correct 62 ms 3052 KB Ok
69 Correct 117 ms 10692 KB Ok
70 Correct 118 ms 11228 KB Ok
71 Correct 116 ms 10732 KB Ok
72 Correct 115 ms 10604 KB Ok
73 Correct 114 ms 10220 KB Ok
74 Correct 113 ms 9964 KB Ok
75 Correct 112 ms 10092 KB Ok
76 Correct 115 ms 10860 KB Ok
77 Correct 112 ms 9580 KB Ok
78 Correct 115 ms 9964 KB Ok
79 Correct 117 ms 10476 KB Ok
80 Correct 118 ms 10732 KB Ok
81 Correct 114 ms 10348 KB Ok
82 Correct 115 ms 10476 KB Ok
83 Correct 117 ms 9964 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 1 ms 364 KB Ok
13 Correct 1 ms 364 KB Ok
14 Correct 1 ms 364 KB Ok
15 Correct 1 ms 364 KB Ok
16 Correct 1 ms 364 KB Ok
17 Correct 1 ms 364 KB Ok
18 Correct 2 ms 492 KB Ok
19 Correct 10 ms 1132 KB Ok
20 Correct 5 ms 620 KB Ok
21 Correct 11 ms 1132 KB Ok
22 Correct 10 ms 876 KB Ok
23 Correct 1 ms 364 KB Ok
24 Correct 1 ms 364 KB Ok
25 Correct 1 ms 364 KB Ok
26 Correct 1 ms 364 KB Ok
27 Correct 1 ms 364 KB Ok
28 Correct 1 ms 364 KB Ok
29 Correct 1 ms 364 KB Ok
30 Correct 1 ms 364 KB Ok
31 Correct 1 ms 364 KB Ok
32 Correct 1 ms 364 KB Ok
33 Correct 1 ms 364 KB Ok
34 Correct 1 ms 384 KB Ok
35 Correct 1 ms 364 KB Ok
36 Correct 1 ms 364 KB Ok
37 Correct 1 ms 364 KB Ok
38 Correct 1 ms 364 KB Ok
39 Correct 90 ms 7912 KB Ok
40 Correct 80 ms 11112 KB Ok
41 Correct 150 ms 14084 KB Ok
42 Correct 117 ms 12392 KB Ok
43 Correct 60 ms 5868 KB Ok
44 Correct 102 ms 9320 KB Ok
45 Correct 2 ms 492 KB Ok
46 Correct 2 ms 492 KB Ok
47 Correct 2 ms 492 KB Ok
48 Correct 2 ms 492 KB Ok
49 Correct 2 ms 492 KB Ok
50 Correct 2 ms 364 KB Ok
51 Correct 2 ms 364 KB Ok
52 Correct 2 ms 492 KB Ok
53 Correct 2 ms 492 KB Ok
54 Correct 2 ms 492 KB Ok
55 Correct 4 ms 620 KB Ok
56 Correct 4 ms 620 KB Ok
57 Correct 4 ms 620 KB Ok
58 Correct 4 ms 620 KB Ok
59 Correct 4 ms 620 KB Ok
60 Correct 4 ms 620 KB Ok
61 Correct 5 ms 620 KB Ok
62 Correct 4 ms 620 KB Ok
63 Correct 4 ms 620 KB Ok
64 Correct 4 ms 620 KB Ok
65 Correct 64 ms 3220 KB Ok
66 Correct 76 ms 3564 KB Ok
67 Correct 72 ms 3564 KB Ok
68 Correct 54 ms 2796 KB Ok
69 Correct 69 ms 3052 KB Ok
70 Correct 62 ms 2924 KB Ok
71 Correct 58 ms 2668 KB Ok
72 Correct 55 ms 2924 KB Ok
73 Correct 73 ms 3364 KB Ok
74 Correct 67 ms 2924 KB Ok
75 Correct 71 ms 3564 KB Ok
76 Correct 67 ms 3180 KB Ok
77 Correct 62 ms 3180 KB Ok
78 Correct 54 ms 2924 KB Ok
79 Correct 62 ms 3052 KB Ok
80 Correct 117 ms 10692 KB Ok
81 Correct 118 ms 11228 KB Ok
82 Correct 116 ms 10732 KB Ok
83 Correct 115 ms 10604 KB Ok
84 Correct 114 ms 10220 KB Ok
85 Correct 113 ms 9964 KB Ok
86 Correct 112 ms 10092 KB Ok
87 Correct 115 ms 10860 KB Ok
88 Correct 112 ms 9580 KB Ok
89 Correct 115 ms 9964 KB Ok
90 Correct 117 ms 10476 KB Ok
91 Correct 118 ms 10732 KB Ok
92 Correct 114 ms 10348 KB Ok
93 Correct 115 ms 10476 KB Ok
94 Correct 117 ms 9964 KB Ok
95 Correct 157 ms 7656 KB Ok
96 Correct 231 ms 10224 KB Ok
97 Correct 221 ms 9060 KB Ok
98 Correct 168 ms 8164 KB Ok
99 Correct 202 ms 8420 KB Ok
100 Correct 203 ms 8548 KB Ok
101 Correct 206 ms 9316 KB Ok
102 Correct 202 ms 8912 KB Ok
103 Correct 197 ms 9060 KB Ok
104 Correct 234 ms 10340 KB Ok
105 Correct 220 ms 10196 KB Ok
106 Correct 195 ms 10084 KB Ok
107 Correct 215 ms 9768 KB Ok
108 Correct 232 ms 10468 KB Ok
109 Correct 215 ms 10572 KB Ok
110 Correct 479 ms 41948 KB Ok
111 Correct 486 ms 44132 KB Ok
112 Correct 494 ms 41444 KB Ok
113 Correct 481 ms 44264 KB Ok
114 Correct 498 ms 44628 KB Ok
115 Correct 474 ms 42084 KB Ok
116 Correct 490 ms 44260 KB Ok
117 Correct 477 ms 43620 KB Ok
118 Correct 492 ms 43868 KB Ok
119 Correct 479 ms 42920 KB Ok
120 Correct 497 ms 42980 KB Ok
121 Correct 478 ms 42596 KB Ok
122 Correct 480 ms 42980 KB Ok
123 Correct 494 ms 44516 KB Ok
124 Correct 472 ms 39396 KB Ok
125 Correct 403 ms 27876 KB Ok