#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define MAX(a,b) (a > b ? a : b) int countones(int x) { x = (x & (0x55555555)) + ((x >> 1) & (0x55555555)); x = (x & (0x33333333)) + ((x >> 2) & (0x33333333)); x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f)); x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff)); x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff)); return x; } int countbits(int num) { int count=0, n; while (num > 0){ n=0; while(num >= 1<<(n+1)) n++; num -= 1<<n; count += (num + 1 + (1<<(n-1))*n); } return count; } int countbits_reverse(int num) { int count = 0; int i = 0; while (1) { count += countones(i); if (count > num) { i--; break; } i++; } printf("c_r %d %d %d\n", i, count, countbits(i)); return i; } int main(void) { struct seq { int num; struct seq *next; }; int n; int ret = 0, c = 1; int sum = 0; scanf("%d", &n); int tmp = n; int count = 0; int i = 0; int rightIdx = 0; int bitson = 0; while (1) { bitson += countones(rightIdx); if (bitson >= n) { break; } rightIdx++; } int diff = bitson - n; //printf("%d %d\n", count, bitson); int omit = -1; int tmpIdx = rightIdx; while (tmpIdx > 0) { //printf("w: %d %d\n", tmpIdx, countones(tmpIdx)); if (diff == countones(tmpIdx)) { omit = tmpIdx; break; } tmpIdx--; } printf("%d\n", omit != -1 ? rightIdx - 1 : rightIdx); for (int i = rightIdx; i > 0; i--) { if (i != omit) { printf("%d ", i); } } printf("\n"); // int diff = n - biggest; // printf("%d - %d = %d\n", n, biggest, diff); //printf("\n"); // tmpseq = head; // while (tmpseq->next != NULL) { // printf("%d\n", tmpseq->num); // } 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 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 | #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define MAX(a,b) (a > b ? a : b) int countones(int x) { x = (x & (0x55555555)) + ((x >> 1) & (0x55555555)); x = (x & (0x33333333)) + ((x >> 2) & (0x33333333)); x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f)); x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff)); x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff)); return x; } int countbits(int num) { int count=0, n; while (num > 0){ n=0; while(num >= 1<<(n+1)) n++; num -= 1<<n; count += (num + 1 + (1<<(n-1))*n); } return count; } int countbits_reverse(int num) { int count = 0; int i = 0; while (1) { count += countones(i); if (count > num) { i--; break; } i++; } printf("c_r %d %d %d\n", i, count, countbits(i)); return i; } int main(void) { struct seq { int num; struct seq *next; }; int n; int ret = 0, c = 1; int sum = 0; scanf("%d", &n); int tmp = n; int count = 0; int i = 0; int rightIdx = 0; int bitson = 0; while (1) { bitson += countones(rightIdx); if (bitson >= n) { break; } rightIdx++; } int diff = bitson - n; //printf("%d %d\n", count, bitson); int omit = -1; int tmpIdx = rightIdx; while (tmpIdx > 0) { //printf("w: %d %d\n", tmpIdx, countones(tmpIdx)); if (diff == countones(tmpIdx)) { omit = tmpIdx; break; } tmpIdx--; } printf("%d\n", omit != -1 ? rightIdx - 1 : rightIdx); for (int i = rightIdx; i > 0; i--) { if (i != omit) { printf("%d ", i); } } printf("\n"); // int diff = n - biggest; // printf("%d - %d = %d\n", n, biggest, diff); //printf("\n"); // tmpseq = head; // while (tmpseq->next != NULL) { // printf("%d\n", tmpseq->num); // } return 0; } |