#include <bits/stdc++.h> #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; typedef long long LL; typedef unsigned long long ULL; using std::vector; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Vertex { Vertex(Vertex *a = nullptr, Vertex *b = nullptr); size_t index; Vertex *next[2]; int ways; }; vector<Vertex*> vertices; Vertex::Vertex(Vertex *a, Vertex *b): next{a,b} { ways = 0; REP(i,2) if(next[i]) ways += next[i]->ways; index = vertices.size(); vertices.push_back(this); } Vertex *target; Vertex *generate(int goal) { if (goal == 1) { return new Vertex(target); } else { Vertex *a = generate(goal/2); Vertex *b = goal%2==0 ? new Vertex(a) : new Vertex(a, target); return new Vertex(a, b); } } void print_graph() { assert(vertices.size()<=100); std::cout << vertices.size() << "\n"; std::reverse(vertices.begin(), vertices.end()); for (Vertex *v : vertices) { REP(i,2) { Vertex *next = v->next[i]; if (!next) std::cout << "-1"; else std::cout << (vertices.size() - next->index); std::cout << " "; } std::cout << "\n"; } } int main() { init_io(); target = new Vertex(); target->ways = 1; int goal; std::cin >> goal; Vertex *v = generate(goal); assert(v->ways == goal); print_graph(); }
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 | #include <bits/stdc++.h> #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; typedef long long LL; typedef unsigned long long ULL; using std::vector; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Vertex { Vertex(Vertex *a = nullptr, Vertex *b = nullptr); size_t index; Vertex *next[2]; int ways; }; vector<Vertex*> vertices; Vertex::Vertex(Vertex *a, Vertex *b): next{a,b} { ways = 0; REP(i,2) if(next[i]) ways += next[i]->ways; index = vertices.size(); vertices.push_back(this); } Vertex *target; Vertex *generate(int goal) { if (goal == 1) { return new Vertex(target); } else { Vertex *a = generate(goal/2); Vertex *b = goal%2==0 ? new Vertex(a) : new Vertex(a, target); return new Vertex(a, b); } } void print_graph() { assert(vertices.size()<=100); std::cout << vertices.size() << "\n"; std::reverse(vertices.begin(), vertices.end()); for (Vertex *v : vertices) { REP(i,2) { Vertex *next = v->next[i]; if (!next) std::cout << "-1"; else std::cout << (vertices.size() - next->index); std::cout << " "; } std::cout << "\n"; } } int main() { init_io(); target = new Vertex(); target->ways = 1; int goal; std::cin >> goal; Vertex *v = generate(goal); assert(v->ways == goal); print_graph(); } |