#include <iostream> #include <vector> typedef long long int ll; constexpr int c_last=99; constexpr int c_none=-2; struct node { int first; int second; int cnt=0; }; std::vector<node> g; void solve(int k) { g.clear(); g.push_back({c_none,c_none}); --k; g.push_back({c_last,c_none,1}); int first=1; while(k) { int best_k=1; int best_id=c_last; for(int i=1;i<(int)g.size();++i) if(i!=first&&g[i].cnt<=k&&g[i].cnt>best_k) { best_k=g[i].cnt; best_id=i; } int cnt=g[first].cnt+best_k; g.push_back({first,best_id,cnt}); first=(int)g.size()-1; k-=best_k; } g[0].first=first; while(g.size()<=c_last) g.push_back({c_none,c_none}); #if 1 std::cout<<g.size()<<std::endl; for(auto v:g) std::cout<<(v.first+1)<<" "<<(v.second+1)<<std::endl; #endif } int check(int id) { if(id==c_last)return 1; int res=0; if(g[id].first!=c_none)res+=check(g[id].first); if(g[id].second!=c_none)res+=check(g[id].second); return res; } int main() { #if 1 int k; std::cin>>k; solve(k); #else for(int k=1;k<1000;++k) //int k=10000000; { solve(k); ll expected=check(0); if(k!=expected) { int x=0; ++x; } } #endif 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 | #include <iostream> #include <vector> typedef long long int ll; constexpr int c_last=99; constexpr int c_none=-2; struct node { int first; int second; int cnt=0; }; std::vector<node> g; void solve(int k) { g.clear(); g.push_back({c_none,c_none}); --k; g.push_back({c_last,c_none,1}); int first=1; while(k) { int best_k=1; int best_id=c_last; for(int i=1;i<(int)g.size();++i) if(i!=first&&g[i].cnt<=k&&g[i].cnt>best_k) { best_k=g[i].cnt; best_id=i; } int cnt=g[first].cnt+best_k; g.push_back({first,best_id,cnt}); first=(int)g.size()-1; k-=best_k; } g[0].first=first; while(g.size()<=c_last) g.push_back({c_none,c_none}); #if 1 std::cout<<g.size()<<std::endl; for(auto v:g) std::cout<<(v.first+1)<<" "<<(v.second+1)<<std::endl; #endif } int check(int id) { if(id==c_last)return 1; int res=0; if(g[id].first!=c_none)res+=check(g[id].first); if(g[id].second!=c_none)res+=check(g[id].second); return res; } int main() { #if 1 int k; std::cin>>k; solve(k); #else for(int k=1;k<1000;++k) //int k=10000000; { solve(k); ll expected=check(0); if(k!=expected) { int x=0; ++x; } } #endif return 0; } |