Submission #4061159


Source Code Expand

#include <stdio.h>
#include <stdlib.h>

#define MAX_N (100000)

int main(int argc, char *argv[]) {
  // read inputs
  int N, as[MAX_N];
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &as[i]);
  }

  // solve
  int us[MAX_N] = {}, ans = 0;
  // all as are smaller than 2^30
  // and thus use only the first 30 bits
  for (int k = 29; k >= 0; k--) {
    const int mask1 = 1 << k;     // 0..010..0
    const int mask2 = mask1 - 1;  // 0..001..1
    // calculate XOR
    int cur_xor = 0;
    for (int i = 0; i < N; i++) {
      cur_xor ^= as[i];
    }
    // printf("k = %d, cur_xor = %#08x\n", k, cur_xor);

    // check if kth LSB is 1
    if (cur_xor & mask1) {
      // printf("k( = %d)th bit is set. attempt removal.\n", k);
      // we need to remove this 1 from the xor result
      // look for an unused a that can be used
      // the a we're looking for has the form,
      //     31   k    0
      //     *...*10...0
      int i0 = -1;
      for (int i = 0; i < N; i++) {
        const int a = as[i];
        if (!us[i] && (a & mask1) != 0 && (a & mask2) == 0) {
          i0 = i;
          break;
        }
      }

      if (i0 >= 0) {  // found
        // printf("i = %d, as[i] = %#08x\n", i0, as[i0]);
        // if found subtract 1 from it and mark as used
        as[i0]--;
        us[i0] = 1;
        ans++;
      } else {  // not found
        // printf("could not remove the k( = %d)th bit. impossible.\n", k);
        // not possible
        ans = -1;
        break;
      }
    }
  }
  printf("%d\n", ans);

  return 0;
}

Submission Info

Submission Time
Task C - Cheating Nim
User khir
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1622 Byte
Status AC
Exec Time 14 ms
Memory 896 KB

Compile Error

./Main.cpp: In function ‘int main(int, char**)’:
./Main.cpp:9:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &N);
                  ^
./Main.cpp:11:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &as[i]);
                        ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 26
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 1 ms 512 KB
001.txt AC 1 ms 512 KB
002.txt AC 14 ms 896 KB
003.txt AC 6 ms 768 KB
004.txt AC 6 ms 768 KB
005.txt AC 4 ms 640 KB
006.txt AC 14 ms 896 KB
007.txt AC 14 ms 896 KB
008.txt AC 13 ms 896 KB
009.txt AC 13 ms 896 KB
010.txt AC 13 ms 896 KB
011.txt AC 14 ms 896 KB
012.txt AC 13 ms 896 KB
013.txt AC 14 ms 896 KB
014.txt AC 13 ms 896 KB
015.txt AC 13 ms 896 KB
016.txt AC 13 ms 896 KB
017.txt AC 13 ms 896 KB
018.txt AC 13 ms 896 KB
019.txt AC 13 ms 896 KB
020.txt AC 13 ms 896 KB
021.txt AC 2 ms 640 KB
022.txt AC 2 ms 640 KB
023.txt AC 13 ms 896 KB
example0.txt AC 1 ms 512 KB
example1.txt AC 1 ms 512 KB