HDU5119 - Happy Matt Friends

Posted RRRR_wys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5119 - Happy Matt Friends相关的知识,希望对你有一定的参考价值。

HDU5119 - Happy Matt Friends


做法:拆成两堆数,分别暴力出两组的所有异或值A,B,枚举A, 将B全部插入Trie树,通过枚举的数每一位的值,确定异或后构成的新树,然后在新树上统计比m大的值的个数即可。

#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
const int N = 1e6 + 7;
using namespace std;
int n, m, a[42], b[42], numa, numb;
ll ans = 0;
vector<int> va;
struct node{
    int ch[2], num;
    void init() {
        ch[0] = ch[1] = -1;
        num = 0;
    }
}T[N*20];
int cc, rt;
void ins(int x) {
    int now = rt;
    for(int i = 22; i >= 0; --i) {
        int t = !!(x&(1<<i));
        if(T[now].ch[t] == -1) {
            T[now].ch[t] = ++cc;
            T[cc].init();
        }
        ++T[now].num;
        now = T[now].ch[t];
    }
    ++T[now].num;
}
int cal(int x) {
    int now = rt, ff = 0, ans = 0;
    for(int i = 22; i >= 0; --i) {
        int t = !!(m&(1<<i));
        int f = !!(x&(1<<i));
        if(!t) {
            if(T[now].ch[1^f]!=-1) ans += T[T[now].ch[1^f]].num;
            now = T[now].ch[0^f];
        }
        else {
            now = T[now].ch[1^f];
        }
        if(now == -1) break;
    }
    if(now != -1) ans+=T[now].num;
    return ans;
}
int TT, CC = 0;
int main() {
    memset(T,0,sizeof(T));
    scanf("%d",&TT);
    while(TT--) {
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; ++i) scanf("%d",&a[i]);
        numa = n/2; numb = 0;
        for(int i = numa; i < n; ++i) b[numb++] = a[i];
        ans = 0;
        va.clear();
        for(int s = 0; s < (1<<numa); ++s) {
            int tmp = 0;
            for(int i = 0; i < numa; ++i) if(s&(1<<i)) tmp^=a[i];
            va.pb(tmp);
        }
        rt = cc = 0;
        rt = ++cc;
        T[rt].init();

        for(int s = 0; s < (1<<numb); ++s) {
            int tmp = 0;
            for(int i = 0; i < numb; ++i) if(s&(1<<i)) tmp^=b[i];
            ins(tmp);
        }

        for(int i = 0; i < va.size(); ++i) ans += 1LL*cal(va[i]);

        printf("Case #%d: %lld
",++CC,ans);

        for(int i = 1; i <= cc; ++i) T[i].init();
    }
}

以上是关于HDU5119 - Happy Matt Friends的主要内容,如果未能解决你的问题,请参考以下文章

HDU5119 - Happy Matt Friends

Happy Matt Friends(HDU5119 + dp)

HDU 5119 Happy Matt Friends ——(背包DP)

HDU-5119 Happy Matt Friends (背包DP,递推枚举)

hdu 5119

Happy Matt Friends(DP)