lowbit()函数其实就是一个数的二进制最低位的1代表的十进制数值
const int MAXN = 25; int lowbit(int n) { return n & -n; } int bin(int n) { int ret = 0; while (n) { n >>= 1; ret++; } return ret - 1; } vector<int> G[MAXN]; int main() { // freopen("in.txt", "r", stdin); int sum, limit; while (~RII(sum, limit)) { REP(i, MAXN) G[i].clear(); vector<int> ans; int s = 0; FE(i, 1, limit) { int t = bin(lowbit(i)); G[t].push_back(i); s += lowbit(i); } if (s < sum) puts("-1"); else { FED(i, 22, 0) { int val = (1 << i); int ct = min((int)G[i].size(), sum / val); REP(j, ct) ans.push_back(G[i][j]); sum -= ct * val; } WI(ans.size()); REP(i, ans.size()) cout << ans[i] << ' '; puts(""); } } return 0; }
Codeforces Round #250 (Div. 2)——The Child and Set,布布扣,bubuko.com
Codeforces Round #250 (Div. 2)——The Child and Set
原文:http://blog.csdn.net/wty__/article/details/28436541