답안 #161636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
161636 2019-11-03T11:21:22 Z srvlt Nice sequence (IZhO18_sequence) C++14
100 / 100
1930 ms 47944 KB
#pragma GCC target ("avx2,sse2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree <int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
//#define endl "\n"

#define left fsdkfsdfsdfd
#define sum dfsdfsdfsdf
#define assign sdkfskdfkk

//#define int long long

using namespace std;

void dout() {
    cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;

int bit(signed x) {
    return __builtin_popcount(x);
}
int bit(ll x) {
    return __builtin_popcountll(x);
}

const int N = 4e5 + 123;
int n, m, k, used[N], p[N], a[N];
bool cycle;
vector <int> topsort;

void cleanup() {
    cycle = false;
    topsort = {};
    for (int i = 0; i <= k; i++) {
        used[i] = p[i] = a[i] = 0;
    }
    k = 0;
}

void dfs(int v) {
    used[v] = 1;
    if (v - n >= 0) {
        if (used[v - n] == 1) {
            cycle = true;
            return;
        }
        if (used[v - n] == 0) {
            dfs(v - n);
        }
    }
    if (v + m <= k) {
        if (used[v + m] == 1) {
            cycle = true;
            return;
        }
        if (used[v + m] == 0) {
            dfs(v + m);
        }
    }
    used[v] = 2;
}

void get(int v) {
    used[v] = 1;
    if (v - n >= 0) {
        if (!used[v - n]) {
            get(v - n);
        }
    }
    if (v + m <= k) {
        if (!used[v + m]) {
            get(v + m);
        }
    }
    topsort.pb(v);
}

bool check() {
    if (k == 0) {
        return true;
    }
    cycle = false;
    for (int i = 0; i <= k; i++) {
        if (used[i] == 0) {
            dfs(i);
        }
    }
    for (int i = 0; i <= k; i++) {
        used[i] = 0;
    }
    return !cycle;
}

void construct() {
    for (int i = 0; i <= k; i++) {
        if (used[i] == 0) {
            get(i);
        }
    }
    reverse(topsort.begin(), topsort.end());
    for (int i = 0; i <= k; i++) {
        p[topsort[i]] = i;
    }
    bool zero = false;
    for (int i = 1; i <= k; i++) {
        a[i] = p[i] - p[i - 1];
        if (a[i] == 0) {
            zero = true;
        }
    }
    cout << k << endl;
    for (int i = 1; i <= k; i++) {
        if (zero) {
            a[i]++;
        }
        cout << a[i] << " ";
    }
    cout << endl;
}

void solve(int tc) {
    // check for (int i = 0; i < n; j++)
    cin >> n >> m;
    int l = min(n, m) - 2, r = n + m + 7;
    while (l < r - 1) {
        int mid = l + r >> 1;
        k = mid;
        if (check()) {
            l = mid;
        }   else {
            r = mid;
        }
    }
    k = l;
    construct();
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int tc = 1;
    cin >> tc;
    for (int i = 0; i < tc; i++) {
        solve(i);
        cleanup();
    }
}

Compilation message

sequence.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization ("Ofast")
 
sequence.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization ("unroll-loops")
 
sequence.cpp: In function 'void solve(int)':
sequence.cpp:151:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid = l + r >> 1;
                   ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 376 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 376 KB Ok
5 Correct 2 ms 376 KB Ok
6 Correct 2 ms 376 KB Ok
7 Correct 2 ms 504 KB Ok
8 Correct 2 ms 376 KB Ok
9 Correct 2 ms 376 KB Ok
10 Correct 2 ms 376 KB Ok
11 Correct 2 ms 376 KB Ok
12 Correct 3 ms 376 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 376 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 376 KB Ok
5 Correct 0 ms 380 KB Ok
6 Correct 5 ms 508 KB Ok
7 Correct 19 ms 1272 KB Ok
8 Correct 9 ms 760 KB Ok
9 Correct 23 ms 1400 KB Ok
10 Correct 12 ms 888 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 376 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 376 KB Ok
5 Correct 2 ms 376 KB Ok
6 Correct 2 ms 376 KB Ok
7 Correct 2 ms 376 KB Ok
8 Correct 2 ms 376 KB Ok
9 Correct 2 ms 376 KB Ok
10 Correct 2 ms 376 KB Ok
11 Correct 2 ms 376 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 504 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 380 KB Ok
5 Correct 2 ms 376 KB Ok
6 Correct 217 ms 11424 KB Ok
7 Correct 180 ms 11892 KB Ok
8 Correct 335 ms 14240 KB Ok
9 Correct 290 ms 14100 KB Ok
10 Correct 159 ms 8048 KB Ok
11 Correct 303 ms 14628 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 376 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 376 KB Ok
5 Correct 2 ms 376 KB Ok
6 Correct 2 ms 376 KB Ok
7 Correct 2 ms 504 KB Ok
8 Correct 2 ms 376 KB Ok
9 Correct 2 ms 376 KB Ok
10 Correct 2 ms 376 KB Ok
11 Correct 2 ms 376 KB Ok
12 Correct 3 ms 376 KB Ok
13 Correct 2 ms 376 KB Ok
14 Correct 2 ms 376 KB Ok
15 Correct 2 ms 376 KB Ok
16 Correct 2 ms 376 KB Ok
17 Correct 2 ms 376 KB Ok
18 Correct 2 ms 376 KB Ok
19 Correct 2 ms 376 KB Ok
20 Correct 2 ms 376 KB Ok
21 Correct 2 ms 376 KB Ok
22 Correct 2 ms 376 KB Ok
23 Correct 2 ms 376 KB Ok
24 Correct 5 ms 504 KB Ok
25 Correct 5 ms 504 KB Ok
26 Correct 7 ms 504 KB Ok
27 Correct 5 ms 504 KB Ok
28 Correct 4 ms 504 KB Ok
29 Correct 4 ms 504 KB Ok
30 Correct 4 ms 380 KB Ok
31 Correct 5 ms 504 KB Ok
32 Correct 5 ms 504 KB Ok
33 Correct 5 ms 504 KB Ok
34 Correct 9 ms 632 KB Ok
35 Correct 9 ms 632 KB Ok
36 Correct 9 ms 760 KB Ok
37 Correct 9 ms 764 KB Ok
38 Correct 9 ms 632 KB Ok
39 Correct 9 ms 760 KB Ok
40 Correct 12 ms 760 KB Ok
41 Correct 9 ms 764 KB Ok
42 Correct 10 ms 760 KB Ok
43 Correct 10 ms 760 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 376 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 376 KB Ok
5 Correct 2 ms 376 KB Ok
6 Correct 2 ms 376 KB Ok
7 Correct 2 ms 504 KB Ok
8 Correct 2 ms 376 KB Ok
9 Correct 2 ms 376 KB Ok
10 Correct 2 ms 376 KB Ok
11 Correct 2 ms 376 KB Ok
12 Correct 3 ms 376 KB Ok
13 Correct 2 ms 376 KB Ok
14 Correct 2 ms 376 KB Ok
15 Correct 2 ms 376 KB Ok
16 Correct 2 ms 376 KB Ok
17 Correct 0 ms 380 KB Ok
18 Correct 5 ms 508 KB Ok
19 Correct 19 ms 1272 KB Ok
20 Correct 9 ms 760 KB Ok
21 Correct 23 ms 1400 KB Ok
22 Correct 12 ms 888 KB Ok
23 Correct 2 ms 376 KB Ok
24 Correct 2 ms 376 KB Ok
25 Correct 2 ms 376 KB Ok
26 Correct 2 ms 376 KB Ok
27 Correct 2 ms 376 KB Ok
28 Correct 2 ms 376 KB Ok
29 Correct 2 ms 376 KB Ok
30 Correct 2 ms 376 KB Ok
31 Correct 2 ms 376 KB Ok
32 Correct 2 ms 376 KB Ok
33 Correct 2 ms 376 KB Ok
34 Correct 5 ms 504 KB Ok
35 Correct 5 ms 504 KB Ok
36 Correct 7 ms 504 KB Ok
37 Correct 5 ms 504 KB Ok
38 Correct 4 ms 504 KB Ok
39 Correct 4 ms 504 KB Ok
40 Correct 4 ms 380 KB Ok
41 Correct 5 ms 504 KB Ok
42 Correct 5 ms 504 KB Ok
43 Correct 5 ms 504 KB Ok
44 Correct 9 ms 632 KB Ok
45 Correct 9 ms 632 KB Ok
46 Correct 9 ms 760 KB Ok
47 Correct 9 ms 764 KB Ok
48 Correct 9 ms 632 KB Ok
49 Correct 9 ms 760 KB Ok
50 Correct 12 ms 760 KB Ok
51 Correct 9 ms 764 KB Ok
52 Correct 10 ms 760 KB Ok
53 Correct 10 ms 760 KB Ok
54 Correct 132 ms 4140 KB Ok
55 Correct 168 ms 4620 KB Ok
56 Correct 154 ms 4508 KB Ok
57 Correct 106 ms 3572 KB Ok
58 Correct 142 ms 4424 KB Ok
59 Correct 137 ms 4216 KB Ok
60 Correct 115 ms 3924 KB Ok
61 Correct 110 ms 3828 KB Ok
62 Correct 167 ms 4724 KB Ok
63 Correct 123 ms 4112 KB Ok
64 Correct 155 ms 4472 KB Ok
65 Correct 140 ms 4312 KB Ok
66 Correct 123 ms 4056 KB Ok
67 Correct 109 ms 3744 KB Ok
68 Correct 133 ms 4336 KB Ok
69 Correct 320 ms 11336 KB Ok
70 Correct 324 ms 11944 KB Ok
71 Correct 310 ms 11504 KB Ok
72 Correct 307 ms 11252 KB Ok
73 Correct 300 ms 11608 KB Ok
74 Correct 310 ms 11328 KB Ok
75 Correct 297 ms 11252 KB Ok
76 Correct 318 ms 11636 KB Ok
77 Correct 298 ms 11316 KB Ok
78 Correct 345 ms 11268 KB Ok
79 Correct 315 ms 11636 KB Ok
80 Correct 308 ms 11376 KB Ok
81 Correct 296 ms 11696 KB Ok
82 Correct 330 ms 11380 KB Ok
83 Correct 311 ms 11404 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Ok
2 Correct 2 ms 376 KB Ok
3 Correct 2 ms 376 KB Ok
4 Correct 2 ms 376 KB Ok
5 Correct 2 ms 376 KB Ok
6 Correct 2 ms 376 KB Ok
7 Correct 2 ms 504 KB Ok
8 Correct 2 ms 376 KB Ok
9 Correct 2 ms 376 KB Ok
10 Correct 2 ms 376 KB Ok
11 Correct 2 ms 376 KB Ok
12 Correct 3 ms 376 KB Ok
13 Correct 2 ms 376 KB Ok
14 Correct 2 ms 376 KB Ok
15 Correct 2 ms 376 KB Ok
16 Correct 2 ms 376 KB Ok
17 Correct 0 ms 380 KB Ok
18 Correct 5 ms 508 KB Ok
19 Correct 19 ms 1272 KB Ok
20 Correct 9 ms 760 KB Ok
21 Correct 23 ms 1400 KB Ok
22 Correct 12 ms 888 KB Ok
23 Correct 2 ms 376 KB Ok
24 Correct 2 ms 376 KB Ok
25 Correct 2 ms 376 KB Ok
26 Correct 2 ms 376 KB Ok
27 Correct 2 ms 376 KB Ok
28 Correct 2 ms 376 KB Ok
29 Correct 2 ms 376 KB Ok
30 Correct 2 ms 376 KB Ok
31 Correct 2 ms 376 KB Ok
32 Correct 2 ms 376 KB Ok
33 Correct 2 ms 376 KB Ok
34 Correct 2 ms 376 KB Ok
35 Correct 2 ms 504 KB Ok
36 Correct 2 ms 376 KB Ok
37 Correct 2 ms 380 KB Ok
38 Correct 2 ms 376 KB Ok
39 Correct 217 ms 11424 KB Ok
40 Correct 180 ms 11892 KB Ok
41 Correct 335 ms 14240 KB Ok
42 Correct 290 ms 14100 KB Ok
43 Correct 159 ms 8048 KB Ok
44 Correct 303 ms 14628 KB Ok
45 Correct 5 ms 504 KB Ok
46 Correct 5 ms 504 KB Ok
47 Correct 7 ms 504 KB Ok
48 Correct 5 ms 504 KB Ok
49 Correct 4 ms 504 KB Ok
50 Correct 4 ms 504 KB Ok
51 Correct 4 ms 380 KB Ok
52 Correct 5 ms 504 KB Ok
53 Correct 5 ms 504 KB Ok
54 Correct 5 ms 504 KB Ok
55 Correct 9 ms 632 KB Ok
56 Correct 9 ms 632 KB Ok
57 Correct 9 ms 760 KB Ok
58 Correct 9 ms 764 KB Ok
59 Correct 9 ms 632 KB Ok
60 Correct 9 ms 760 KB Ok
61 Correct 12 ms 760 KB Ok
62 Correct 9 ms 764 KB Ok
63 Correct 10 ms 760 KB Ok
64 Correct 10 ms 760 KB Ok
65 Correct 132 ms 4140 KB Ok
66 Correct 168 ms 4620 KB Ok
67 Correct 154 ms 4508 KB Ok
68 Correct 106 ms 3572 KB Ok
69 Correct 142 ms 4424 KB Ok
70 Correct 137 ms 4216 KB Ok
71 Correct 115 ms 3924 KB Ok
72 Correct 110 ms 3828 KB Ok
73 Correct 167 ms 4724 KB Ok
74 Correct 123 ms 4112 KB Ok
75 Correct 155 ms 4472 KB Ok
76 Correct 140 ms 4312 KB Ok
77 Correct 123 ms 4056 KB Ok
78 Correct 109 ms 3744 KB Ok
79 Correct 133 ms 4336 KB Ok
80 Correct 320 ms 11336 KB Ok
81 Correct 324 ms 11944 KB Ok
82 Correct 310 ms 11504 KB Ok
83 Correct 307 ms 11252 KB Ok
84 Correct 300 ms 11608 KB Ok
85 Correct 310 ms 11328 KB Ok
86 Correct 297 ms 11252 KB Ok
87 Correct 318 ms 11636 KB Ok
88 Correct 298 ms 11316 KB Ok
89 Correct 345 ms 11268 KB Ok
90 Correct 315 ms 11636 KB Ok
91 Correct 308 ms 11376 KB Ok
92 Correct 296 ms 11696 KB Ok
93 Correct 330 ms 11380 KB Ok
94 Correct 311 ms 11404 KB Ok
95 Correct 357 ms 10064 KB Ok
96 Correct 514 ms 14568 KB Ok
97 Correct 505 ms 12648 KB Ok
98 Correct 360 ms 11440 KB Ok
99 Correct 429 ms 11624 KB Ok
100 Correct 469 ms 12516 KB Ok
101 Correct 445 ms 12776 KB Ok
102 Correct 444 ms 12520 KB Ok
103 Correct 439 ms 13052 KB Ok
104 Correct 513 ms 14236 KB Ok
105 Correct 493 ms 14324 KB Ok
106 Correct 423 ms 13032 KB Ok
107 Correct 479 ms 13576 KB Ok
108 Correct 525 ms 14824 KB Ok
109 Correct 477 ms 14056 KB Ok
110 Correct 1505 ms 45416 KB Ok
111 Correct 1834 ms 47136 KB Ok
112 Correct 1510 ms 47444 KB Ok
113 Correct 1704 ms 45936 KB Ok
114 Correct 1581 ms 47876 KB Ok
115 Correct 1734 ms 47196 KB Ok
116 Correct 1675 ms 47944 KB Ok
117 Correct 1731 ms 46424 KB Ok
118 Correct 1561 ms 47852 KB Ok
119 Correct 1930 ms 45816 KB Ok
120 Correct 1695 ms 46576 KB Ok
121 Correct 1592 ms 46052 KB Ok
122 Correct 1930 ms 46632 KB Ok
123 Correct 1779 ms 47148 KB Ok
124 Correct 1499 ms 47864 KB Ok
125 Correct 1293 ms 29296 KB Ok