#include <bits/stdc++.h> template <typename... Ts> void __print(Ts&&... ts) {} #ifdef DEBUG #include "print.hpp" #endif // DEBUG using namespace std; template <class T> using pq_max = priority_queue<T>; template <class T> using pq_min = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, b) for (int i = a; i < (b); ++i) #define F0R(i, a) for (int i = 0; i < (a); ++i) #define FORd(i, a, b) for (int i = (b)-1; i >= a; --i) #define F0Rd(i, a) for (int i = (a)-1; i >= 0; --i) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define all(x) x.begin(), x.end() #define GET_FOR(_1, _2, _3, NAME, ...) NAME #define loop(...) GET_FOR(__VA_ARGS__, FOR, F0R)(__VA_ARGS__) #define GET_FORd(_1, _2, _3, NAME, ...) NAME #define loopr(...) GET_FOR(__VA_ARGS__, FORd, F0Rd)(__VA_ARGS__) #define sz(x) ((int)(x).size()) using ll = long long; template <typename T> using vec = vector<T>; template <typename T, size_t N> using arr = array<T, N>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using pi = pair<int, int>; using pl = pair<ll, ll>; /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// class Solution {}; void solution() { int n; cin >> n; } bool last = 0; pi bit_pos_and_bits_taken(int n, int k = 0) { // if (n == 2) { // return pi{1, k + 1}; // } int curr = k; int r = 1; int p = 1; // cerr << "n: " << n << " k: " << k << " curr: " << curr << " r: " << r // << " p: " << p << endl; while (r * p + k * p * 2 < n) { curr = r * p + k * p * 2; p *= 2; r++; // cerr << "n: " << n << " k: " << k << " curr: " << curr << " r: " << r // << " p: " << p << endl; } // cerr << endl; return pi{r - 1, curr}; } pi smallest_and_bits_taken(int n) { int res = 0; int bits_res = 0; int bits_taken = 0; for (int k = 0; bits_taken < n; k++) { pi h = bit_pos_and_bits_taken(n - bits_taken, k); bits_taken += h.second; if (bits_taken >= n) { break; } res += 1 << h.first; // cerr << "res: " << res << endl << endl; bits_res++; if (h.first == 0) break; } // cerr << "res: " << res << " bits_res: " << bits_res << endl << endl; return pi{res, bits_res}; // return pi{res & 0xFFFFFFFE, bits_res}; } pi smallest_brute(int n) { for (int k = 1; k < 1000000; k++) { int h = __builtin_popcount(k); if (n <= h) { if (n == h) { n = 0; } return pi{k, n}; } n -= h; } return pi{n, 0}; } bool check(vi& res, int n) { int sum = 0; for (int a : res) { sum += __builtin_popcount(a); } return sum == n; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; // n = 2; vi res; int N = n; while (n > 0) { pi h = smallest_and_bits_taken(n); res.push_back(h.first); n -= h.second; } cout << res.size() << endl; for (int a : res) { cout << a << ' '; } // int bad = 0; // int N = 10000000; // constexpr int S = 16; // for (int i = 1; i <= N + 1; i++) { // auto [l, h] = smallest_brute(i); // int r = smallest_and_bits_taken(i).first; // if (l != r) { // cout << "i: " << bitset<S>(i) << " l: " << bitset<S>(l) // << " r: " << bitset<S>(r) << " " // << (bitset<S>(l) ^ bitset<S>(r)) << ' ' << h << ' ' << last // << endl; // bad++; // } // last = 0; // } // cout << "Bad: " << bad << "/" << N << endl; }
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include <bits/stdc++.h> template <typename... Ts> void __print(Ts&&... ts) {} #ifdef DEBUG #include "print.hpp" #endif // DEBUG using namespace std; template <class T> using pq_max = priority_queue<T>; template <class T> using pq_min = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, b) for (int i = a; i < (b); ++i) #define F0R(i, a) for (int i = 0; i < (a); ++i) #define FORd(i, a, b) for (int i = (b)-1; i >= a; --i) #define F0Rd(i, a) for (int i = (a)-1; i >= 0; --i) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define all(x) x.begin(), x.end() #define GET_FOR(_1, _2, _3, NAME, ...) NAME #define loop(...) GET_FOR(__VA_ARGS__, FOR, F0R)(__VA_ARGS__) #define GET_FORd(_1, _2, _3, NAME, ...) NAME #define loopr(...) GET_FOR(__VA_ARGS__, FORd, F0Rd)(__VA_ARGS__) #define sz(x) ((int)(x).size()) using ll = long long; template <typename T> using vec = vector<T>; template <typename T, size_t N> using arr = array<T, N>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using pi = pair<int, int>; using pl = pair<ll, ll>; /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// class Solution {}; void solution() { int n; cin >> n; } bool last = 0; pi bit_pos_and_bits_taken(int n, int k = 0) { // if (n == 2) { // return pi{1, k + 1}; // } int curr = k; int r = 1; int p = 1; // cerr << "n: " << n << " k: " << k << " curr: " << curr << " r: " << r // << " p: " << p << endl; while (r * p + k * p * 2 < n) { curr = r * p + k * p * 2; p *= 2; r++; // cerr << "n: " << n << " k: " << k << " curr: " << curr << " r: " << r // << " p: " << p << endl; } // cerr << endl; return pi{r - 1, curr}; } pi smallest_and_bits_taken(int n) { int res = 0; int bits_res = 0; int bits_taken = 0; for (int k = 0; bits_taken < n; k++) { pi h = bit_pos_and_bits_taken(n - bits_taken, k); bits_taken += h.second; if (bits_taken >= n) { break; } res += 1 << h.first; // cerr << "res: " << res << endl << endl; bits_res++; if (h.first == 0) break; } // cerr << "res: " << res << " bits_res: " << bits_res << endl << endl; return pi{res, bits_res}; // return pi{res & 0xFFFFFFFE, bits_res}; } pi smallest_brute(int n) { for (int k = 1; k < 1000000; k++) { int h = __builtin_popcount(k); if (n <= h) { if (n == h) { n = 0; } return pi{k, n}; } n -= h; } return pi{n, 0}; } bool check(vi& res, int n) { int sum = 0; for (int a : res) { sum += __builtin_popcount(a); } return sum == n; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; // n = 2; vi res; int N = n; while (n > 0) { pi h = smallest_and_bits_taken(n); res.push_back(h.first); n -= h.second; } cout << res.size() << endl; for (int a : res) { cout << a << ' '; } // int bad = 0; // int N = 10000000; // constexpr int S = 16; // for (int i = 1; i <= N + 1; i++) { // auto [l, h] = smallest_brute(i); // int r = smallest_and_bits_taken(i).first; // if (l != r) { // cout << "i: " << bitset<S>(i) << " l: " << bitset<S>(l) // << " r: " << bitset<S>(r) << " " // << (bitset<S>(l) ^ bitset<S>(r)) << ' ' << h << ' ' << last // << endl; // bad++; // } // last = 0; // } // cout << "Bad: " << bad << "/" << N << endl; } |