57582023-09-17 17:30:12kukkermanAutókódoláscpp14Accepted 50/503ms3980 KiB
#include <iostream>

int64_t beolvas(std::istream &in) {
    int64_t k;
    in >> k;

    return k;
}
// elozo:
//   ... | 1 | 0{n} | 1{e}  =>  ... | 0 | 1{e+1} | 0{n-1}
// 
// kovetkezo:
//   ... | 0 | 1{e} | 0{n}  =>  ... | 1 | 0{n+1} | 1{e-1}

std::pair<int64_t, int64_t> feldolgoz_ciklus_nelkul(int64_t k) {
    // elozo kod
    int64_t elozo_kod;
    auto elso_nulla_bit = ~k & (k + 1);
    if (elso_nulla_bit < k) {
        auto y = k & ~(elso_nulla_bit - 1);
        auto kov_egyes_bit = y & -y;
        auto felso_bitek = k & ~(kov_egyes_bit * 2 - 1);
        elozo_kod = felso_bitek | (elso_nulla_bit * 2 - 1) * (kov_egyes_bit / elso_nulla_bit / 2);

    } else {
        elozo_kod = -1;
    }

    // kovetkezo kod
    auto elso_egyes_bit = k & -k;
    auto y = k | (elso_egyes_bit - 1);
    auto kov_nulla_bit = ~y & (y + 1);
    auto felso_bitek = k & ~(kov_nulla_bit - 1);
    int64_t kovetkezo_kod = felso_bitek | kov_nulla_bit | (kov_nulla_bit - 1) / elso_egyes_bit / 2;

    if (kovetkezo_kod > 1000000000000LL) {
        kovetkezo_kod = -1;
    }

    return { elozo_kod, kovetkezo_kod };
}

std::pair<int64_t, int64_t> feldolgoz(int64_t k) {
    int64_t bit = 1;
    int64_t e = 0;
    while ((k & bit) != 0) {
        e++;
        bit <<= 1;
    }

    int64_t elozo_kod;
    if (bit < k) {
        int64_t n = 0;
        while ((k & bit) == 0) {
            n++;
            bit <<= 1;
        }

        int64_t also_maszk = (1LL << (n + e + 1)) - 1;
        elozo_kod = (k & ~also_maszk) | (((1LL << (e + 1)) - 1) << (n - 1));

    } else {
        elozo_kod = -1;
    }

    bit = 1;
    int64_t n = 0;
    while ((k & bit) == 0) {
        n++;
        bit <<= 1;
    }

    e = 0;
    while ((k & bit) != 0) {
        e++;
        bit <<= 1;
    }

    int64_t also_maszk = (1LL << (n + e + 1)) - 1;
    int64_t kovetkezo_kod = (k & ~also_maszk) | (1LL << (n + e)) | ((1 << (e - 1)) - 1);

    if (kovetkezo_kod > 1000000000000LL) {
        kovetkezo_kod = -1;
    }

    return { elozo_kod, kovetkezo_kod };
}

int main() {
    const auto k = beolvas(std::cin);
    const auto p = feldolgoz_ciklus_nelkul(k);

    std::cout << p.first << '\n' << p.second << std::endl;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1872 KiB
2Accepted0/03ms2076 KiB
3Accepted2/23ms2284 KiB
4Accepted2/23ms2536 KiB
5Accepted2/23ms2868 KiB
6Accepted2/23ms3032 KiB
7Accepted2/22ms3128 KiB
8Accepted4/43ms3152 KiB
9Accepted4/42ms3140 KiB
10Accepted4/42ms3144 KiB
11Accepted4/43ms3392 KiB
12Accepted4/43ms3508 KiB
13Accepted4/43ms3696 KiB
14Accepted4/43ms3880 KiB
15Accepted4/42ms3868 KiB
16Accepted4/42ms3868 KiB
17Accepted4/42ms3980 KiB