#include #include #define n 1000000 extern void registerme ( const char * ); extern int readbit ( int, int ); extern void checksolution ( int ); int findmissing ( ) { int *IDX, *BIT, x, R, bitpos, n0, n1, i, j; /* Invariance: One element in the interval [x, x+R] is missing. Initially, nothing is known about x, and the search interval is the entire of [0, n]. */ x = 0; R = n; /* IDX[] stores the indices of elements in A[] that lie in the search interval [x, x+R] */ IDX = (int *)malloc(n * sizeof(int)); /* Initially no elements of A[] are eliminated */ for (i=0; i= (1 << bitpos)) ++bitpos; /* Repeat from most significant to least significant bit positions */ while (--bitpos >= 0) { /* Count of integers in [x, x+R] with bitpos-th position storing bit 0 */ n0 = (1 << bitpos); /* Count of integers in [x, x+R] with bitpos-th position storing bit 1 */ n1 = R - (1 << bitpos) + 1; if (n1 < 0) continue; /* Switch to next bit position */ for (i=0; i