HDU 4876 ZCC loves cards (brute force pruning), hduzcc

Source: Internet
Author: User

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;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.