#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#ifdef HOME_
#define DEBUG(x) x
#else
#define DEBUG(x)
#endif
#define REP(x,n) for(int x=0;x<(n);++x)
#define FOREACH(x,n) for(__typeof(n.begin()) x = (n).begin(); x != (n).end(); ++x)
using namespace std;
#define LAST_V -100
#define NONE -1
const int MAX_N = 102;
typedef pair<int,int> PII;
int n;
PII paths[MAX_N];
int main() {
ios_base::sync_with_stdio(0);
cin>>n;
if (n==1) {
cout << "2\n2 -1\n-1 -1";
return 0;
}
int v = 1;
while(n!=1) {
if (n&1) {
paths[v] = {LAST_V, v+1};
--n;
++v;
}
else {
paths[v] = {v+1, v+2};
paths[v+1] = {v+2, NONE};
v += 2;
n /= 2;
}
}
paths[v] = {NONE, NONE};
cout << v << endl;
REP(x,v) {
#define FIX(x) ((x)==LAST_V ? v : (x))
cout << FIX(paths[x+1].first) << " " << FIX(paths[x+1].second) << endl;
}
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 | #include <iostream> #include <algorithm> #include <set> #include <vector> #ifdef HOME_ #define DEBUG(x) x #else #define DEBUG(x) #endif #define REP(x,n) for(int x=0;x<(n);++x) #define FOREACH(x,n) for(__typeof(n.begin()) x = (n).begin(); x != (n).end(); ++x) using namespace std; #define LAST_V -100 #define NONE -1 const int MAX_N = 102; typedef pair<int,int> PII; int n; PII paths[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin>>n; if (n==1) { cout << "2\n2 -1\n-1 -1"; return 0; } int v = 1; while(n!=1) { if (n&1) { paths[v] = {LAST_V, v+1}; --n; ++v; } else { paths[v] = {v+1, v+2}; paths[v+1] = {v+2, NONE}; v += 2; n /= 2; } } paths[v] = {NONE, NONE}; cout << v << endl; REP(x,v) { #define FIX(x) ((x)==LAST_V ? v : (x)) cout << FIX(paths[x+1].first) << " " << FIX(paths[x+1].second) << endl; } return 0; } |
English