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 maximized.

A sequence \(b\) is a subsegment of \(a\) sequence \(a\) if \(b\) can be obtained from \(a\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

题意

给两个数 \(n\) 和 \(x\),构造一个满足以下条件的序列:
·对任何序列中的元素 \(a_i\),1\leq a_i<2^n1≤a_i<2^n$
·序列中没有非空连续子序列异或和为 \(0\) 或 \(x\)
·序列长度 \(l\) 应该最大

思路

思路比较巧妙,因为元素可重复不太好搞,就考虑构造一个答案序列 \(a\) 的异或前缀和 \(b\),且 \(b\) 满足任意 \(b_i \ xor \ b_j \ \not= \ x\) 或 \(0\)。
因为若 \(a \ xor \ b \ = \ c\),则 \(a \ xor \ c \ = \ b\),所以从 \(1\) 枚举到 \(2^n-1\) ,每次用可行的 \(i\) 数加入答案并排除 \(i \ xor \ x\) 这个数。

代码

/************************************************
*Author        :  lrj124
*Created Time  :  2019.10.15.19:28
*Mail          :  [email protected]
*Problem       :  cf1174d
************************************************/
#include <cstdio>
const int maxn = 1<<18;
int n,x,ans[maxn];
bool vis[maxn];
int main() {
    //freopen("cf1174d.in","r",stdin);
    //freopen("cf1174d.out","w",stdout);
    scanf("%d%d",&n,&x);
    vis[0] = vis[x] = true;
    for (int i = 1;i < 1<<n;i++)
        if (!vis[i]) {
            vis[i^x] = true;
            ans[++ans[0]] = i;
        }
    printf("%d\n",ans[0]);
    for (int i = 1;i <= ans[0];i++) printf("%d ",ans[i]^(i ^ 1 ? ans[i-1] : 0));
    return 0;
}

原文地址:https://www.cnblogs.com/lrj124/p/11679998.html

时间: 2024-08-30 16:28:19

CF1174D 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 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("%

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

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\) 个因子)当我们

HDU 5402 Travelling Salesman Problem (构造)(好题)

大致题意:n*m的非负数矩阵,从(1,1) 只能向四面走,一直走到(n,m)为终点,路径的权就是数的和,输出一条权值最大的路径方案 思路:由于这是非负数,要是有负数就是神题了,要是n,m中有一个是奇数,显然可以遍历,要是有一个偶数,可以画图发现,把图染成二分图后,(1,1)为黑色,总能有一种构造方式可以只绕过任何一个白色的点,然后再遍历其他点,而绕过黑色的点必然还要绕过两个白色点才能遍历全部点,这是画图发现的,所以找一个权值最小的白色点绕过就可以了, 题解给出了证明: 如果n,mn,m都为偶数,

hdu 1757 A Simple Math Problem 构造矩阵

题意:函数f(x), 若 x < 10 f(x) = x. 若 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 且 ai(0<=i<=9) 仅为 0 或 1 . 给定k,m,求f(k)%m; 思路:求一个递推函数的函数值,显然是矩阵快速幂,矩阵构造方法如下: #include<cstdio> #include<cstring> #include<al

hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)

题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10) 同时ai(0<=i<=9) 不是 0 就是 1: 现在给你 ai 的数字,以及k和mod,请你算出 f(x)%mod 的结果是多少 思路:线性递推关系是组合计数中常用的一种递推关系,如果直接利用递推式,需要很长的时间才能计算得出,时间无法承受,但是现在我们已知

hdu 5402 Travelling Salesman Problem (构造)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给定N*M的矩阵,每一格子里面有一个非负整数,求从(1,1)到(n,m)这条路上的和,(每个格子只能走一次,求最大的和). 分析:官方题解当N为奇数或M为奇数时,可以遍历到所有格子.当N和M都为偶数的时候,那么讲棋盘黑白染色,假设 (1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中