69712023-12-22 23:20:11MagyarKendeSZLGAutókódoláscpp17Accepted 50/503ms3596 KiB
#include <iostream>
#include <bitset>

using namespace std;
using ll = long long;

#define speed cin.tie(0); ios::sync_with_stdio(0)

int count_set(ll n) {
	int c = 0;
	while (n) {
		n = n & (n - 1);
		c++;
	}
	return c;
}

void unset_until(ll& n, int pos) {
	for (int i = 0; i < pos; i++) n >>= 1;
	for (int i = 0; i < pos; i++) n <<= 1;
}

int main()
{
	speed;
	
	ll M, N, O;
	cin >> N;

	M = O = N;
	
	if (M & 1) {
		int pos = 1;
		while ((M >> pos) & 1) pos++;
		
		if (pos == count_set(M)) { // all ones
			M = -1; // impossible
		}
		else {
			int zero_c = pos;
			while (!((M >> zero_c) & 1)) zero_c++;
			zero_c -= pos;
			// cout << bitset<sizeof(ll) * 8>(((1LL << (pos + 1)) - 1) << (zero_c - 1)) << '\n';
			unset_until(M, pos + zero_c + 1);
			// cout << bitset<sizeof(ll) * 8 >(M) << '\n';
			M |= ((1LL << (pos + 1)) - 1) << (zero_c - 1);
		}
	}
	else {
		int pos = 1;
		while (!((M >> pos) & 1)) pos++; // get the position of the first one
		M ^= ((1LL << pos) | (1LL << (pos - 1))); // unset the one and set the zero right to it
	}

	int pos = 0;
	while (!((O >> pos) & 1) || ((O >> (pos + 1)) & 1)) pos++;

	O ^= (1LL << pos) | (1LL << (pos + 1));
	
	int zero_c = 0;
	while (!((O >> zero_c) & 1) && zero_c < pos) zero_c++;

	int one_c = zero_c;
	while (((O >> one_c) & 1) && one_c < pos) one_c++;
	one_c -= zero_c;

	// cout << pos << ' ' << zero_c << ' ' << one_c << '\n';

	// cout << bitset<sizeof(ll) * 8 >(N) << '\n';
	// cout << bitset<sizeof(ll) * 8>((1LL << one_c) - 1) << '\n';
	unset_until(O, zero_c + one_c);
	// cout << bitset<sizeof(ll) * 8 >(O) << '\n';

	O |= (1LL << one_c) - 1;

	cout << M << '\n' << O;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1760 KiB
2Accepted0/03ms2000 KiB
3Accepted2/23ms2284 KiB
4Accepted2/23ms2520 KiB
5Accepted2/23ms2644 KiB
6Accepted2/23ms2748 KiB
7Accepted2/23ms2796 KiB
8Accepted4/43ms2844 KiB
9Accepted4/43ms3072 KiB
10Accepted4/43ms3068 KiB
11Accepted4/43ms3152 KiB
12Accepted4/43ms3156 KiB
13Accepted4/43ms3296 KiB
14Accepted4/43ms3284 KiB
15Accepted4/43ms3492 KiB
16Accepted4/43ms3592 KiB
17Accepted4/43ms3596 KiB