214472026-01-13 09:09:16hunzombiAutókódoláscpp11Partially correct 46/501ms556 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()
{
    long long n;
    cin >> n;
    cout << findLow(n) << '\n' << findHigh(n) << '\n';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base46/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted2/21ms508 KiB
4Accepted2/21ms316 KiB
5Partially correct1/21ms316 KiB
6Partially correct1/21ms332 KiB
7Accepted2/21ms500 KiB
8Accepted4/41ms316 KiB
9Accepted4/41ms316 KiB
10Accepted4/41ms540 KiB
11Accepted4/41ms556 KiB
12Accepted4/41ms552 KiB
13Partially correct2/41ms316 KiB
14Accepted4/41ms316 KiB
15Accepted4/41ms324 KiB
16Accepted4/41ms316 KiB
17Accepted4/41ms508 KiB