题意:
给出N,B,D,要求输出N个十进制数字,他们之间的Hamming距离在长度为B位的时候都等于D。
思路:
感觉图论就是一个,允许我们记忆化的边权好工具!(废话用edges存边了还不是当然的)
代码:
/* ID :ggy_7781 TASK :hamming LANG :C++11 */ #include <bits/stdc++.h> #define maxB 8 #define maxN 64 #define maxD 7 using namespace std; int N,B,D; int maxn; int getDist(int x,int y) { int tmp = (x^y); int cnt = 0; while(tmp) { cnt += (tmp&1); tmp = tmp >> 1; } return cnt; } int one[256][256]; int main(void) { freopen("hamming.in","r",stdin); freopen("hamming.out","w",stdout); cin>>N>>B>>D; maxn = pow(2,B) ; for(int i = 0;i < maxn;i ++) { for(int j = 0;j <maxn;j ++) { one[i][j] = getDist(i,j); } } int pos = 0; vector<int> ret; ret.push_back(pos); while(ret.size() < N) { for(int i =pos + 1 ;i <maxn;i ++) { bool ok = true; for(int j = 0;j <ret.size();j ++) { if(one[ret[j]][i] < D) { ok = false; break; } } if(ok) { ret.push_back(i); pos = i; break; } } } int cnt = 0; bool bgn = true; bool edd = false; for(int i =0 ;i <N;i ++){ cnt ++; if(bgn) { bgn = false; edd = false; }else cout<<" "; cout<<ret[i]; if(cnt == 10) { bgn = true; edd = true; cnt = 0; cout<<endl; } } if(!edd) cout<<endl; return 0; }