217672026-01-13 20:31:17hunzombiAutókódoláscpp17Partially correct 46/502ms560 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

long long findHigh(long long n) {
    ll start = n;
    ll last_idx = 0;
    for (ll i = 1; i <= n; i *= 2) {
        if ((n & i)) last_idx = i;
    }
    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;
        }
    }

    if ((n & (last_idx * 2)) == 1) return -1;
    if (__builtin_popcount(n) != __builtin_popcount(start)) return -1;
    return (start == n) ? -1 : n;
}

ll findLow(ll n) {
    ll start = n;
    for (long long i=2; i <= n; i *= 2) {
        if (n & i && !(n & (i / 2))) {
            n ^= 3 * i / 2;
            break;
        }
    }
    return (n == start) ? -1 : n;
}

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