#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <stdio.h> #include <iostream> #include <string.h> #include <math.h> #include <algorithm> #include <vector> #include <map> #include <stack> typedef std::stack<int> SI; typedef std::map<int,SI*,std::greater<>> MSI; constexpr int MAX_N = 1000005; int CountOnes(int number) { int result = 0; while (number > 0) { if (number % 2 == 1) result++; number /= 2; } return result; } int main() { int n; auto _ = scanf("%d\n", &n); int sumOnes = 0; int i = 0; MSI dic; while (sumOnes < n) { i++; auto ones = CountOnes(i); sumOnes += ones; if (dic.count(ones) <= 0) { dic[ones] = new SI(); } dic[ones]->push(i); } auto toRemove = sumOnes - n; for (auto kv : dic) { while (toRemove >= kv.first && !kv.second->empty()) { kv.second->pop(); toRemove -= kv.first; } } std::vector<int> results; for (auto kv : dic) { while (!kv.second->empty()) { results.push_back(kv.second->top()); kv.second->pop(); } } std::sort(results.begin(), results.end(), std::greater<>()); printf("%d\n", (int) results.size()); for (int i = 0; i < results.size(); i++) { if (i > 0) printf(" "); printf("%d",results[i]); } printf("\n"); 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <stdio.h> #include <iostream> #include <string.h> #include <math.h> #include <algorithm> #include <vector> #include <map> #include <stack> typedef std::stack<int> SI; typedef std::map<int,SI*,std::greater<>> MSI; constexpr int MAX_N = 1000005; int CountOnes(int number) { int result = 0; while (number > 0) { if (number % 2 == 1) result++; number /= 2; } return result; } int main() { int n; auto _ = scanf("%d\n", &n); int sumOnes = 0; int i = 0; MSI dic; while (sumOnes < n) { i++; auto ones = CountOnes(i); sumOnes += ones; if (dic.count(ones) <= 0) { dic[ones] = new SI(); } dic[ones]->push(i); } auto toRemove = sumOnes - n; for (auto kv : dic) { while (toRemove >= kv.first && !kv.second->empty()) { kv.second->pop(); toRemove -= kv.first; } } std::vector<int> results; for (auto kv : dic) { while (!kv.second->empty()) { results.push_back(kv.second->top()); kv.second->pop(); } } std::sort(results.begin(), results.end(), std::greater<>()); printf("%d\n", (int) results.size()); for (int i = 0; i < results.size(); i++) { if (i > 0) printf(" "); printf("%d",results[i]); } printf("\n"); return 0; } |