HDU 4876 ZCC loves cards (brute force pruning), hduzcc
HDU 4876 ZCC loves cards
Question Link
Given some cards with numbers on each card, select k cards and wrap them into a ring. You can choose 1-k cards on the ring each time, obtain the number of their exclusive or sum. Given an L, ask the maximum value of R when [L, R] is composed of all numbers.
Idea: brute-force C (20, 6), and then simulate the calculated value after each sequence is deprecated to the full row. However, there must be a pruning before the whole row, first, Random Number of k numbers (that is, do not need to be continuous). If this still does not meet the requirements, then the continuous conditions will certainly not meet the requirements and will end directly without entering the full row. As a result, the situation that cannot be met actually accounts for the vast majority, so the overall time complexity is not very high, and because the question is random data, it can still be passed.
Code:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n, k, l, r, a[25], save[25], have[25], v[205], Max, vis[205];void calmax(int num, int sum) { vis[sum] = 1; if (num == k) return; calmax(num + 1, sum ^ save[num]); calmax(num + 1, sum);}bool Maxcal() { memset(vis, 0, sizeof(vis)); calmax(0, 0); for (int i = l; i <= r; i++)if (!vis[i]) return false; return true;}void cal() { if (!Maxcal()) return; for (int i = 0; i < k; i++)have[i] = save[i]; do {memset(v, 0, sizeof(v));for (int i = 0; i < k; i++) { int ans = 0; for (int j = i; j < k + i; j++) {ans ^= have[(j % k)];v[ans] = 1; }}for (int i = l; i <= l + k * k; i++) if (!v[i]) {r = max(r, i - 1);break; } } while(next_permutation(have + 1, have + k));}void dfs(int now, int num) { if (num == k) {cal();return; } for (int i = now; i < n; i++) {save[num] = a[i];dfs(i + 1, num + 1); }}int main() { while (~scanf("%d%d%d", &n, &k, &l)) {for (int i = 0; i < n; i++) scanf("%d", &a[i]);sort(a, a + n);r = l - 1;dfs(0, 0);if (r < l) printf("0\n");else printf("%d\n", r); } return 0;}