#include <stdio.h> #define LIMIT 1000000 int ans[LIMIT]; int ansl = 0; int memo[LIMIT]; int nbits(unsigned int u){ unsigned int uCount; uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); return ((uCount + (uCount >> 3)) & 030707070707) % 63; } int main() { int n, nn; scanf("%d", &n); nn = n; unsigned sum = 0, num = 0; while(sum < n) { num++; memo[num-1] = nbits(num); sum += memo[num-1]; } ans[ansl++] = num; nn -= memo[num-1]; sum -= memo[num-1]; while(nn) { num--; if(sum >= nn && sum-memo[num-1] < nn) { nn -= memo[num-1]; ans[ansl++] = num; } sum -= memo[num-1]; } printf("%d\n", ansl); //int chk = 0; for(int i=0;i<ansl;i++) { printf("%d ", ans[i]); //chk += nbits(ans[i]); } //printf("%d\n", chk); 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 | #include <stdio.h> #define LIMIT 1000000 int ans[LIMIT]; int ansl = 0; int memo[LIMIT]; int nbits(unsigned int u){ unsigned int uCount; uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); return ((uCount + (uCount >> 3)) & 030707070707) % 63; } int main() { int n, nn; scanf("%d", &n); nn = n; unsigned sum = 0, num = 0; while(sum < n) { num++; memo[num-1] = nbits(num); sum += memo[num-1]; } ans[ansl++] = num; nn -= memo[num-1]; sum -= memo[num-1]; while(nn) { num--; if(sum >= nn && sum-memo[num-1] < nn) { nn -= memo[num-1]; ans[ansl++] = num; } sum -= memo[num-1]; } printf("%d\n", ansl); //int chk = 0; for(int i=0;i<ansl;i++) { printf("%d ", ans[i]); //chk += nbits(ans[i]); } //printf("%d\n", chk); return 0; } |