128262025-01-01 23:22:23BucsMateAutókódoláscpp17Partially correct 48/501ms508 KiB
#include <iostream>

using namespace std;

long long calc_lower(long long N)
{
    int bitRep[100] = {};
    long long mask = 1;
    for(int i = 63; i >= 0; i--){
        if(N & mask){
            bitRep[i] = 1;
        }
        mask *= 2;
    }
    bool valid = false;
    for(int i = 63; i > 0; i--){
        if(bitRep[i] == 0 && bitRep[i-1] == 1){
            bitRep[i] = 1;
            bitRep[i-1] = 0;
            valid = true;
            break;
        }
    }
    if(!valid){
        return -1;
    }
    long long sol = 0, multiplier = 1;
    for(int i = 0; i <= 63; i++){
        sol += bitRep[63-i] * multiplier;
        multiplier *= 2;
    }
    return sol;
}

long long calc_higher(long long N)
{
    int bitRep[100] = {};
    long long mask = 1;
    for(int i = 63; i >= 0; i--){
        if(N & mask){
            bitRep[i] = 1;
        }
        mask *= 2;
    }
    int nrOne = 0, index = 0;

    for(int i = 63; i > 0; i--){
        if(bitRep[i] == 1){
            nrOne++;
        }
        if(bitRep[i] == 1 && bitRep[i-1] == 0){
            bitRep[i] = 0;
            bitRep[i-1] = 1;
            index = i;
            break;
        }
    }
    nrOne--;
    for(int i = index; i <= 63; i++){
        bitRep[i] = 0;
    }
    for(int i = 63; 63-i < nrOne; i--){
        bitRep[i] = 1;
    }

    long long sol = 0, multiplier = 1;
    for(int i = 0; i <= 63; i++){
        sol += bitRep[63-i] * multiplier;
        multiplier *= 2;
    }
    return sol;
}

int main()
{
    long long N;
    cin >> N;
    cout << calc_lower(N) << endl;
    cout << calc_higher(N) << endl;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base48/50
1Accepted0/01ms320 KiB
2Accepted0/01ms320 KiB
3Accepted2/21ms320 KiB
4Accepted2/21ms320 KiB
5Partially correct1/21ms320 KiB
6Partially correct1/21ms320 KiB
7Accepted2/21ms320 KiB
8Accepted4/41ms508 KiB
9Accepted4/41ms372 KiB
10Accepted4/41ms320 KiB
11Accepted4/41ms320 KiB
12Accepted4/41ms320 KiB
13Accepted4/41ms320 KiB
14Accepted4/41ms500 KiB
15Accepted4/41ms320 KiB
16Accepted4/41ms320 KiB
17Accepted4/41ms508 KiB