bitmasks constructive algorithms *1900

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, x = map(int, input().split())
pow2 = [1]
for _ in range(n):
    pow2.append(2 * pow2[-1])
pn = pow2[n]
if x >= pn:
    l = pn - 1
    ans = [i for i in range(1, pn)]
    for i in range(l - 1, 0, -1):
        ans[i] ^= ans[i - 1]
elif n == 1:
    l = 0
else:
    ok = [1] * pn
    ok[x] = 0
    if x ^ 1:
        ans = [1]
        ok[1], ok[1 ^ x] = 0, 0
    else:
        ans = [2]
        ok[2], ok[2 ^ x] = 0, 0
    i = 1
    while i < pn:
        if not ok[i]:
            i += 1
        else:
            ans.append(i)
            ok[i] = 0
            ok[i ^ x] = 0
    l = len(ans)
    for i in range(l - 1, 0, -1):
        ans[i] ^= ans[i - 1]
print(l)
if l:
    sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <iostream>
#include <bits/stdc++.h> // Standard Template Library
#include <math.h>
 
#define ll long long
#define ld long double
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define se second
#define vii vector<pii>
#define setBits(x) builtin_popcount(x)
#define F(a, i) for (int i = 0; i < a; i++)
#define FN(a, b, i) for (int i = a; i >= b; i--)
const ll M = 1e9 + 7;
const ll md = 998244353;
 
using namespace std;
// using namespace chrono;

// DEBUG FUNCTIONS 
#ifndef ONLINE_JUDGE
 
template<typename T>
void __p(T a) {
    cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
    cout<<"{";
    __p(a.first);
    cout<<",";
    __p(a.second);
    cout<<"}";
}
template<typename T>
void __p(std::vector<T> a) {
    cout<<"{";
    for(auto it=a.begin(); it<a.end(); it++)
        __p(*it),cout<<",}"[it+1==a.end()];
}
template<typename T>
void __p(std::set<T> a) {
    cout<<"{";
    for(auto it=a.begin(); it!=a.end();){
        __p(*it); 
        cout<<",}"[++it==a.end()];
    }
 
}
template<typename T>
void __p(std::multiset<T> a) {
    cout<<"{";
    for(auto it=a.begin(); it!=a.end();){
        __p(*it); 
        cout<<",}"[++it==a.end()];
    }
}
template<typename T, typename F>
void __p(std::map<T,F> a) {
    cout<<"{\n";
    for(auto it=a.begin(); it!=a.end();++it)
    {
        __p(it->first);
        cout << ": ";
        __p(it->second);
        cout<<"\n";
    }
    cout << "}\n";
}
 
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
    __p(a1);
    __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";
    __p(arg1);
    cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(;; i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<" | ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
 
 
// DEBUG FUNCTIONS END 

void solve(){
    ll n, x; cin>>n>>x;
    if(n==1 && x==1){
        cout<<0<<endl;
        return;
    }
    ll z = 1<<n;
    //trace(z);
    if(x>=z){
        cout<<z-1<<endl;
        ll m = z-1;
        vector<ll> v(m);
        vector<ll> sufxor(m);
        F(m,i) sufxor[i] = m-i;
        //trace(sufxor);
        v[m-1] = sufxor[m-1];
        for(int i=0; i<m-1; i++){
            v[i] = sufxor[i]^sufxor[i+1];
        }
        for(int i=0; i<m; i++) cout<<v[i]<<" ";
        return;
    }
    cout<<(z-2)/2<<endl;
    ll m = (z-2)/2;
    vector<ll> v(m);
    vector<ll> sufxor(m);
    ll cnt=0;
    vector<bool> str;
    ll ptr =1;
    for(int i=0; i<z; i++) str.push_back(true);
    while(cnt<m){
        if(str[ptr] && str[ptr^x] && ptr!=x){
            sufxor[cnt] = ptr;
            cnt++;
            str[ptr] = false;
            str[ptr^x] = false;
        }
        ptr++;
    }
    //trace(sufxor);
    v[m-1] = sufxor[m-1];
    for(int i=0; i<m-1; i++){
        v[i] = sufxor[i]^sufxor[i+1];
    }
    for(int i=0; i<m; i++) cout<<v[i]<<" ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 
    ll t = 1;// cin >> t;
    while(t--)solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people