#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int dag[1005][2]; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int k; std::cin >> k; int fre = 2; int head = 1; while(k > 1){ if(k % 2 == 0){ dag[head][0] = fre; dag[head][1] = fre+1; dag[fre][0] = fre+1; dag[fre][1] = -1; head = fre+1; fre += 2; k /= 2; }else{ dag[head][0] = fre; dag[head][1] = -2; head = fre; fre++; k--; } } dag[head][0] = fre; dag[head][1] = -1; dag[fre][0] = -1; dag[fre][1] = -1; fre++; std::cout << fre-1 << "\n"; REP(i, 1, fre) std::cout << dag[i][0] << " " << (dag[i][1] == -2 ? fre-1 : dag[i][1])<< "\n"; 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int dag[1005][2]; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int k; std::cin >> k; int fre = 2; int head = 1; while(k > 1){ if(k % 2 == 0){ dag[head][0] = fre; dag[head][1] = fre+1; dag[fre][0] = fre+1; dag[fre][1] = -1; head = fre+1; fre += 2; k /= 2; }else{ dag[head][0] = fre; dag[head][1] = -2; head = fre; fre++; k--; } } dag[head][0] = fre; dag[head][1] = -1; dag[fre][0] = -1; dag[fre][1] = -1; fre++; std::cout << fre-1 << "\n"; REP(i, 1, fre) std::cout << dag[i][0] << " " << (dag[i][1] == -2 ? fre-1 : dag[i][1])<< "\n"; return 0; } |