Submission #495768

# Submission time Handle Problem Language Result Execution time Memory
495768 2021-12-20T04:38:28 Z xynex Nice sequence (IZhO18_sequence) C++17
100 / 100
1485 ms 33076 KB
/*
* Author: xynex
* Created: 20.12.2021 09:17
* Why am I so stupid? :c
* Slishkom slab
*/

#include <bits/stdc++.h>

// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse,-fgcse-lm")
// #pragma GCC optimize("-ftree-pre,-ftree-vrp")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define dl double long
#define ull unsigned long long
#define pr pair
#define vt vector
#define ff first
#define ss second
#define mp make_pair
#define sz(a) (int)a.size()
#define pb push_back
#define pf push_front
#define popB pop_back
#define popF pop_front
#define bit_count __builtin_popcount
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed << setprecision(x)

template<typename T> T get_rand(T l, T r) {
    random_device rd;
    mt19937 gen(rd());
    return uniform_int_distribution<T>(l, r)(gen);
}

template<typename T> T lcm(T a, T b) { return a * (b / __gcd(a, b)); }

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) { cin >> x; }
void read(double& d) { string t; read(t); d = stod(t); }
void read(long double& d) { string t; read(t); d = stold(t); }
template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); }
template<class A> void read(vt<A>& x) { for (auto& a : x) read(a); }
template<class A, size_t S> void read(array<A, S>& x) { for (auto& a : x) read(a); }

string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vt<bool> v) { string res; for (int i = 0; i < sz(v); ++i) res += char('0' + v[i]); return res; }
template<size_t S> string to_string(bitset<S> b) { string res; for (int i = 0; i < S; ++i) res += char('0' + b[i]); return res; }
template<class T> string to_string(T v) { bool f = 1; string res; for (auto x : v) { if (!f) res += ' '; f = 0; res += to_string(x); } return res; }

template<class A> void write(A x) { cout << to_string(x); }
template<class H, class... T> void write(const H& h, const T&... t) { write(h); write(t...); }

void print() { write("\n"); }
template<class H, class... T> void print(const H& h, const T&... t) { write(h); if (sizeof...(t)) write(' '); print(t...); }

