#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ff first #define ss second using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.ff << "," << p.ss << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- vector<pii> nodes; int last = 0; void create_chain(int n) { nodes = vector<pii>(n, pair(-1, -1)); FOR(i, 1, n-1) { nodes[i] = pair(i-1, size(nodes)); nodes.emplace_back(pair(i-1, -1)); } } int prv = -1; void connect(int p) { nodes.emplace_back(prv, p); prv = size(nodes)-1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> k; create_chain(32); for(int p = 0; k > 0; ++p) { if(k & (1 << p)) { connect(p); k ^= (1 << p); } } reverse(nodes.begin(), nodes.end()); int n = size(nodes); cout << n << '\n'; for(auto [a, b] : nodes) { cout << (a == -1 ? -1 : n-a) << ' ' << (b == -1 ? -1 : n-b) << '\n'; } debug(size(nodes)); }
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ff first #define ss second using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.ff << "," << p.ss << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- vector<pii> nodes; int last = 0; void create_chain(int n) { nodes = vector<pii>(n, pair(-1, -1)); FOR(i, 1, n-1) { nodes[i] = pair(i-1, size(nodes)); nodes.emplace_back(pair(i-1, -1)); } } int prv = -1; void connect(int p) { nodes.emplace_back(prv, p); prv = size(nodes)-1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> k; create_chain(32); for(int p = 0; k > 0; ++p) { if(k & (1 << p)) { connect(p); k ^= (1 << p); } } reverse(nodes.begin(), nodes.end()); int n = size(nodes); cout << n << '\n'; for(auto [a, b] : nodes) { cout << (a == -1 ? -1 : n-a) << ' ' << (b == -1 ? -1 : n-b) << '\n'; } debug(size(nodes)); } |