35132023-02-28 14:42:49BenedekAutókódoláscsharpTime limit exceeded 38/50284ms24120 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/027ms20304 KiB
2Time limit exceeded0/0280ms4384 KiB
3Accepted2/226ms21208 KiB
4Accepted2/227ms21432 KiB
5Accepted2/227ms21528 KiB
6Accepted2/226ms21728 KiB
7Accepted2/227ms22364 KiB
8Accepted4/426ms22696 KiB
9Accepted4/427ms23136 KiB
10Accepted4/426ms23632 KiB
11Accepted4/426ms23816 KiB
12Accepted4/426ms23556 KiB
13Time limit exceeded0/4284ms7132 KiB
14Accepted4/428ms24120 KiB
15Accepted4/483ms23928 KiB
16Time limit exceeded0/4284ms7632 KiB
17Time limit exceeded0/4266ms7404 KiB