#include<bits/stdc++.h> using namespace std; int main(void){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int k; cin >> k; vector<pair<int,int> > t; for(int i=0;i<30;i++) t.push_back({i+1,-2}); t.back().first = -2; int free=0; int msb=30; while(!(k&(1<<msb))) msb--; msb--; while(msb >= 0){ int last = t.size()-1; t[last] = {last+1,last+2}; t.push_back({last+2,-2}); t.push_back({-2,-2}); if(k&(1<<msb)) t[free++].second = last+2; msb--; } cout << t.size() << "\n"; for(auto& i : t) cout << i.first+1 << " " << i.second+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 | #include<bits/stdc++.h> using namespace std; int main(void){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int k; cin >> k; vector<pair<int,int> > t; for(int i=0;i<30;i++) t.push_back({i+1,-2}); t.back().first = -2; int free=0; int msb=30; while(!(k&(1<<msb))) msb--; msb--; while(msb >= 0){ int last = t.size()-1; t[last] = {last+1,last+2}; t.push_back({last+2,-2}); t.push_back({-2,-2}); if(k&(1<<msb)) t[free++].second = last+2; msb--; } cout << t.size() << "\n"; for(auto& i : t) cout << i.first+1 << " " << i.second+1 << "\n"; return 0; } |