#include <iostream> #include <fstream> #include <vector> #include <utility> using namespace std; #ifdef USE_CERR_LOG #define LOG if (true) cerr const bool LogEnabled = true; #else #define LOG if (false) cerr const bool LogEnabled = false; #endif bool LogBigEnabled = true; #ifdef USE_FILE_CIN ifstream fin("dag.in"); #define cin fin #endif typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; vector<int> factorize(int n) { int a = 1 << 30; int b = 30; vector<int> factors; while (a > 0) { if (n >= a) { factors.push_back(b); n -= a; } b --; a /= 2; } #ifdef USE_CERR_LOG for (int f : factors) LOG << f << ' '; LOG << endl; #endif return factors; } void solve(int k) { vector<int> factors = factorize(k); vector<pair<int, int>> nodes; nodes.resize(2*factors[0] + factors.size()); nodes[0] = {-1, -1}; for (int i = 0; i < factors[0]; i++) { nodes[2*i + 1] = {2*i, -1}; nodes[2*i + 2] = {2*i + 1, 2*i}; } int nodeIdx = factors[0] * 2 + 1; for (uint i = 1; i < factors.size(); i++, nodeIdx++) { nodes[nodeIdx] = {nodeIdx - 1, 2*factors[i]}; } int n = nodes.size(); cout << n << endl; for (int i = n-1; i>=0; i--) { int dest1 = nodes[i].first; dest1 = dest1 < 0 ? -1 : n - dest1; int dest2 = nodes[i].second; dest2 = dest2 < 0 ? -1 : n - dest2; cout << dest1 << ' ' << dest2 << endl; } } int main() { int k; cin >> k; if (k == 1) { cout << 2 << endl << "2 -1" << endl << "-1 -1" << endl; } else if (k == 2) { cout << 3 << endl << "2 3" << endl << "3 -1" << endl << "-1 -1" << endl; } else { solve(k); } 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <iostream> #include <fstream> #include <vector> #include <utility> using namespace std; #ifdef USE_CERR_LOG #define LOG if (true) cerr const bool LogEnabled = true; #else #define LOG if (false) cerr const bool LogEnabled = false; #endif bool LogBigEnabled = true; #ifdef USE_FILE_CIN ifstream fin("dag.in"); #define cin fin #endif typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; vector<int> factorize(int n) { int a = 1 << 30; int b = 30; vector<int> factors; while (a > 0) { if (n >= a) { factors.push_back(b); n -= a; } b --; a /= 2; } #ifdef USE_CERR_LOG for (int f : factors) LOG << f << ' '; LOG << endl; #endif return factors; } void solve(int k) { vector<int> factors = factorize(k); vector<pair<int, int>> nodes; nodes.resize(2*factors[0] + factors.size()); nodes[0] = {-1, -1}; for (int i = 0; i < factors[0]; i++) { nodes[2*i + 1] = {2*i, -1}; nodes[2*i + 2] = {2*i + 1, 2*i}; } int nodeIdx = factors[0] * 2 + 1; for (uint i = 1; i < factors.size(); i++, nodeIdx++) { nodes[nodeIdx] = {nodeIdx - 1, 2*factors[i]}; } int n = nodes.size(); cout << n << endl; for (int i = n-1; i>=0; i--) { int dest1 = nodes[i].first; dest1 = dest1 < 0 ? -1 : n - dest1; int dest2 = nodes[i].second; dest2 = dest2 < 0 ? -1 : n - dest2; cout << dest1 << ' ' << dest2 << endl; } } int main() { int k; cin >> k; if (k == 1) { cout << 2 << endl << "2 -1" << endl << "-1 -1" << endl; } else if (k == 2) { cout << 3 << endl << "2 3" << endl << "3 -1" << endl << "-1 -1" << endl; } else { solve(k); } return 0; } |