AT_arc138_d [ARC138D] Differ by K bits 题解

· · 题解

这个题直接暴力即可。

考虑存下所有的 popcount(x)=k 的值。

搜索即可。

#include <bits/stdc++.h>
#define f(c, a, b) for (c = a; c <= b; c++)
#define fd(c, a, b) for (c = b; c >= a; c--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define int long long
#define vpii vector<pi>
#define il inline
#define ri register
#define aint(a) a.begin(), a.end()
#define fr(a) freopen(a, "r", stdin)
#define fo(a) freopen(a, "w", stdout);
#define mod 998244353
#define debug puts("------------------------")
#define lowbit(x) (x & -x)
#define ls(x) x << 1
#define rs(x) x << 1 | 1
#define co const
using namespace std;
const int N = 2e5 + 3;
int n, m;//**//
il int read() {
    ri int ans = 0;
    ri char c = getchar();
    ri bool neg = 0;
    while ((c < '0') | (c > '9')) neg ^= !(c ^ '-'), c = getchar();
    while ((c >= '0') & (c <= '9')) ans = (ans << 3) + (ans << 1) + c - 48, c = getchar();
    return neg ? -ans : ans;
}
il void write(ri int x) {
    if (x < 0)
        x = -x, putchar('-');
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
il void writes(ri int x) {
    write(x);
    putchar(' ');
}
il void writed(ri int x) {
    write(x);
    putchar('\n');
}
int k;
vector<int>ans,c;
bitset<(1<<20)+2>v;
inline void dfs(const int x){
    ans.push_back(x);
    v[x]=1;
    if(ans.size()==(1<<n)){
        puts("1");
        for(auto x:ans) cout<<x<<" ";
        exit(0); 
    }
    for(register int b:c) if(!v[b^x]) dfs(b^x);
}
signed main() {
    n=read(),k=read();
    int i,j;
    for(register int i=0;i<(1<<n);++i) if(__builtin_popcount(i)==k) c.push_back(i);
        dfs(0);
    puts("No"); 
    return 0;
}