186902025-10-30 22:30:23ubormaciAutókódoláscpp17Elfogadva 50/501ms564 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*
 *
 * notes:
 *
 * int64_t
 * stoi(string s) -> string to int
 * to_string() -> int (or else) to string
 *
 * vector declaration:
 * vector<ll> v(n, 0)
 * vector<vector<ll>> v(n, vector<ll>(n, 0));
 *
 * {if statement} ? {truth value} : {false value}
 *
 * set lower bound/upper bound:
 * 	// . . . m1 . . . d . . . . m2
 *    auto m1_it = b.lower_bound(d);
 *    advance(m1_it, -1);
 *    m1 = *m1_it;
 *	m2 = *b.upper_bound(d);
 *
 * #ifdef LOCAL
 *    freopen("in.txt","r",stdin);
 *    freopen("out.txt","w",stdout);
 * #endif
 *
 * constexpr auto lcm(auto x, auto... xs)
 * {
 *	return ((x = std::lcm(x, xs)), ...);
 * }
 *
 * std::gcd(int a, int b)
 *
 * cout << setprecision(n);
 *
 * set with custom ordering
 * set<ll, decltype(&cmp)> qu(cmp);
 *
 */

typedef int64_t ll;

void solve() {

    ll n;
    cin >> n;

    ll tlen = log2(n) + 1;

    vector<ll> v(tlen+1, 0);
    vector<ll> p(tlen+1, 0);
    vector<ll> nx(tlen+1, 0);

    ll c = n;
    ll tind = 0;
    ll onecount = 0;
    while(c > 0) {
        v[tind] = c % 2;
        c /= 2;
        onecount += v[tind];
        p[tind] = v[tind];
        nx[tind] = v[tind];
        tind++;
    }   

    // hatulrol megyunk, es a legelso 01-part megforditjuk
    // az ez utan a pozicio utani (vagyis kisebb hatvanyokon levo egyeseket hatra rakjuk)
    
    //cerr << "\nv=" << v;

    bool prev = false;
    bool next = true;

    if(onecount == tlen) {
        prev = false;
    }else{
        prev = true;

        // find first 01
        // reading from opposite
        // so that zero is the smaller power
        // switch them
        // then bring up all the ones after
        for(ll i = 0; i < tlen; i++) {

            if(v[i] == 0 && v[i+1] == 1) {

                //cerr << "\ni=" << i;

                p[i] = 1;
                p[i+1] = 0; 

                ll oneback = 0;
                ll totback = i;
                for(ll j = i - 1; j >= 0; j--) {
                    oneback += v[j];
                }

                ll zeroback = totback - oneback;

                //cerr << "\nzeroback=" << zeroback;

                for(ll j = 0; j < i; j++) {
                    if(j < zeroback) {
                        p[j] = 0;
                    }else{
                        p[j] = 1;
                    }
                }

                break;
            }
        }

    }

    //cerr << "\np=" << p;

    // get next
    for(ll i = 0; i < tlen; i++) {

        if(v[i] == 1 && v[i+1] == 0) {

            //cerr << "\ni=" << i;

            nx[i] = 0;
            nx[i+1] = 1; 

            ll oneback = 0;
            for(ll j = i - 1; j >= 0; j--) {
                oneback += v[j];
            }

            for(ll j = 0; j < i; j++) {
                if(j < oneback) {
                    nx[j] = 1;
                }else{
                    nx[j] = 0;
                }
            }

            break;
        }
    }

    //cerr << "\nnx=" << nx;

    ll retprev = 0;
    ll retnext = 0;
    ll tp = 1;
    for(ll i = 0; i < tlen + 1; i++) {

        retprev += p[i] * tp;
        retnext += nx[i] * tp;
        tp *= 2;

    }

    if(prev == false) {
        retprev = -1;
    }

    cout << retprev << "\n" << retnext;

}

int main()
{
    std::ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);

    solve();

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/01ms316 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms388 KiB
7Elfogadva2/21ms564 KiB
8Elfogadva4/41ms316 KiB
9Elfogadva4/41ms316 KiB
10Elfogadva4/41ms316 KiB
11Elfogadva4/41ms316 KiB
12Elfogadva4/41ms328 KiB
13Elfogadva4/41ms316 KiB
14Elfogadva4/41ms348 KiB
15Elfogadva4/41ms508 KiB
16Elfogadva4/41ms332 KiB
17Elfogadva4/41ms316 KiB