3373 2023. 02. 27 11:34:17 CWM Autókódolás cpp17 Elfogadva 50/50 3ms 3616 KiB
// 0227.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
string ConvertToBits(ll a) {
	ll divisor = pow(2, 41);
	string s = "";
	for (size_t i = 0; i < 42; i++)
	{
		if (a - divisor >= 0) {
			s += '1';
			a -= divisor;
		}
		else s += '0';
		divisor/=2;
	}
	return s;
}
string FindNext(string cur) {
	int oc = 0;
	bool hadOne = false;
	for (int i = 41; i >= 0; i--)
	{
		if (cur[i] == '0' && hadOne) {
			cur[i] = '1';
			for (int j = i+1; j < 42; j++)
			{
				if (j > 42-oc) cur[j] = '1';
				else cur[j] = '0';
			}
			return cur;
		}
		else if (cur[i] == '1') {
			hadOne = true;
			oc++;
		}
	}
}
string FindPrevious(string cur) {
	int oc = 0;
	for (int i = 41; i > 0; i--)
	{
		if (cur[i] == '0' && cur[i - 1] == '1') {
			cur[i] = '1';
			cur[i - 1] = '0';
			for (size_t j = i+1; j <= i+oc; j++)
			{
				cur[j] = '1';
			}
			return cur;
		}
		else if(cur[i]=='1') {
			oc++;
			cur[i] = '0';
		}
	}
	return "-1";
}
ll Reconvert(string cur) {
	if (cur == "-1") return -1;
	ll val = 0;
	ll addition = 1;
	for (int i = 41; i >= 0; i--)
	{
		if (cur[i] == '1') val += addition;
		addition *= 2;
	}
	return val;
}
int main()
{
	ll num;
	cin >> num;
	string c = ConvertToBits(num);
	string n = FindNext(c);
	string p = FindPrevious(c);
	ll nI = Reconvert(n);
	ll pI = Reconvert(p);
	cout << pI << "\n" << nI;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Részfeladat Összpont Teszt Verdikt Idő Memória
base 50/50
1 Elfogadva 0/0 3ms 1812 KiB
2 Elfogadva 0/0 3ms 2060 KiB
3 Elfogadva 2/2 3ms 2272 KiB
4 Elfogadva 2/2 3ms 2504 KiB
5 Elfogadva 2/2 3ms 3000 KiB
6 Elfogadva 2/2 2ms 2828 KiB
7 Elfogadva 2/2 3ms 2992 KiB
8 Elfogadva 4/4 3ms 3204 KiB
9 Elfogadva 4/4 3ms 3288 KiB
10 Elfogadva 4/4 2ms 3292 KiB
11 Elfogadva 4/4 2ms 3168 KiB
12 Elfogadva 4/4 3ms 3284 KiB
13 Elfogadva 4/4 3ms 3284 KiB
14 Elfogadva 4/4 3ms 3408 KiB
15 Elfogadva 4/4 3ms 3448 KiB
16 Elfogadva 4/4 3ms 3536 KiB
17 Elfogadva 4/4 3ms 3616 KiB