cf 1174 D Ehab and the Expected XOR Problem

cf 1174 D Ehab and the Expected XOR Problem

题意

在1~\(2^n\)范围内找到一个最长的序列,使得该序列的每一个子串异或后不等于0和x

题解

假设该序列为a,那么前缀异或和b[i] = a[i]^a[i-1]^...^a[0],如果b之间异或都不会等于0和x,那么a之间也不会。

#include <cstdio>
#include <cstring>

int main() {
    int n, x;
    while(~scanf("%d %d", &n, &x)) {
        n = 1 << n;
        int ans[300000], cnt = 0;
        bool vis[300000];
        memset(vis, false, sizeof(vis));
        vis[x] = true;
        for(int i = 1; i < n; i++) {
            if(vis[i] == false){
                ans[cnt++] = i;
                vis[i] = vis[i^x] = true;
            }
        }
        printf("%d\n", cnt);
        if(cnt) printf("%d ", ans[0]);
        for(int i = 1; i < cnt; i++) {
            printf("%d ", ans[i] ^ ans[i-1]);
        }
        if(cnt) printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fanshhh/p/11406456.html

时间: 2024-08-30 13:26:08

cf 1174 D Ehab and the Expected XOR Problem的相关文章

Codeforces Round #563 (Div. 2) D、Ehab and the Expected XOR Problem

D. Ehab and the Expected XOR Problem Given two integers n and x, construct an array that satisfies the following conditions: for any element ai in the array, 1≤ai<2^n there is no non-empty subsegment with bitwise XOR equal to 0 or x, its length l sho

CF D. Ehab and the Expected XOR Problem 贪心+位运算

code: #include <bits/stdc++.h> #define N 1000000 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int vis[N],b[N]; void solve() { int n,m,i,j,cur=1,cnt=0; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&a

CF1174D Ehab and the Expected XOR Problem - 构造

题面 Given two integers \(n\) and \(x\), construct an array that satisfies the following conditions: ·for any element ai in the array, \(1≤ai<2^n\); ·there is no non-empty subsegment with bitwise XOR equal to \(0\) or \(x\), ·its length \(l\) should be

CF1174E Ehab and the Expected GCD Problem(DP,数论)

题目大意:对于一个序列,定义它的价值是它的所有前缀和的 $\gcd$ 中互不相同的数的个数.给定整数 $n$,问在 $1$ 到 $n$ 的排列中,有多少个排列的价值达到最大值.答案对 $10^9+7$ 取模. $2\le n\le 10^6$. 一道 Div. 2 的难度 2500 的题,真的不是吹的…… 首先考虑排列的第一个数 .假如分解质因子后为 $\prod p_i^{c_i}$,那么此时排列价值的最大值为 $\sum c_i$. 为什么?因为如果 $\gcd$ 变了,那么一定变成原来 $

CodeForces 1325E - Ehab&#39;s REAL Number Theory Problem【质因子+】

题意: ??给定一个数组 \(a\) ,数组中任意一个元素的因子数不超过 \(7\) ,找出一个最短的子序列,满足该子序列之积为完全平方数.输出其长度. 数据范围:\(1≤n≤10^5,1≤a_i≤10^6\) 分析: ??首先,对于数组中的每个元素,如果其因子中包含有一个完全平方数,那么可以把该完全平方数除去,不影响最后的结果. ??然后,可以发现,当一个数的因子个数 \(\leq 7\) 时,其包含的质因子个数 \(\leq 2\).(如果有3个质因子,那么至少有 \(8\) 个因子)当我们

cf round 482D Kuro and GCD and XOR and SUM

题意: 开始有个空集合,现在有两种操作: $(1,x)$:给集合加一个数$x$,$x \leq 10^5$; $(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gcd(a,x)$:现在需要找满足条件的$a$,它异或$x$的值最大.$x,k,s \leq 10^5$ 操作数$q \leq 10^5$ 这道题就是看你想到一个算法有没有去算算实际复杂度 我们发现,对于所有在$[1,10^5]$的$i$,$10^5$之内的$i$的倍数的个数和,并不是很大,只有$2*1

cfE. Ehab and a component choosing problem(贪心)

题意 题目链接 给出一棵树,每个节点有权值,选出\(k\)个联通块,最大化 \[\frac{\sum_{i \in S} a_i}{k}\] Sol 结论:选出的\(k\)个联通块的大小是一样的且都等于最大联通块的大小 证明:因为我们是在保证分数最大的情况下才去最大化\(k\),一个很经典的结论是单独选择一个权值最大的联通块得到的分数一定是最大的,然后我们这时我们才去考虑最大化\(k\) 那么思路就很清晰了,先一遍dfs dp出最大联通块,然后再一遍dfs从下往上删就行了 #include<bi

cf1088E Ehab and a component choosing problem (树形dp)

题意(考试时看错了对着样例wa了好久..):从树上选k个连通块,使得权值的平均值最大的基础上,选的块数最多 如果不考虑块数最多的限制,肯定是只选一个权值最大的块是最好的 然后只要看这个权值最大的块有多少个不相交的就可以了 做法就是,在dp的时候,一旦找到了和最大权值相等的块,直接统计答案,然后把这一块的权值改成-inf 1 #include<bits/stdc++.h> 2 #define pa pair<int,int> 3 #define CLR(a,x) memset(a,x

Codeforces Round #525 E - Ehab and a component choosing problem

题目大意: 在一棵树中 选出k个联通块 使得 这k个联通块的点权总和 / k 最大 并且这k个联通块不相互覆盖(即一个点只能属于一个联通块) 如果有多种方案,找到k最大的那种 给定n 有n个点 给定n个点的点权(点权可能出现负数) 给定这个树的n-1条边 当将所有点分成联通块后,比较各个强联通块的点权总和,绝对存在最大值,而点权总和=最大值的也可能有多个 此时 若选择了所有点权总和等于最大值的联通块,那么 /k 之后得到的 ans=这个最大值 假设继续选择次大值,那么此时 res = (ans*