34752023-02-28 11:56:24DoraAutókódoláscsharpTime limit exceeded 34/50300ms23368 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace autokodolas
{
    internal class Program
    {
        static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());

            // Count the number of 1 bits in the binary representation of n
            int oneBits = CountOneBits(n);

            // Find the previous code with the same number of 1 bits
            long prevCode = n - 1;
            while (prevCode >= 1 && CountOneBits(prevCode) != oneBits)
            {
                prevCode--;
            }

            // Find the next code with the same number of 1 bits
            long nextCode = n + 1;
            while (CountOneBits(nextCode) != oneBits)
            {
                nextCode++;
            }

            Console.WriteLine(prevCode >= 1 ? prevCode.ToString() : "-1");
            Console.WriteLine(nextCode.ToString());
            Console.ReadKey();
        }

        static int CountOneBits(long n)
        {
            int count = 0;
            while (n > 0)
            {
                if (n % 2 == 1)
                {
                    count++;
                }
                n /= 2;
            }
            return count;
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base34/50
1Accepted0/026ms20188 KiB
2Time limit exceeded0/0248ms4036 KiB
3Accepted2/227ms21096 KiB
4Accepted2/227ms21420 KiB
5Accepted2/226ms21552 KiB
6Accepted2/228ms22244 KiB
7Accepted2/227ms22460 KiB
8Accepted4/427ms22388 KiB
9Accepted4/427ms22656 KiB
10Accepted4/427ms22808 KiB
11Accepted4/426ms22912 KiB
12Accepted4/426ms23048 KiB
13Time limit exceeded0/4300ms6384 KiB
14Accepted4/434ms23060 KiB
15Time limit exceeded0/4256ms23368 KiB
16Time limit exceeded0/4254ms6944 KiB
17Time limit exceeded0/4270ms7180 KiB