前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >noi.ac#309 Mas的童年(子集乱搞)

noi.ac#309 Mas的童年(子集乱搞)

作者头像
attack
发布2019-04-01 14:56:51
4620
发布2019-04-01 14:56:51
举报

题意

题目链接

Sol

记\(s_i\)表示前\(i\)个数的前缀异或和,我们每次相当于要找一个\(j\)满足\(0 < j < i\)且\((s_i \oplus s_j) + s_j\)最大

然后下面的就和标算相差十万八千里了。

\[ \begin{aligned} &(s_i \oplus s_j) + s_j\\ =&(s_i \oplus s_j \oplus s_j) + ((s_i \oplus s_j) \& s_j )\\ =&(s_i + (\text{~}s_i \& s_j)) \end{aligned} \]

也就是对于每个\(i\),我们要在前面找一个\(j\)使得\(\text{~}s[i] \& s[j]\)最大

然后这里暴力处理子集就行了(一开始还想了半天trie树)。

加一个记忆化可以保证复杂度

最后复杂度为\(O(2^{20} + n \log{a_i})\)

代码语言:javascript
复制
#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 3e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, a[MAXN], s[MAXN];
bool mark[MAXN];
void insert(int x) {
    //if(mark[x]) return ;
    mark[x] = 1;
    for(int i = 0; i < 20; i++)
        if((x >> i & 1) && (!mark[x ^ (1 << i)]))
            insert(x ^ (1 << i));
}
int Query(int x) {
    int ans = 0;
    for(int i = 19; ~i; i--) 
        if((x >> i & 1) && mark[ans | 1 << i])
            ans |= 1 << i;
    return ans;
}
signed main() {
    //freopen("ex_childhood2.in", "r", stdin);
    N = read();
    for(int i = 1; i <= N; i++) a[i] = read(), s[i] = s[i - 1] ^ a[i];
    for(int i = 1; i <= N; i++) {
    //  for(int j = i - 1; j >= 0; j--) chmax(ans, (s[i] ^ s[j]) + s[j]);
        //for(int j = i - 1; j >= 0; j--) chmax(ans, (~s[i]) & s[j]);
        int ans = Query(~s[i]);
        cout << s[i] + ans * 2 << ' '; 
        insert(s[i]);
    }
    puts("");
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档