#include "bits/stdc++.h"
using namespace std;
template <class A, class B> ostream& operator<<(ostream &os, pair<A, B> p) {
return os << '{' << p.first << ", " << p.second << '}';
}
template <class T> ostream& operator<<(ostream &os, vector<T> v) {
os << '{';
for (T i : v) os << i << ", ";
return os << '}';
}
#ifdef DEBUG
#define D(x...) x
#else
#define D(x...)
#endif
#define LN(x) D(cerr << #x << ": " << x << ' ')
#define LOG(x) D(cerr << #x << ": " << x << '\n')
#define MES(x) D(cerr << x << ' ')
#define MESL(x) D(cerr << x << '\n')
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
#define EB emplace_back
#define PB pop_back
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
// Ignacy Boehlke
int main() {
int n;
scanf("%d", &n);
int N = 2, n2 = n;
vector<pii> graph(N, {-1, -1});
while (n2 > 1) {
graph.resize(N += 2, {-1, -1});
graph[N - 3] = {N - 1, N - 2};
graph[N - 2].fi = N - 1;
n2 >>= 1;
}
graph.back().fi = N;
graph.EB(-1, -1);
REP(i, N / 2 - 1) if (n & (1 << i)) graph[i * 2 + 2].se = N;
printf("%d\n", N);
FOR(i, 1, N) printf("%d %d\n", graph[i].fi, graph[i].se);
}
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 | #include "bits/stdc++.h" using namespace std; template <class A, class B> ostream& operator<<(ostream &os, pair<A, B> p) { return os << '{' << p.first << ", " << p.second << '}'; } template <class T> ostream& operator<<(ostream &os, vector<T> v) { os << '{'; for (T i : v) os << i << ", "; return os << '}'; } #ifdef DEBUG #define D(x...) x #else #define D(x...) #endif #define LN(x) D(cerr << #x << ": " << x << ' ') #define LOG(x) D(cerr << #x << ": " << x << '\n') #define MES(x) D(cerr << x << ' ') #define MESL(x) D(cerr << x << '\n') typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<bool> vb; typedef pair<int, int> pii; #define EB emplace_back #define PB pop_back #define fi first #define se second #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) // Ignacy Boehlke int main() { int n; scanf("%d", &n); int N = 2, n2 = n; vector<pii> graph(N, {-1, -1}); while (n2 > 1) { graph.resize(N += 2, {-1, -1}); graph[N - 3] = {N - 1, N - 2}; graph[N - 2].fi = N - 1; n2 >>= 1; } graph.back().fi = N; graph.EB(-1, -1); REP(i, N / 2 - 1) if (n & (1 << i)) graph[i * 2 + 2].se = N; printf("%d\n", N); FOR(i, 1, N) printf("%d %d\n", graph[i].fi, graph[i].se); } |
English