Ejemplo n.º 1
0
        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput())
            {
                AutoFlush = false
            };

            new Program().Solve(ss, sw);
            sw.Flush();
        }
Ejemplo n.º 2
0
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var N = ss.Next(int.Parse);
            var K = ss.Next(long.Parse);
            var A = ss.Next(long.Parse, N);

            var memo = new long[50, 2];

            for (var i = 0; i < 50; i++)
            {
                memo[i, 0] = long.MinValue;
                memo[i, 1] = long.MinValue;
            }

            Func <int, bool, long> DFS = null;

            DFS = (digit, fever) =>
            {
                if (digit == -1)
                {
                    return(0);
                }
                if (memo[digit, fever ? 1 : 0] != long.MinValue)
                {
                    return(memo[digit, fever ? 1 : 0]);
                }

                var place    = 1L << digit;
                var popCount = A.Sum(a => (a >> digit) & 1);

                Func <bool, long> f0 = b => popCount * place + DFS(digit - 1, b);
                Func <bool, long> f1 = b => (N - popCount) * place + DFS(digit - 1, b);

                if (fever)
                {
                    return(memo[digit, 1] = Math.Max(f0(true), f1(true)));
                }
                if (((K >> digit) & 1) == 1)
                {
                    return(memo[digit, 0] = Math.Max(f0(true), f1(false)));
                }
                return(memo[digit, 0] = f0(false));
            };

            sw.WriteLine(DFS(40, false));
            //---------------------------------
        }
Ejemplo n.º 3
0
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            const int MAX = 50;

            var N = ss.Next(int.Parse);
            var K = ss.Next(long.Parse);
            var A = ss.Next(long.Parse, N);

            var pops = new long[MAX];

            foreach (var a in A)
            {
                for (var i = 0; i < MAX; i++)
                {
                    if (((a >> i) & 1) == 1)
                    {
                        pops[i]++;
                    }
                }
            }

            var memo = new long[MAX];

            for (var i = 0; i < MAX; i++)
            {
                var place = 1L << i;
                memo[i] += Math.Max(N - pops[i], pops[i]) * place;
                if (i > 0)
                {
                    memo[i] += memo[i - 1];
                }
            }

            var ans       = 0L;
            var current   = 0L;
            var kBase2    = Convert.ToString(K, 2);
            var kBase2Rev = (new string('0', MAX - kBase2.Length) + kBase2).Select(c => c - '0').Reverse().ToArray();

            for (var i = MAX - 1; i >= 0; i--)
            {
                var place = 1L << i;

                if (kBase2Rev[i] == 0)
                {
                    current += pops[i] * place;
                }
                else
                {
                    if (i > 0)
                    {
                        ans = Math.Max(ans, current + pops[i] * place + memo[i - 1]);
                    }
                    current += (N - pops[i]) * place;
                }
            }

            ans = Math.Max(ans, current);
            sw.WriteLine(ans);
            //---------------------------------
        }