35152023-02-28 14:48:21BenedekAutókódoláscsharpTime limit exceeded 38/50300ms23956 KiB
using System;

class Program
{
    static void Main(string[] args)
    {
        long n = long.Parse(Console.ReadLine());
        long prev = GetPreviousCode(n);
        long next = GetNextCode(n);

        Console.WriteLine(prev >= 0 ? prev.ToString() : "-1");
        Console.WriteLine(next >= 0 ? next.ToString() : "-1");
        Console.ReadKey();
    }

    static long GetPreviousCode(long n)
    {
        int bitCount = CountOnes(n);
        for (long i = n - 1; i >= 1; i--)
        {
            if (CountOnes(i) == bitCount) return i;
        }
        return -1;
    }

    static long GetNextCode(long n)
    {
        int bitCount = CountOnes(n);
        for (long i = n + 1; i <= long.MaxValue; i++)
        {
            if (CountOnes(i) == bitCount) return i;
        }
        return -1;
    }

    static int CountOnes(long n)
    {
        int count = 0;
        while (n > 0)
        {
            if ((n & 1) == 1) count++;
            n >>= 1;
        }
        return count;
    }
}
SubtaskSumTestVerdictTimeMemory
base38/50
1Accepted0/028ms20432 KiB
2Time limit exceeded0/0300ms4156 KiB
3Accepted2/227ms21216 KiB
4Accepted2/228ms21396 KiB
5Accepted2/226ms21972 KiB
6Accepted2/228ms22024 KiB
7Accepted2/228ms22320 KiB
8Accepted4/427ms22820 KiB
9Accepted4/427ms23216 KiB
10Accepted4/427ms23200 KiB
11Accepted4/428ms23684 KiB
12Accepted4/427ms23956 KiB
13Time limit exceeded0/4300ms7204 KiB
14Accepted4/429ms23936 KiB
15Accepted4/482ms23812 KiB
16Time limit exceeded0/4268ms7376 KiB
17Time limit exceeded0/4246ms8048 KiB