#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)
#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
if(ISDEBUG) {
for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}
static const int MAX_POWER = 29;
static const int MAX_V = MAX_POWER * 3 + 2;
static const int START = 2*MAX_POWER+1;
int graph[100][2];
void build_graph() {
graph[0][0] = graph[0][1] = -1;
for(int i=1; i<=MAX_POWER; ++i) {
graph[2*i][0] = 2*i-1;
graph[2*i][1] = 2*i-2;
graph[2*i-1][0] = 2*i-2;
graph[2*i-1][1] = -1;
}
for(int i=0; i<=MAX_POWER; ++i) {
graph[START+i][0] = i*2;
if(i==0) graph[START][1] = -1;
else graph[START+i][1] = START+i-1;
}
}
int transform(int index) {
if(-1 == index) return -1;
else return MAX_V - index;
}
void print_graph() {
printf("%d\n", MAX_V);
for(int i=MAX_V-1; i>=0; --i) {
static int id = 0;
++id;
dprintf("%2d #%d: ", (i<=2*MAX_POWER && i%2==0) ? (int)pow(2,i/2) : 0, id);
printf("%d %d\n", transform(graph[i][0]), transform(graph[i][1]));
}
}
int main() {
build_graph();
GET(x);
int bit = 0;
for(int bit = 0; bit <= MAX_POWER; ++bit) {
dprintf("bit #%d: %d\n", bit, x%2);
if(0==x%2) {
graph[START+bit][0] = -1;
}
x/=2;
}
print_graph();
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a); i<(b); ++i) typedef vector<int> vi; #define pb push_back #define sz size() typedef pair<int,int> pii; #define mp make_pair #define st first #define nd second typedef long long ll; #define INF 1000000001 #define ALL(t) t.begin(),t.end() #define SC(a) scanf("%d", &a) #define GET(a) int a; SC(a) #define ISDEBUG 0 #define dprintf(...) if(ISDEBUG) \ {printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");} template <class It> void dptab(It b, It e, const char* f="%d ") { if(ISDEBUG) { for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n"); }} static const int MAX_POWER = 29; static const int MAX_V = MAX_POWER * 3 + 2; static const int START = 2*MAX_POWER+1; int graph[100][2]; void build_graph() { graph[0][0] = graph[0][1] = -1; for(int i=1; i<=MAX_POWER; ++i) { graph[2*i][0] = 2*i-1; graph[2*i][1] = 2*i-2; graph[2*i-1][0] = 2*i-2; graph[2*i-1][1] = -1; } for(int i=0; i<=MAX_POWER; ++i) { graph[START+i][0] = i*2; if(i==0) graph[START][1] = -1; else graph[START+i][1] = START+i-1; } } int transform(int index) { if(-1 == index) return -1; else return MAX_V - index; } void print_graph() { printf("%d\n", MAX_V); for(int i=MAX_V-1; i>=0; --i) { static int id = 0; ++id; dprintf("%2d #%d: ", (i<=2*MAX_POWER && i%2==0) ? (int)pow(2,i/2) : 0, id); printf("%d %d\n", transform(graph[i][0]), transform(graph[i][1])); } } int main() { build_graph(); GET(x); int bit = 0; for(int bit = 0; bit <= MAX_POWER; ++bit) { dprintf("bit #%d: %d\n", bit, x%2); if(0==x%2) { graph[START+bit][0] = -1; } x/=2; } print_graph(); return 0; } |
English