给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值。
标签:style color led scanf out script bzoj problem desc
又是一道线性基,先求出一组线性基,这组线性基的异或和就是最大值,再异或一下其中最小的,就是最小值了。
1 #include <cstdio> 2 3 template <class T> 4 __inline void swap(T &a, T &b) 5 { 6 static T c; 7 c = a; 8 a = b; 9 b = c; 10 } 11 12 const int mxn = 100005; 13 14 int n, a[mxn], b[mxn], c; 15 16 signed main(void) 17 { 18 scanf("%d", &n); 19 20 for (int i = 1; i <= n; ++i) 21 scanf("%d", a + i); 22 23 for (int i = 1; i <= n; ++i) 24 { 25 for (int j = n; j > i; --j) 26 if (a[i] < a[j]) 27 swap(a[i], a[j]); 28 29 if (a[i]) 30 ++c; 31 else 32 break; 33 34 for (int j = 31; ~j; --j) 35 if ((a[i] >> j) & 1) 36 { 37 b[i] = j; 38 39 for (int k = 1; k <= n; ++k) 40 if (k != i && (a[k] >> j) & 1) 41 a[k] ^= a[i]; 42 43 break; 44 } 45 } 46 47 int ans = 0; 48 49 for (int i = 1; i <= c; ++i) 50 ans ^= a[i]; 51 52 printf("%d %d\n", ans, ans ^ a[c]); 53 }
@Author: YouSiki
标签:style color led scanf out script bzoj problem desc
原文地址:http://www.cnblogs.com/yousiki/p/6362502.html