/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define FOR(x, b, e) for(long x = b; x <= (e); x++) #define FORD(x, b, e) for(long x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) #define LL long long using namespace std; class Node { public: long nr; long ile; Node* forw[2]; Node() { nr=0; ile=0; forw[0]=NULL; forw[1]=NULL; } void addEdge(Node* where) { ile++; forw[ile-1]=where; }; }; vector<Node*> nodes; Node* pot[40]; // wezly potegowe void zrobPotegowe(long p) { Node* node=new Node(); nodes.push_back(node); pot[0]=node; Node* prevPot=node; FOR(i,1,p) { Node* gorny=new Node(); nodes.push_back(gorny); Node* dolny=new Node(); nodes.push_back(dolny); gorny->addEdge(prevPot); dolny->addEdge(prevPot); dolny->addEdge(gorny); pot[i]=dolny; prevPot=dolny; } } void ponumeruj(long od) { long nr=od; FOREACH(pn,nodes) { Node* node=*pn; if (node->nr==0) { node->nr=nr; nr++; } } } bool comp(Node* n1, Node* n2) { return n1->nr < n2->nr; } void wypiszGraf() { sort(nodes.begin(), nodes.end(), comp); cout << nodes.size() << "\n"; FOR(i,1,nodes.size()) { //cout << nodes[i-1]->nr << ": "; if (nodes[i-1]->ile==0) cout << "-1 -1\n"; else if (nodes[i-1]->ile==1) cout << nodes[i-1]->forw[0]->nr << " -1\n"; else cout << nodes[i-1]->forw[0]->nr << " " << nodes[i-1]->forw[1]->nr << "\n"; } } void zrobPoczatkowe(LL k) { long b=0; Node* node=NULL; Node* last=NULL; while (k!=0) { if (k%2==1) { if (node==NULL) { node=new Node(); nodes.push_back(node); last=node; node->addEdge(pot[b]); } else if (node->ile==1) { node->addEdge(pot[b]); } else { node=new Node(); nodes.push_back(node); node->addEdge(last); last=node; node->addEdge(pot[b]); } } k=k/2; b++; } last->nr=1; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); LL k; cin >> k; zrobPotegowe(30); zrobPoczatkowe(k); ponumeruj(2); // dodaj koncowy node: Node* last=new Node(); nodes.push_back(last); last->nr=nodes.size(); pot[0]->addEdge(last); wypiszGraf(); 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define FOR(x, b, e) for(long x = b; x <= (e); x++) #define FORD(x, b, e) for(long x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) #define LL long long using namespace std; class Node { public: long nr; long ile; Node* forw[2]; Node() { nr=0; ile=0; forw[0]=NULL; forw[1]=NULL; } void addEdge(Node* where) { ile++; forw[ile-1]=where; }; }; vector<Node*> nodes; Node* pot[40]; // wezly potegowe void zrobPotegowe(long p) { Node* node=new Node(); nodes.push_back(node); pot[0]=node; Node* prevPot=node; FOR(i,1,p) { Node* gorny=new Node(); nodes.push_back(gorny); Node* dolny=new Node(); nodes.push_back(dolny); gorny->addEdge(prevPot); dolny->addEdge(prevPot); dolny->addEdge(gorny); pot[i]=dolny; prevPot=dolny; } } void ponumeruj(long od) { long nr=od; FOREACH(pn,nodes) { Node* node=*pn; if (node->nr==0) { node->nr=nr; nr++; } } } bool comp(Node* n1, Node* n2) { return n1->nr < n2->nr; } void wypiszGraf() { sort(nodes.begin(), nodes.end(), comp); cout << nodes.size() << "\n"; FOR(i,1,nodes.size()) { //cout << nodes[i-1]->nr << ": "; if (nodes[i-1]->ile==0) cout << "-1 -1\n"; else if (nodes[i-1]->ile==1) cout << nodes[i-1]->forw[0]->nr << " -1\n"; else cout << nodes[i-1]->forw[0]->nr << " " << nodes[i-1]->forw[1]->nr << "\n"; } } void zrobPoczatkowe(LL k) { long b=0; Node* node=NULL; Node* last=NULL; while (k!=0) { if (k%2==1) { if (node==NULL) { node=new Node(); nodes.push_back(node); last=node; node->addEdge(pot[b]); } else if (node->ile==1) { node->addEdge(pot[b]); } else { node=new Node(); nodes.push_back(node); node->addEdge(last); last=node; node->addEdge(pot[b]); } } k=k/2; b++; } last->nr=1; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); LL k; cin >> k; zrobPotegowe(30); zrobPoczatkowe(k); ponumeruj(2); // dodaj koncowy node: Node* last=new Node(); nodes.push_back(last); last->nr=nodes.size(); pot[0]->addEdge(last); wypiszGraf(); return 0; }; |