2013-02-13 20:02:44Morris
[UVA] 12571 - Brother & Sisters!
Brother & Sisters!
Taman is excited to announce that he has learnt bitwise AND operation. His Big Sister Titly
has given him a sequence of non-negative integers
x1, x2,..., xn as key. To test that whether
Taman knows bitwise AND operation or not, Taman is asked to find maximum value among
all (
a AND xi) where
1Brother & Sisters! |
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
Note:
Expression
x AND y means applying the operation of bitwise AND to numbers x and y. This
operation exists in all modern programming languages, for example, in language C++ and
Java it is marked as "&".
Input
First line of input will contain the number of test cases, T![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
Output
For each query output a single integer, the maximum value of ( a AND xi) where 1![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
![$ le$](http://uva.onlinejudge.org/external/125/12571img1.png)
Sample Input
1 3 3 1 2 3 10 11 12
Sample Output
2 3 0
Problem Setter: Muhammed Hedayet
Alternate Solution: Kazi Rakibul Hossain
a < 230 是個關鍵,把 255 以下的值都找出來。
#include <stdio.h>
inline int readchar() {
const int N = 1048576;
static char buf[N];
static char *p = buf, *end = buf;
if(p == end) {
if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
p = buf;
}
return *p++;
}
inline int ReadInt(int *x) {
static char c, neg;
while((c = readchar()) < '-') {if(c == EOF) return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = readchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
int main() {
int t, n, q, i, x, a;
ReadInt(&t);
//scanf("%d", &t);
while(t--) {
ReadInt(&n), ReadInt(&q);
// scanf("%d %d", &n, &q);
int m[256] = {};
while(n--) {
ReadInt(&x);
//scanf("%d", &x);
m[x&0xff] = 1;
}
while(q--) {
ReadInt(&a);
//scanf("%d", &a);
x = 0;
for(i = 255; i > x; i--)
if(m[i] && (a&i) > x)
x = a&i;
printf("%d\n", x);
}
}
return 0;
}