void freop(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int MOD = 1e9 + 7;
const int N = 3e6 + 5;
const ll INF = 9e18;
const int M = 3e3 + 5;
const dl pi = acos(-1);
const dl eps = 1e-12;
const int sq = 700;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
/* ll vs int*/

void precalc() {

}

int n, m;
int _[N];
int ans[N];
int cnt[N];
bool ok;
void made_array(int lim) {
    for(int i = 0; i <= lim; ++i) _[i] = 0, cnt[i] = 0;
    for(int i = 0; i <= lim; ++i) {
        if(i >= n) _[i - n]++;
        if((i + m) <= lim) _[i + m]++;
    }
    queue<int> q;
    for(int i = 0; i <= lim; ++i) {
        if(_[i]) continue;
        q.push(i);
    }
    if(q.empty()) {
        ok = 0;
    }
    int whr = 1;
    while(!q.empty()) {
        int v = q.front();
        q.pop();
        cnt[v] = whr++;
        if(v >= n && _[v - n]) {
            _[v - n]--;
            if(!_[v - n]) q.push(v - n);
        }
        if((v + m) <= lim && _[v + m]) {
            _[v + m]--;
            if(!_[v + m]) q.push(v + m);
        }
    }
}
bool check(int lim) {
    for(int i = 1; i <= lim; ++i) ans[i] = cnt[i] - cnt[i - 1];
    int cnt = 0;
    /*for(int i = 1; i <= lim; ++i) write(ans[i], ' ');
    print("");*/
    for(int i = 1; i <= lim; ++i) {
        if(i > n) cnt -= ans[i - n];
        cnt += ans[i];
        if(i >= n) {
            if(cnt >= 0) {
                return 0;
            }
        }
    }
    cnt = 0;
    for(int i = 1; i <= lim; ++i) {
        if(i > m) cnt -= ans[i - m];
        cnt += ans[i];
        if(i >= m) {
            if(cnt <= 0) {
                return 0;
            }
        }
    }
    return 1;
}
void solve() {
    read(n, m);
    int l = min(n - 1, m - 1), r = n + m;
  //  print("X");
    while((r - l) >= 0) {
        int mid = (r + l) >> 1;
        ok = 1;
        made_array(mid);
        //print(mid);
        if(check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    l--;
    made_array(l);
    if(!check(l)) {
        print(0);
        return;
    }
    print(l);
    for(int i = 1; i <= l; ++i) write(ans[i], ' ');
    print("");
}

int main() {
    //freop("");
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    read(t);
    precalc();
    for (int i = 1; i <= t; ++i) {
        //write("Case #" + to_string(i) + ": ");
        solve();
    }
    return 0;
}

Compilation message

sequence.cpp: In function 'void freop(std::string)':
sequence.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Ok
2 Correct 1 ms 204 KB Ok
3 Correct 1 ms 204 KB Ok
4 Correct 1 ms 332 KB Ok
5 Correct 0 ms 332 KB Ok
6 Correct 1 ms 332 KB Ok
7 Correct 1 ms 332 KB Ok
8 Correct 1 ms 332 KB Ok
9 Correct 1 ms 332 KB Ok
10 Correct 1 ms 332 KB Ok
11 Correct 1 ms 332 KB Ok
12 Correct 1 ms 332 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 252 KB Ok
2 Correct 1 ms 384 KB Ok
3 Correct 1 ms 332 KB Ok
4 Correct 0 ms 204 KB Ok
5 Correct 1 ms 204 KB Ok
6 Correct 4 ms 332 KB Ok
7 Correct 23 ms 716 KB Ok
8 Correct 10 ms 460 KB Ok
9 Correct 28 ms 776 KB Ok
10 Correct 17 ms 552 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Ok
2 Correct 0 ms 204 KB Ok
3 Correct 0 ms 204 KB Ok
4 Correct 1 ms 332 KB Ok
5 Correct 1 ms 328 KB Ok
6 Correct 1 ms 336 KB Ok
7 Correct 1 ms 312 KB Ok
8 Correct 1 ms 336 KB Ok
9 Correct 1 ms 208 KB Ok
10 Correct 1 ms 320 KB Ok
11 Correct 1 ms 208 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Ok
2 Correct 1 ms 332 KB Ok
3 Correct 1 ms 324 KB Ok
4 Correct 1 ms 336 KB Ok
5 Correct 1 ms 324 KB Ok
6 Correct 345 ms 5100 KB Ok
7 Correct 249 ms 4744 KB Ok
8 Correct 489 ms 6852 KB Ok
9 Correct 335 ms 7188 KB Ok
10 Correct 187 ms 3268 KB Ok
11 Correct 334 ms 6284 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Ok
2 Correct 1 ms 204 KB Ok
3 Correct 1 ms 204 KB Ok
4 Correct 1 ms 332 KB Ok
5 Correct 0 ms 332 KB Ok
6 Correct 1 ms 332 KB Ok
7 Correct 1 ms 332 KB Ok
8 Correct 1 ms 332 KB Ok
9 Correct 1 ms 332 KB Ok
10 Correct 1 ms 332 KB Ok
11 Correct 1 ms 332 KB Ok
12 Correct 1 ms 332 KB Ok
13 Correct 0 ms 204 KB Ok
14 Correct 0 ms 204 KB Ok
15 Correct 0 ms 204 KB Ok
16 Correct 1 ms 332 KB Ok
17 Correct 1 ms 328 KB Ok
18 Correct 1 ms 336 KB Ok
19 Correct 1 ms 312 KB Ok
20 Correct 1 ms 336 KB Ok
21 Correct 1 ms 208 KB Ok
22 Correct 1 ms 320 KB Ok
23 Correct 1 ms 208 KB Ok
24 Correct 5 ms 336 KB Ok
25 Correct 6 ms 336 KB Ok
26 Correct 6 ms 336 KB Ok
27 Correct 6 ms 408 KB Ok
28 Correct 5 ms 320 KB Ok
29 Correct 4 ms 328 KB Ok
30 Correct 4 ms 336 KB Ok
31 Correct 4 ms 336 KB Ok
32 Correct 5 ms 384 KB Ok
33 Correct 6 ms 336 KB Ok
34 Correct 9 ms 464 KB Ok
35 Correct 10 ms 532 KB Ok
36 Correct 10 ms 464 KB Ok
37 Correct 10 ms 464 KB Ok
38 Correct 10 ms 500 KB Ok
39 Correct 8 ms 464 KB Ok
40 Correct 10 ms 464 KB Ok
41 Correct 10 ms 532 KB Ok
42 Correct 10 ms 464 KB Ok
43 Correct 10 ms 464 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Ok
2 Correct 1 ms 204 KB Ok
3 Correct 1 ms 204 KB Ok
4 Correct 1 ms 332 KB Ok
5 Correct 0 ms 332 KB Ok
6 Correct 1 ms 332 KB Ok
7 Correct 1 ms 332 KB Ok
8 Correct 1 ms 332 KB Ok
9 Correct 1 ms 332 KB Ok
10 Correct 1 ms 332 KB Ok
11 Correct 1 ms 332 KB Ok
12 Correct 1 ms 332 KB Ok
13 Correct 1 ms 252 KB Ok
14 Correct 1 ms 384 KB Ok
15 Correct 1 ms 332 KB Ok
16 Correct 0 ms 204 KB Ok
17 Correct 1 ms 204 KB Ok
18 Correct 4 ms 332 KB Ok
19 Correct 23 ms 716 KB Ok
20 Correct 10 ms 460 KB Ok
21 Correct 28 ms 776 KB Ok
22 Correct 17 ms 552 KB Ok
23 Correct 0 ms 204 KB Ok
24 Correct 0 ms 204 KB Ok
25 Correct 0 ms 204 KB Ok
26 Correct 1 ms 332 KB Ok
27 Correct 1 ms 328 KB Ok
28 Correct 1 ms 336 KB Ok
29 Correct 1 ms 312 KB Ok
30 Correct 1 ms 336 KB Ok
31 Correct 1 ms 208 KB Ok
32 Correct 1 ms 320 KB Ok
33 Correct 1 ms 208 KB Ok
34 Correct 5 ms 336 KB Ok
35 Correct 6 ms 336 KB Ok
36 Correct 6 ms 336 KB Ok
37 Correct 6 ms 408 KB Ok
38 Correct 5 ms 320 KB Ok
39 Correct 4 ms 328 KB Ok
40 Correct 4 ms 336 KB Ok
41 Correct 4 ms 336 KB Ok
42 Correct 5 ms 384 KB Ok
43 Correct 6 ms 336 KB Ok
44 Correct 9 ms 464 KB Ok
45 Correct 10 ms 532 KB Ok
46 Correct 10 ms 464 KB Ok
47 Correct 10 ms 464 KB Ok
48 Correct 10 ms 500 KB Ok
49 Correct 8 ms 464 KB Ok
50 Correct 10 ms 464 KB Ok
51 Correct 10 ms 532 KB Ok
52 Correct 10 ms 464 KB Ok
53 Correct 10 ms 464 KB Ok
54 Correct 193 ms 2852 KB Ok
55 Correct 241 ms 3100 KB Ok
56 Correct 246 ms 3224 KB Ok
57 Correct 196 ms 2612 KB Ok
58 Correct 215 ms 2940 KB Ok
59 Correct 230 ms 2820 KB Ok
60 Correct 195 ms 2612 KB Ok
61 Correct 170 ms 2636 KB Ok
62 Correct 282 ms 2988 KB Ok
63 Correct 180 ms 2720 KB Ok
64 Correct 223 ms 3012 KB Ok
65 Correct 217 ms 2976 KB Ok
66 Correct 184 ms 2956 KB Ok
67 Correct 179 ms 2616 KB Ok
68 Correct 202 ms 2908 KB Ok
69 Correct 329 ms 6856 KB Ok
70 Correct 324 ms 8112 KB Ok
71 Correct 323 ms 6088 KB Ok
72 Correct 336 ms 7240 KB Ok
73 Correct 326 ms 6652 KB Ok
74 Correct 374 ms 5576 KB Ok
75 Correct 312 ms 5696 KB Ok
76 Correct 322 ms 7860 KB Ok
77 Correct 307 ms 5168 KB Ok
78 Correct 324 ms 7612 KB Ok
79 Correct 334 ms 6988 KB Ok
80 Correct 362 ms 6708 KB Ok
81 Correct 341 ms 7320 KB Ok
82 Correct 370 ms 6748 KB Ok
83 Correct 334 ms 5792 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Ok
2 Correct 1 ms 204 KB Ok
3 Correct 1 ms 204 KB Ok
4 Correct 1 ms 332 KB Ok
5 Correct 0 ms 332 KB Ok
6 Correct 1 ms 332 KB Ok
7 Correct 1 ms 332 KB Ok
8 Correct 1 ms 332 KB Ok
9 Correct 1 ms 332 KB Ok
10 Correct 1 ms 332 KB Ok
11 Correct 1 ms 332 KB Ok
12 Correct 1 ms 332 KB Ok
13 Correct 1 ms 252 KB Ok
14 Correct 1 ms 384 KB Ok
15 Correct 1 ms 332 KB Ok
16 Correct 0 ms 204 KB Ok
17 Correct 1 ms 204 KB Ok
18 Correct 4 ms 332 KB Ok
19 Correct 23 ms 716 KB Ok
20 Correct 10 ms 460 KB Ok
21 Correct 28 ms 776 KB Ok
22 Correct 17 ms 552 KB Ok
23 Correct 0 ms 204 KB Ok
24 Correct 0 ms 204 KB Ok
25 Correct 0 ms 204 KB Ok
26 Correct 1 ms 332 KB Ok
27 Correct 1 ms 328 KB Ok
28 Correct 1 ms 336 KB Ok
29 Correct 1 ms 312 KB Ok
30 Correct 1 ms 336 KB Ok
31 Correct 1 ms 208 KB Ok
32 Correct 1 ms 320 KB Ok
33 Correct 1 ms 208 KB Ok
34 Correct 0 ms 332 KB Ok
35 Correct 1 ms 332 KB Ok
36 Correct 1 ms 324 KB Ok
37 Correct 1 ms 336 KB Ok
38 Correct 1 ms 324 KB Ok
39 Correct 345 ms 5100 KB Ok
40 Correct 249 ms 4744 KB Ok
41 Correct 489 ms 6852 KB Ok
42 Correct 335 ms 7188 KB Ok
43 Correct 187 ms 3268 KB Ok
44 Correct 334 ms 6284 KB Ok
45 Correct 5 ms 336 KB Ok
46 Correct 6 ms 336 KB Ok
47 Correct 6 ms 336 KB Ok
48 Correct 6 ms 408 KB Ok
49 Correct 5 ms 320 KB Ok
50 Correct 4 ms 328 KB Ok
51 Correct 4 ms 336 KB Ok
52 Correct 4 ms 336 KB Ok
53 Correct 5 ms 384 KB Ok
54 Correct 6 ms 336 KB Ok
55 Correct 9 ms 464 KB Ok
56 Correct 10 ms 532 KB Ok
57 Correct 10 ms 464 KB Ok
58 Correct 10 ms 464 KB Ok
59 Correct 10 ms 500 KB Ok
60 Correct 8 ms 464 KB Ok
61 Correct 10 ms 464 KB Ok
62 Correct 10 ms 532 KB Ok
63 Correct 10 ms 464 KB Ok
64 Correct 10 ms 464 KB Ok
65 Correct 193 ms 2852 KB Ok
66 Correct 241 ms 3100 KB Ok
67 Correct 246 ms 3224 KB Ok
68 Correct 196 ms 2612 KB Ok
69 Correct 215 ms 2940 KB Ok
70 Correct 230 ms 2820 KB Ok
71 Correct 195 ms 2612 KB Ok
72 Correct 170 ms 2636 KB Ok
73 Correct 282 ms 2988 KB Ok
74 Correct 180 ms 2720 KB Ok
75 Correct 223 ms 3012 KB Ok
76 Correct 217 ms 2976 KB Ok
77 Correct 184 ms 2956 KB Ok
78 Correct 179 ms 2616 KB Ok
79 Correct 202 ms 2908 KB Ok
80 Correct 329 ms 6856 KB Ok
81 Correct 324 ms 8112 KB Ok
82 Correct 323 ms 6088 KB Ok
83 Correct 336 ms 7240 KB Ok
84 Correct 326 ms 6652 KB Ok
85 Correct 374 ms 5576 KB Ok
86 Correct 312 ms 5696 KB Ok
87 Correct 322 ms 7860 KB Ok
88 Correct 307 ms 5168 KB Ok
89 Correct 324 ms 7612 KB Ok
90 Correct 334 ms 6988 KB Ok
91 Correct 362 ms 6708 KB Ok
92 Correct 341 ms 7320 KB Ok
93 Correct 370 ms 6748 KB Ok
94 Correct 334 ms 5792 KB Ok
95 Correct 517 ms 7132 KB Ok
96 Correct 763 ms 9952 KB Ok
97 Correct 724 ms 8852 KB Ok
98 Correct 539 ms 8084 KB Ok
99 Correct 677 ms 8188 KB Ok
100 Correct 670 ms 8068 KB Ok
101 Correct 706 ms 8972 KB Ok
102 Correct 647 ms 8208 KB Ok
103 Correct 682 ms 8520 KB Ok
104 Correct 830 ms 9892 KB Ok
105 Correct 754 ms 9336 KB Ok
106 Correct 599 ms 8920 KB Ok
107 Correct 694 ms 9260 KB Ok
108 Correct 790 ms 9772 KB Ok
109 Correct 683 ms 9508 KB Ok
110 Correct 1456 ms 23088 KB Ok
111 Correct 1452 ms 33076 KB Ok
112 Correct 1444 ms 25236 KB Ok
113 Correct 1421 ms 29300 KB Ok
114 Correct 1458 ms 30668 KB Ok
115 Correct 1454 ms 29904 KB Ok
116 Correct 1419 ms 29740 KB Ok
117 Correct 1445 ms 30700 KB Ok
118 Correct 1400 ms 26136 KB Ok
119 Correct 1485 ms 31736 KB Ok
120 Correct 1467 ms 26288 KB Ok
121 Correct 1470 ms 25148 KB Ok
122 Correct 1449 ms 28200 KB Ok
123 Correct 1423 ms 33024 KB Ok
124 Correct 1411 ms 22200 KB Ok
125 Correct 1371 ms 15300 KB Ok