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,&m);
    vis[m]=1;
    for(i=1;i<=(1<<n);++i)
    {
        while(vis[cur]) ++cur;
        if((cur^b[cnt-1]) >= (1<<n)) break;
        b[++cnt]=cur;
        vis[cur^m]=1;
        ++cur;
    }
    printf("%d\n",cnt);
    for(i=0;i<cnt;++i) printf("%d ",b[i]^b[i+1]);
}
int main()
{
    int i,j,T;
    // setIO("input");
    solve();
    return 0;
}

  

原文地址:https://www.cnblogs.com/guangheli/p/11641816.html

时间: 2024-08-30 01:40:32

CF D. Ehab and the Expected XOR Problem 贪心+位运算的相关文章

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

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

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$ 变了,那么一定变成原来 $

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

【CF 459D】 Pashmak and Parmida&#39;s problem

[CF 459D] Pashmak and Parmida's problem 预处理+线段树求逆序对 新学了树状数组 很适合这题 来一发 代码如下: #include <iostream> #include <cstdio> #include <cstdlib> #include <map> #include <cstring> #define ll long long using namespace std; map <int,int&

CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x,k,s(<=1e5),求当前所有数中满足,k|v,x+v<=s,且\(x\oplus v\)最大的v. 做法好神啊.关于异或的问题有一种常见做法,就是利用01trie来查找在一堆数里面,那个数与x的异或值最大.这道题就是这个思路.如果去掉k必须整除v这个条件,那么就转化成了上一个问题(只不过有最大

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 1325F - Ehab&#39;s Last Theorem

首先: 无向图的dfs树无横边 这很显然,因为若有u->v 为横边,那么v早就从这条边过来找u了...矛盾 设 \(k = ceil( \sqrt{n} )\) 接下来跑dfs树,如果有回边使得环大于等于k,就输出环. 否则,说明一个问题:所有点的回边的数量都小于k(不然一定能找到环了,鸽巢原理-) 于是,可以不停自底向上地选取点为独立集点,同时把它的领边都标记为不能取为独立集. 结果一定至少有k个独立集点. 为什么? 因为前面说了,所有点的回边的数量都小于k,这样取点,每去一次点,最多标记k-