HDU-tricks in BITs YY

Source: Internet
Author: User

The problem was solved by DP at the beginning, but it was wrong to think about it, because such bit operations could not obtain the optimal solution until the last computation, that is, there was no optimal sub-structure.

First, when a and B perform operations, we analyze every bit on the 64-bit. We know that the & and ^ operations always generate 0, so we can always make the 32-bit value equal to 0 for the two 64-bit numbers. If we calculate the number X and C, we will directly convert X & C, those bits whose values are already 0 will definitely remain 0, and the remaining bits are not 0 or 1. If the result contains more than 1, we will reverse C, if at least half of the total number is 0, then the number is more than six. If the number is less than 0, we are directly violent.

The Code is as follows:

#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef unsigned long long int Int64;int N;Int64 seq[10], ret;void dfs(Int64 statu, int deep){    if (deep == N) {        ret = min(statu, ret);        return;    }    if (deep == 0) {        dfs(seq[deep+1], deep+1);        dfs(~seq[deep+1], deep+1);    }    else {        dfs(statu & seq[deep+1], deep+1);        dfs(statu | seq[deep+1], deep+1);        dfs(statu ^ seq[deep+1], deep+1);        dfs(statu & (~seq[deep+1]), deep+1);        dfs(statu | (~seq[deep+1]), deep+1);        dfs(statu ^ (~seq[deep+1]), deep+1);    }}int main(){    int T, ca = 0;    Int64 x;    scanf("%d", &T);    while (T--) {        scanf("%d", &N);        if (N > 6) {            for (int i = 0; i < N; ++i) {                scanf("%I64u", &x);                }            printf("Case #%d: %I64u\n", ++ca, 0LLU);            continue;        }        ret = 1LLU<<63;        for (int i = 1; i <= N; ++i) {            scanf("%I64u", &seq[i]);            }        dfs(0, 0);        printf("Case #%d: %I64u\n", ++ca, ret);    }    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.