#include <stdio.h>
#include <vector>
int totalBits[1000000];
int bits[1024];
int calcBits(int a){
int ret = 0;
while(a > 0){
ret += (a & 1);
a = a >> 1;
}
return ret;
}
void preFillBits(){
for(unsigned int i = 0; i < 256; ++i){
bits[i] = calcBits(i);
}
}
int calcBiggerBits(int a){
int ret = 0;
while(a > 0){
ret += bits[a & 255];
a = a >> 8;
}
return ret;
}
int main(){
int n;
std::vector<int> results;
scanf("%d", &n);
preFillBits();
int idx = 1;
while(1){
totalBits[idx] = totalBits[idx - 1] + calcBiggerBits(idx);
// printf("%d %d\n", idx, totalBits[idx]);
if (totalBits[idx] >= n)
break;
++idx;
}
while(n > 0){
results.push_back(idx);
n -= calcBiggerBits(idx);
while(totalBits[idx - 1] >= n){
--idx;
}
}
printf("%d\n", (int)results.size());
for(int i = 0; i < (int) results.size(); ++i) 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 | #include <stdio.h> #include <vector> int totalBits[1000000]; int bits[1024]; int calcBits(int a){ int ret = 0; while(a > 0){ ret += (a & 1); a = a >> 1; } return ret; } void preFillBits(){ for(unsigned int i = 0; i < 256; ++i){ bits[i] = calcBits(i); } } int calcBiggerBits(int a){ int ret = 0; while(a > 0){ ret += bits[a & 255]; a = a >> 8; } return ret; } int main(){ int n; std::vector<int> results; scanf("%d", &n); preFillBits(); int idx = 1; while(1){ totalBits[idx] = totalBits[idx - 1] + calcBiggerBits(idx); // printf("%d %d\n", idx, totalBits[idx]); if (totalBits[idx] >= n) break; ++idx; } while(n > 0){ results.push_back(idx); n -= calcBiggerBits(idx); while(totalBits[idx - 1] >= n){ --idx; } } printf("%d\n", (int)results.size()); for(int i = 0; i < (int) results.size(); ++i) printf("%d ", results[i]); printf("\n"); return 0; } |
English