214502026-01-13 09:12:34hunzombiAutókódoláscpp17Partially correct 46/502ms520 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

long long findHigh(long long n) {
    long long target = __builtin_popcount(n);
    for (long long i=1; i <= n; i *= 2) {
        if (n & i) {
            n += i;
            break;
        }
    }
    long long missing = target - __builtin_popcount(n);
    long long i = 1;
    while (missing-- > 0) {
        if (!(n & i)) {
            n += i;
            i *= 2;
        }
    }

    return n;
}

ll findLow(ll n) {
    ll target  = __builtin_popcount(n);
    ll last_index;
    for (long long i=1; i <= n; i *= 2) {
        if (n & i) {
            last_index = i;
        }
    }
    for (ll i=1; i <= last_index; i *= 2) {
        target--;
    }
    if (target == 0) return -1;
    for (long long i=2; i <= n; i *= 2) {
        if (n & i && !(n & (i / 2))) {
            n ^= 3 * i / 2;
            break;
        }
    }
    return n;
}

int main()
{
    ll n;
    cin >> n;
    cout << findLow(n) << '\n' << findHigh(n) << '\n';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base46/50
1Accepted0/01ms316 KiB
2Accepted0/01ms520 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms316 KiB
5Partially correct1/21ms316 KiB
6Partially correct1/21ms316 KiB
7Accepted2/22ms316 KiB
8Accepted4/41ms336 KiB
9Accepted4/41ms316 KiB
10Accepted4/41ms316 KiB
11Accepted4/41ms316 KiB
12Accepted4/41ms316 KiB
13Partially correct2/41ms344 KiB
14Accepted4/41ms316 KiB
15Accepted4/41ms404 KiB
16Accepted4/41ms316 KiB
17Accepted4/41ms316 KiB