15292022-11-22 09:54:20TomaSajtAutókódoláscpp14Accepted 50/503ms3900 KiB
#include <bits/stdc++.h>
#define speed ios::sync_with_stdio(0);cin.tie(0)
using namespace std;
typedef unsigned long long ull;

int popcount(ull n) {
    int res = 0;
    for (int i = 0; i < 64; i++) res += (n >> i) & 1;
    return res;
}

ull ne(ull n) {
    ull p1 = n & -n;
    ull m = n + p1 - 1; // right of the first 1 is all 1s
    ull p0 = ~m & (m + 1); // position of first 0 after the first bunch of 1
    ull masked = n & (p0 - 1);
    int c = popcount(masked) - 1;
    ull res = n - masked + p0 + (1ull << c) - 1;
    return res;
}

int main() {
    speed;
    ull n; cin >> n;
    ull pr = ~ne(~n);
    if ((pr >> 63) & 1) cout << "-1";
    else cout << pr;
    cout << '\n' << ne(n);
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1888 KiB
2Accepted0/02ms2120 KiB
3Accepted2/22ms2316 KiB
4Accepted2/22ms2392 KiB
5Accepted2/22ms2372 KiB
6Accepted2/22ms2600 KiB
7Accepted2/22ms2740 KiB
8Accepted4/42ms2968 KiB
9Accepted4/42ms3036 KiB
10Accepted4/42ms3232 KiB
11Accepted4/42ms3440 KiB
12Accepted4/42ms3520 KiB
13Accepted4/42ms3728 KiB
14Accepted4/42ms3692 KiB
15Accepted4/42ms3692 KiB
16Accepted4/42ms3828 KiB
17Accepted4/42ms3900 KiB