#include <iostream> #include <vector> using namespace std; int bity(int x) { int i = 0; while (x > 0) { if (x%2) i++; x /= 2; } return i; } int inna(int a, int b, int bitA, int bitB) { int tmp=b+1, tmpBit=bity(tmp); while (tmp < a && tmpBit != bitA + bitB) { tmp++; tmpBit = bity(tmp); } if (tmpBit == bitA + bitB && tmp!=a) return tmp; else return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, tmp1=0; cin >> n; vector<pair<int, int> > ciag; int x = 1; while (tmp1 < n) { int r = bity(x); if (r <= n - tmp1) { ciag.push_back(make_pair(x, r)); tmp1 += r; } x++; } for (int i = ciag.size() - 1; i > 0; i--) { //cout << "D: " << ciag[i].first << endl; for (int j = i - 1; j >= 0; j--) { int x = inna(ciag[i].first, ciag[i - 1].first, ciag[i].second, ciag[j].second); if (x != -1) { ciag[i] = make_pair(x, ciag[i].second + ciag[j].second); ciag.erase(ciag.begin() + j); //cout << "D\n"; i--; } } } cout << ciag.size()<<endl; for (int i = ciag.size() - 1; i >= 0; i--) printf("%i ", ciag[i].first); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <iostream> #include <vector> using namespace std; int bity(int x) { int i = 0; while (x > 0) { if (x%2) i++; x /= 2; } return i; } int inna(int a, int b, int bitA, int bitB) { int tmp=b+1, tmpBit=bity(tmp); while (tmp < a && tmpBit != bitA + bitB) { tmp++; tmpBit = bity(tmp); } if (tmpBit == bitA + bitB && tmp!=a) return tmp; else return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, tmp1=0; cin >> n; vector<pair<int, int> > ciag; int x = 1; while (tmp1 < n) { int r = bity(x); if (r <= n - tmp1) { ciag.push_back(make_pair(x, r)); tmp1 += r; } x++; } for (int i = ciag.size() - 1; i > 0; i--) { //cout << "D: " << ciag[i].first << endl; for (int j = i - 1; j >= 0; j--) { int x = inna(ciag[i].first, ciag[i - 1].first, ciag[i].second, ciag[j].second); if (x != -1) { ciag[i] = make_pair(x, ciag[i].second + ciag[j].second); ciag.erase(ciag.begin() + j); //cout << "D\n"; i--; } } } cout << ciag.size()<<endl; for (int i = ciag.size() - 1; i >= 0; i--) printf("%i ", ciag[i].first); return 0; } |