답안 #737057

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
737057 2023-05-06T14:05:20 Z GrindMachine Nice sequence (IZhO18_sequence) C++17
0 / 100
456 ms 136088 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
https://codeforces.com/blog/entry/63991?#comment-478025
https://oj.uz/submission/682671

*/

const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<ll> adj[N];
vector<bool> vis(N), recstack(N);
vector<ll> topo;
bool cyc;

void dfs(ll u) {
    vis[u] = 1;
    recstack[u] = 1;

    trav(v, adj[u]) {
        if (vis[v]) {
            if (recstack[v]) cyc = true;
            conts;
        }

        dfs(v);
    }

    topo.pb(u);
    recstack[u] = 0;
}

void solve(int test_case)
{
    ll n, m; cin >> n >> m;

    auto ok = [&](ll mid) {
        rep(i, mid + 1) adj[i].clear(), vis[i] = 0;

        rep1(i, mid) {
            if (i - n >= 0) {
                adj[i].pb(i - n);
            }

            if (i - m >= 0) {
                adj[i - m].pb(i);
            }
        }

        topo.clear();
        cyc = false;

        rep(i, mid + 1) {
            if (vis[i]) conts;
            dfs(i);
        }

        reverse(all(topo));

        if (cyc) return false;
        return true;
    };

    ll l = 1, r = N - 1;
    ll ans = 0;

    while (l <= r) {
        ll mid = (l + r) >> 1;
        if (ok(mid)) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }

    ok(ans);

    vector<ll> p(n + 5);
    rep(i, sz(topo)) {
        p[topo[i]] = i;
    }

    cout << ans << endl;

    rep1(i, ans) {
        ll v = p[i] - p[i - 1];
        cout << v << " ";
    }

    cout << endl;
}

int main()
{
    fastio;

    int t = 1;
    cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}

Compilation message

sequence.cpp: In function 'void solve(int)':
sequence.cpp:30:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 | #define rep(i, n) for(int i = 0; i < n; ++i)
      |                                    ^
sequence.cpp:134:5: note: in expansion of macro 'rep'
  134 |     rep(i, sz(topo)) {
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 67420 KB Ok
2 Runtime error 346 ms 136088 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 67156 KB Ok
2 Runtime error 236 ms 136064 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 67388 KB Ok
2 Correct 421 ms 67416 KB Ok
3 Correct 408 ms 67124 KB Ok
4 Correct 425 ms 67300 KB Ok
5 Correct 421 ms 67388 KB Ok
6 Runtime error 362 ms 136088 KB Execution killed with signal 6
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 422 ms 67388 KB Ok
2 Runtime error 310 ms 136056 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 67420 KB Ok
2 Runtime error 346 ms 136088 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 67420 KB Ok
2 Runtime error 346 ms 136088 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 67420 KB Ok
2 Runtime error 346 ms 136088 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -