当前位置: 移动技术网 > IT编程>开发语言>C/C++ > noi.ac#309 Mas的童年(子集乱搞)

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

2019年03月31日  | 移动技术网IT编程  | 我要评论

沈阳市天气预报,南丫39死海难案,雅鹿羽绒服怎么样

题意

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})\)

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网