Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 828 Accepted Submission(s): 184
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <set> 7 #include <map> 8 #include <queue> 9 10 using namespace std; 11 12 #define read() freopen("sw.in", "r", stdin) 13 14 const int MAX_N = 25; 15 const int MAX = (1 << 7); 16 int N, K, L; 17 struct node { 18 int st[6]; 19 bool operator < (const node &rhs) const { 20 for (int i = 0; i < K; ++i) { 21 if (st[i] != rhs.st[i]) return st[i] < rhs.st[i]; 22 } 23 return 0; 24 } 25 }; 26 27 int x[MAX_N]; 28 bool vis[MAX]; 29 int ele[MAX_N]; 30 int _max; 31 int len = 0; 32 node p[720]; 33 34 35 int cal(int n) { 36 int ret = 0; 37 if (n == 0) ret = 1; 38 while (n > 0) { 39 ++ret; 40 n >>= 1; 41 } 42 43 return (1 << ret) - 1; 44 } 45 46 void check() { 47 int t = 0, _max1 = 0; 48 /*for (int i = 0; i < K; ++i) printf("%d ", ele[i]); 49 printf("\n");*/ 50 memset(vis, 0, sizeof(vis)); 51 int cnt = 0; 52 for (int s = 0; s < (1 << K); ++s) { 53 t = 0; 54 for (int i = 0; i < K; ++i) { 55 if (s >> i & 1) { 56 t ^= ele[i]; 57 } 58 } 59 if (!vis[t]) { 60 vis[t] = 1; 61 if (t >= L && t <= _max + 1) ++cnt; 62 } 63 64 _max1 = max(_max1, t); 65 } 66 67 if (_max1 <= _max || cnt < (_max + 2 - L)) return; 68 69 for (int i = 0; i < len; ++i) { 70 memset(vis, 0, sizeof(vis)); 71 cnt = 0; 72 for (int start = 0; start < K; ++start) { 73 int v; 74 for (int l = 1; l <= K; ++l) { 75 v = 0; 76 for (int pos = start, num = 1; num <= l; pos = (pos + 1) % K, ++num) 77 v ^= ele[ p[i].st[pos] ]; 78 if (!vis[ v ]) { 79 vis[v] = 1; 80 if (v >= L && v <= _max + 1) ++cnt; 81 } 82 } 83 84 } 85 int k = _max + 1; 86 if (cnt < (_max + 2 - L)) continue; 87 while (vis[k] == 1) ++k; 88 _max = max(_max, k - 1); 89 } 90 91 92 93 94 } 95 96 void dfs(int id, int num) { 97 if (num >= K) { 98 //printf("fuck\n"); 99 check(); 100 return ; 101 } 102 if (id >= N) return ; 103 104 if (num == 0 && cal(x[id]) <= _max) return; 105 ele[num] = x[id]; 106 dfs(id + 1, num + 1); 107 dfs(id + 1, num); 108 } 109 110 111 112 int main() 113 { 114 //read(); 115 while (scanf("%d%d%d", &N, &K, &L) != EOF) { 116 for (int i = 0; i < N; ++i) scanf("%d", &x[i]); 117 set <node> Set; 118 len = 0; 119 int id[6] = {0, 1, 2, 3, 4, 5}; 120 sort(x, x + N, greater<int>()); 121 _max = L - 1; 122 123 do { 124 node st; 125 for (int i = 0; i < K; ++i) st.st[i] = id[i]; 126 if (Set.find(st) != Set.end()) continue; 127 // for (int i = 0; i < K; ++i) printf("%d ", id[i]); 128 //printf("\n"); 129 for (int i = 0; i < K; ++i) p[len].st[i] = id[i]; 130 len++; 131 for (int i = 0; i < K; ++i) { 132 for (int m = 0, j = i; m < K; ++m, j = (j + 1) % K) { 133 st.st[m] = id[j]; 134 } 135 Set.insert(st); 136 } 137 138 }while (next_permutation(id, id + K)); 139 140 dfs(0, 0); 141 printf("%d\n", _max < L ? 0 : _max); 142 143 144 } 145 //cout << "Hello world!" << endl; 146 return 0; 147 }
原文:http://www.cnblogs.com/hyxsolitude/p/3867455.html