34772023-02-28 12:11:17DoraAutókódoláscsharpTime limit exceeded 38/50277ms24400 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace autokodolas_gyorsabb
{
    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)
            {
                count += (int)(n & 1);
                n >>= 1;
            }
            return count;
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base38/50
1Accepted0/027ms20604 KiB
2Time limit exceeded0/0261ms4468 KiB
3Accepted2/228ms21508 KiB
4Accepted2/227ms21448 KiB
5Accepted2/227ms21856 KiB
6Accepted2/227ms21976 KiB
7Accepted2/227ms22120 KiB
8Accepted4/427ms22052 KiB
9Accepted4/426ms22444 KiB
10Accepted4/426ms22896 KiB
11Accepted4/426ms22788 KiB
12Accepted4/427ms23048 KiB
13Time limit exceeded0/4275ms6840 KiB
14Accepted4/426ms23716 KiB
15Accepted4/441ms24400 KiB
16Time limit exceeded0/4232ms7384 KiB
17Time limit exceeded0/4277ms7632 KiB