#include<cstdio> int k, len, count, next_node, sink; int binary[33]; int main() { scanf("%d", &k); if (k == 1) { printf("2\n2 -1\n-1 -1\n"); return 0; } len = count = 0; while (k) { binary[len++] = k & 1; count += k & 1; k >>= 1; } sink = 2 * len + count - 2; printf("%d\n", sink); next_node = 2; if (binary[0]) printf("%d %d\n", next_node++, sink); for (int i = 1; i < len - 1; ++i) { printf("%d %d\n%d -1\n", next_node, next_node + 1, next_node + 1); next_node += 2; if (binary[i]) printf("%d %d\n", next_node++, sink); } printf("%d %d\n%d -1\n-1 -1\n", next_node, next_node + 1, next_node + 1); 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 | #include<cstdio> int k, len, count, next_node, sink; int binary[33]; int main() { scanf("%d", &k); if (k == 1) { printf("2\n2 -1\n-1 -1\n"); return 0; } len = count = 0; while (k) { binary[len++] = k & 1; count += k & 1; k >>= 1; } sink = 2 * len + count - 2; printf("%d\n", sink); next_node = 2; if (binary[0]) printf("%d %d\n", next_node++, sink); for (int i = 1; i < len - 1; ++i) { printf("%d %d\n%d -1\n", next_node, next_node + 1, next_node + 1); next_node += 2; if (binary[i]) printf("%d %d\n", next_node++, sink); } printf("%d %d\n%d -1\n-1 -1\n", next_node, next_node + 1, next_node + 1); return 0; } |