#include <bits/stdc++.h> int k; int m = 0; int left[103]; int right[103]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> k; std::vector<int> v; int pot = (1<<29); int wyk = 29; while (k > 0) { if (pot <= k) { v.push_back(wyk); k -= pot; } pot /= 2; wyk--; } // 1 1 2 2 4 4 8 8, ... 2^29 for (int i = 0; i <= 100; i++) left[i] = right[i] = -1; left[1] = 2; m = 2; for (int i = 1; i <= v[0]; i++) { left[2*i] = 2*i+1; right[2*i-1] = 2*i+1; left[2*i+1] = 2*i+2; m += 2; } // do wierzcholka m jest v[0] sciezek, teraz doklejam kolejne moze for (int i = 1; i <= v.size()-1; i++) { m++; left[m-1] = m; int x = 2*(v[i]+1); right[x] = m; } std::cout << m << "\n"; for (int i = 1; i <= m; i++) std::cout << left[i] << " " << right[i] << "\n"; }
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 | #include <bits/stdc++.h> int k; int m = 0; int left[103]; int right[103]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> k; std::vector<int> v; int pot = (1<<29); int wyk = 29; while (k > 0) { if (pot <= k) { v.push_back(wyk); k -= pot; } pot /= 2; wyk--; } // 1 1 2 2 4 4 8 8, ... 2^29 for (int i = 0; i <= 100; i++) left[i] = right[i] = -1; left[1] = 2; m = 2; for (int i = 1; i <= v[0]; i++) { left[2*i] = 2*i+1; right[2*i-1] = 2*i+1; left[2*i+1] = 2*i+2; m += 2; } // do wierzcholka m jest v[0] sciezek, teraz doklejam kolejne moze for (int i = 1; i <= v.size()-1; i++) { m++; left[m-1] = m; int x = 2*(v[i]+1); right[x] = m; } std::cout << m << "\n"; for (int i = 1; i <= m; i++) std::cout << left[i] << " " << right[i] << "\n"; } |