#include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define MAXN 100001 using namespace std; int calc_rest(int k, vi &values, vi &path) { int i = values.size() - 1; int cost = 0; while (k > 0) { if (values[i] > k) { i--; } else { k -= values[i]; cost++; path[i]++; } } return cost; } int prepare_values(int k, vi &values) { ll next_val = 2; values.pb(1); int cost = 0; while (next_val <= k) { values.pb(next_val); next_val *= 3; cost += 3; } return cost; } int main_path(int N, vi &values, vi &idx, vector<vi> &V) { int current = N; int next_node = 4; idx[0] = N; for (int i = 1; i < values.size(); i++) { idx[i] = next_node; int leftn = next_node - 2; int rightn = next_node - 1; V[leftn].pb(current); V[rightn].pb(current); if (leftn > 2) { V[leftn].pb(leftn - 3); V[rightn].pb(rightn - 3); } V[next_node].pb(leftn); V[next_node].pb(rightn); current = next_node; next_node += 3; } return current; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k; cin >> k; if(k == 1) { cout<<2<<endl; cout<<2<<" "<<-1<<endl; cout<<-1<<" "<<-1<<endl; return 0; } vi values; int cost = 2; cost += prepare_values(k, values); // cout << cost << endl; vi path(values.size(), 0); cost += calc_rest(k, values, path); vi idx(values.size()); int N = cost; vector<vi> V(N + 1); int next_node = main_path(N, values, idx, V) + 1; int parent = 1; for (int i = values.size() - 1; i >= 0; i--) { for (int j = 0; j < path[i]; j++) { V[parent].pb(next_node); V[next_node].pb(idx[i]); parent = next_node; next_node++; } } for (int i = 0; i < V.size(); i++) while (V[i].size() < 2) { V[i].pb(-1); } cout << N << endl; for (int i = 1; i < V.size(); i++) cout<<V[i][0]<<" "<<V[i][1]<<endl; // cout << i << ": " << V[i][0] << " " << V[i][1] << 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 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define MAXN 100001 using namespace std; int calc_rest(int k, vi &values, vi &path) { int i = values.size() - 1; int cost = 0; while (k > 0) { if (values[i] > k) { i--; } else { k -= values[i]; cost++; path[i]++; } } return cost; } int prepare_values(int k, vi &values) { ll next_val = 2; values.pb(1); int cost = 0; while (next_val <= k) { values.pb(next_val); next_val *= 3; cost += 3; } return cost; } int main_path(int N, vi &values, vi &idx, vector<vi> &V) { int current = N; int next_node = 4; idx[0] = N; for (int i = 1; i < values.size(); i++) { idx[i] = next_node; int leftn = next_node - 2; int rightn = next_node - 1; V[leftn].pb(current); V[rightn].pb(current); if (leftn > 2) { V[leftn].pb(leftn - 3); V[rightn].pb(rightn - 3); } V[next_node].pb(leftn); V[next_node].pb(rightn); current = next_node; next_node += 3; } return current; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k; cin >> k; if(k == 1) { cout<<2<<endl; cout<<2<<" "<<-1<<endl; cout<<-1<<" "<<-1<<endl; return 0; } vi values; int cost = 2; cost += prepare_values(k, values); // cout << cost << endl; vi path(values.size(), 0); cost += calc_rest(k, values, path); vi idx(values.size()); int N = cost; vector<vi> V(N + 1); int next_node = main_path(N, values, idx, V) + 1; int parent = 1; for (int i = values.size() - 1; i >= 0; i--) { for (int j = 0; j < path[i]; j++) { V[parent].pb(next_node); V[next_node].pb(idx[i]); parent = next_node; next_node++; } } for (int i = 0; i < V.size(); i++) while (V[i].size() < 2) { V[i].pb(-1); } cout << N << endl; for (int i = 1; i < V.size(); i++) cout<<V[i][0]<<" "<<V[i][1]<<endl; // cout << i << ": " << V[i][0] << " " << V[i][1] << endl; return 0; } |