USACO 2.1 Hamming Codes

#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

int main() {
ifstream fin("hamming.in");
ofstream fout("hamming.out");

int N, B, D;
fin >> N >> B >> D;
vector<int> result;
result.push_back(0);
int count = 1;
for (int i = 1; count < N; ++i) {
if (count == N) break;

bool ok(true);
for (auto &item : result) {
bitset<64> diff(i ^ item);
if (diff.count() < D) {
ok = false;
break;
}
}

if (ok) {
++count;
result.push_back(i);
}
}

count = 0;
for (int i = 0; i < result.size(); ++i) {
count++;
fout << result[i];
if ((count != 0 && count % 10 == 0) || count == result.size())
fout << endl;
else
fout << " ";
}

return 0;
}