fork(4) download
  1. /**
  2.  * A Fibonacci heap, a graph with weighted edges, and dijkstra's algorithm
  3.  *
  4.  * Author: Erel Segal http://t...content-available-to-author-only...s.fm/rentabrain
  5.  * Date: 2010-11-11
  6.  */
  7.  
  8.  
  9.  
  10. #include <iostream>
  11. #include <map>
  12. #include <set>
  13. #include <string>
  14. #include <queue>
  15. #include <algorithm>
  16. #include <vector>
  17. using namespace std;
  18.  
  19. typedef unsigned int uint;
  20.  
  21.  
  22.  
  23. /* ------ PART A: Fibonacci Heap -------- */
  24.  
  25. void maximize (uint& a, uint b) {
  26. if (b>a)
  27. a = b;
  28. }
  29.  
  30.  
  31. /**
  32.  * The heap is a min-heap sorted by Key.
  33.  */
  34. template <typename Data, typename Key> class FibonacciHeapNode {
  35. Key myKey;
  36. Data myData;
  37.  
  38. uint degree; // number of childern. used in the removeMinimum algorithm.
  39. bool mark; // mark used in the decreaseKey algorithm.
  40.  
  41. FibonacciHeapNode<Data,Key>* previous; // pointers in a circular doubly linked list
  42. FibonacciHeapNode<Data,Key>* next;
  43. FibonacciHeapNode<Data,Key>* child; // pointer to the first child in the list of children
  44. FibonacciHeapNode<Data,Key>* parent;
  45.  
  46. FibonacciHeapNode() {}
  47. FibonacciHeapNode(Data d, Key k):
  48. myKey(k),
  49. myData(d),
  50. degree(0),
  51. mark(false),
  52. child(NULL),
  53. parent(NULL)
  54. {
  55. previous = next = this; // doubly linked circular list
  56. }
  57.  
  58. bool isSingle() const {
  59. return (this == this->next);
  60. }
  61.  
  62. // inserts a new node after this node
  63. void insert(FibonacciHeapNode<Data,Key>* other) {
  64. if (!other)
  65. return;
  66.  
  67. // For example: given 1->2->3->4->1, insert a->b->c->d->a after node 3:
  68. // result: 1->2->3->a->b->c->d->4->1
  69.  
  70. this->next->previous = other->previous;
  71. other->previous->next = this->next;
  72.  
  73. this->next = other;
  74. other->previous = this;
  75. }
  76.  
  77. void remove() {
  78. this->previous->next = this->next;
  79. this->next->previous = this->previous;
  80. this->next = this->previous = this;
  81. }
  82.  
  83. void addChild(FibonacciHeapNode<Data,Key>* other) { // Fibonacci-Heap-Link(other,current)
  84. if (!child)
  85. child = other;
  86. else
  87. child->insert(other);
  88. other->parent = this;
  89. other->mark = false;
  90. degree++;
  91. }
  92.  
  93. void removeChild(FibonacciHeapNode<Data,Key>* other) {
  94. if (other->parent!=this)
  95. throw string ("Trying to remove a child from a non-parent");
  96. if (other->isSingle()) {
  97. if (child != other)
  98. throw string ("Trying to remove a non-child");
  99. child = NULL;
  100. } else {
  101. if (child == other)
  102. child = other->next;
  103. other->remove(); // from list of children
  104. }
  105. other->parent=NULL;
  106. other->mark = false;
  107. degree--;
  108. }
  109.  
  110.  
  111. friend ostream& operator<< (ostream& out, const FibonacciHeapNode& n) {
  112. return (out << n.myData << ":" << n.myKey);
  113. }
  114.  
  115. void printTree(ostream& out) const {
  116. out << myData << ":" << myKey << ":" << degree << ":" << mark;
  117. if (child) {
  118. out << "(";
  119. const FibonacciHeapNode<Data,Key>* n=child;
  120. do {
  121. if (n==this)
  122. throw string("Illegal pointer - node is child of itself");
  123. n->printTree(out);
  124. out << " ";
  125. n = n->next;
  126. } while (n!=child);
  127. out << ")";
  128. }
  129. }
  130.  
  131. void printAll(ostream& out) const {
  132. const FibonacciHeapNode<Data,Key>* n=this;
  133. do {
  134. n->printTree(out);
  135. out << " ";
  136. n = n->next;
  137. } while (n!=this);
  138. out << endl;
  139. }
  140.  
  141. public:
  142. Key key() const { return myKey; }
  143. Data data() const { return myData; }
  144.  
  145. template <typename D, typename K> friend class FibonacciHeap;
  146. }; // FibonacciHeapNode
  147.  
  148.  
  149.  
  150. template <typename Data, typename Key> class FibonacciHeap {
  151. public:
  152. typedef FibonacciHeapNode<Data,Key>* PNode;
  153.  
  154. private:
  155. PNode rootWithMinKey; // a circular d-list of nodes
  156. uint count; // total number of elements in heap
  157. uint maxDegree; // maximum degree (=child count) of a root in the circular d-list
  158.  
  159. protected:
  160. PNode insertNode(PNode newNode) {
  161. //if (debug) cout << "insert " << (*newNode) << endl;
  162. if (!rootWithMinKey) { // insert the first myKey to the heap:
  163. rootWithMinKey = newNode;
  164. } else {
  165. rootWithMinKey->insert(newNode); // insert the root of new tree to the list of roots
  166. if (newNode->key() < rootWithMinKey->key())
  167. rootWithMinKey = newNode;
  168. }
  169. return newNode;
  170. }
  171.  
  172. public:
  173. bool debug, debugRemoveMin, debugDecreaseKey;
  174.  
  175. FibonacciHeap():
  176. rootWithMinKey(NULL), count(0), maxDegree(0), debug(false), debugRemoveMin(false) {}
  177.  
  178. ~FibonacciHeap() { /* TODO: remove all nodes */ }
  179.  
  180. bool empty() const {return count==0;}
  181.  
  182. PNode minimum() const {
  183. if (!rootWithMinKey)
  184. throw string("no minimum element");
  185. return rootWithMinKey;
  186. }
  187.  
  188. void printRoots(ostream& out) const {
  189. out << "maxDegree=" << maxDegree << " count=" << count << " roots=";
  190. if (rootWithMinKey)
  191. rootWithMinKey->printAll(out);
  192. else
  193. out << endl;
  194. }
  195.  
  196. void merge (const FibonacciHeap& other) { // Fibonacci-Heap-Union
  197. rootWithMinKey->insert(other.rootWithMinKey);
  198. if (!rootWithMinKey || (other.rootWithMinKey && other.rootWithMinKey->key() < rootWithMinKey->key()))
  199. this->rootWithMinKey = other.rootWithMinKey;
  200. count += other.count;
  201. }
  202.  
  203. PNode insert (Data d, Key k) {
  204. if (debug) cout << "insert " << d << ":" << k << endl;
  205. count++;
  206. // create a new tree with a single myKey:
  207. return insertNode(new FibonacciHeapNode<Data,Key>(d,k));
  208. }
  209.  
  210.  
  211. void removeMinimum() { // Fibonacci-Heap-Extract-Min, CONSOLIDATE
  212. if (!rootWithMinKey)
  213. throw string("trying to remove from an empty heap");
  214.  
  215. if (debug) cout << "removeMinimum " << (*rootWithMinKey) << endl;
  216. count--;
  217.  
  218. /// Phase 1: Make all the removed root's children new roots:
  219. // Make all children of root new roots:
  220. if (rootWithMinKey->child) {
  221. if (debugRemoveMin) {
  222. cout << " root's children: ";
  223. rootWithMinKey->child->printAll(cout);
  224. }
  225. PNode c = rootWithMinKey->child;
  226. do {
  227. c->parent = NULL;
  228. c = c->next;
  229. } while (c!=rootWithMinKey->child);
  230. rootWithMinKey->child = NULL; // removed all children
  231. rootWithMinKey->insert(c);
  232. }
  233. if (debugRemoveMin) {
  234. cout << " roots after inserting children: ";
  235. printRoots(cout);
  236. }
  237.  
  238.  
  239. /// Phase 2-a: handle the case where we delete the last myKey:
  240. if (rootWithMinKey->next == rootWithMinKey) {
  241. if (debugRemoveMin) cout << " removed the last" << endl;
  242. if (count!=0)
  243. throw string ("Internal error: should have 0 keys");
  244. rootWithMinKey = NULL;
  245. return;
  246. }
  247.  
  248. /// Phase 2: merge roots with the same degree:
  249. vector<PNode> degreeRoots (maxDegree+1); // make room for a new degree
  250. fill (degreeRoots.begin(), degreeRoots.end(), (PNode)NULL);
  251. maxDegree = 0;
  252. PNode currentPointer = rootWithMinKey->next;
  253. uint currentDegree;
  254. do {
  255. currentDegree = currentPointer->degree;
  256. if (debugRemoveMin) {
  257. cout << " roots starting from currentPointer: ";
  258. currentPointer->printAll(cout);
  259. cout << " checking root " << *currentPointer << " with degree " << currentDegree << endl;
  260. }
  261.  
  262. PNode current = currentPointer;
  263. currentPointer = currentPointer->next;
  264. while (degreeRoots[currentDegree]) { // merge the two roots with the same degree:
  265. PNode other = degreeRoots[currentDegree]; // another root with the same degree
  266. if (current->key() > other->key())
  267. swap(other,current);
  268. // now current->key() <= other->key() - make other a child of current:
  269. other->remove(); // remove from list of roots
  270. current->addChild(other);
  271. if (debugRemoveMin) cout << " added " << *other << " as child of " << *current << endl;
  272. degreeRoots[currentDegree]=NULL;
  273. currentDegree++;
  274. if (currentDegree >= degreeRoots.size())
  275. degreeRoots.push_back((PNode)NULL);
  276. }
  277. // keep the current root as the first of its degree in the degrees array:
  278. degreeRoots[currentDegree] = current;
  279. } while (currentPointer != rootWithMinKey);
  280.  
  281. /// Phase 3: remove the current root, and calcualte the new rootWithMinKey:
  282. delete rootWithMinKey;
  283. rootWithMinKey = NULL;
  284.  
  285. uint newMaxDegree=0;
  286. for (uint d=0; d<degreeRoots.size(); ++d) {
  287. if (debugRemoveMin) cout << " degree " << d << ": ";
  288. if (degreeRoots[d]) {
  289. if (debugRemoveMin) cout << " " << *degreeRoots[d] << endl;
  290. degreeRoots[d]->next = degreeRoots[d]->previous = degreeRoots[d];
  291. insertNode(degreeRoots[d]);
  292. maximize(newMaxDegree,d);
  293. } else {
  294. if (debugRemoveMin) cout << " no node" << endl;
  295. }
  296. }
  297. maxDegree=newMaxDegree;
  298. }
  299.  
  300. void decreaseKey(PNode node, Key newKey) {
  301. if (newKey > node->myKey)
  302. throw string("Trying to decrease key to a greater key");
  303. else if (newKey == node->myKey)
  304. return; // nothing to do
  305.  
  306. if (debug) cout << "decrease key of " << *node << " to " << newKey << endl;
  307. // Update the key and possibly the min key:
  308. node->myKey = newKey;
  309.  
  310. // Check if the new key violates the heap invariant:
  311. PNode parent = node->parent;
  312. if (!parent) { // root node - just make sure the minimum is correct
  313. if (newKey < rootWithMinKey->key())
  314. rootWithMinKey = node;
  315. return; // heap invariant not violated - nothing more to do
  316. } else if (parent->key() <= newKey) {
  317. return; // heap invariant not violated - nothing more to do
  318. }
  319.  
  320. for(;;) {
  321. parent->removeChild(node);
  322. insertNode(node);
  323. if (debugDecreaseKey) {
  324. cout << " removed " << *node << " as child of " << *parent << endl;
  325. cout << " roots after remove: ";
  326. rootWithMinKey->printAll(cout);
  327. }
  328.  
  329. if (!parent->parent) { // parent is a root - nothing more to do
  330. break;
  331. } else if (!parent->mark) { // parent is not a root and is not marked - just mark it
  332. parent->mark = true;
  333. break;
  334. } else {
  335. node = parent;
  336. parent = parent->parent;
  337. continue;
  338. }
  339. };
  340. }
  341.  
  342. void remove(PNode node, Key minusInfinity) {
  343. if (minusInfinity >= minimum()->key())
  344. throw string("2nd argument to remove must be a key that is smaller than all other keys");
  345. decreaseKey(node, minusInfinity);
  346. removeMinimum();
  347. }
  348.  
  349. static int UnitTest() {
  350. try {
  351. typedef FibonacciHeap<string, uint> FibHeap;
  352. FibHeap h;
  353. h.debug=true;
  354. h.debugRemoveMin=false;
  355. h.debugDecreaseKey = false;
  356.  
  357. h.insert("a",4);
  358. h.insert("b",2);
  359. h.insert("c",7);
  360. h.insert("d",5);
  361. h.insert("e",1);
  362. h.insert("f",8);
  363. h.printRoots(cout);
  364.  
  365. while (!h.empty()) {
  366. cout << "min=" << *h.minimum() << endl;
  367. h.removeMinimum();
  368. h.printRoots(cout);
  369. }
  370.  
  371. cout << endl << endl;
  372.  
  373. vector <FibHeap::PNode> nodes(6);
  374. nodes[0] =
  375. h.insert("a",400);
  376. nodes[1] =
  377. h.insert("b",200);
  378. nodes[2] =
  379. h.insert("c",70);
  380. nodes[3] =
  381. h.insert("d",50);
  382. nodes[4] =
  383. h.insert("e",10);
  384. nodes[5] =
  385. h.insert("f",80);
  386. h.printRoots(cout);
  387. cout << "min=" << *h.minimum() << endl;
  388.  
  389. h.removeMinimum();
  390. cout << "min=" << *h.minimum() << endl;
  391. nodes[4]=NULL;
  392. h.printRoots(cout);
  393.  
  394. for (uint i=0; i<nodes.size(); ++i) {
  395. if (!nodes[i]) // minimum - already removed
  396. continue;
  397. h.decreaseKey(nodes[i], nodes[i]->key()/10);
  398. cout << "min=" << *h.minimum() << endl;
  399. h.printRoots(cout);
  400. }
  401.  
  402. cout << endl << endl;
  403.  
  404. h.insert("AA",4);
  405. h.insert("BB",2);
  406. h.insert("CC",7);
  407. h.insert("DD",5);
  408. h.insert("EE",1);
  409. h.insert("FF",8);
  410. h.printRoots(cout);
  411.  
  412. while (!h.empty()) {
  413. cout << "min=" << *h.minimum() << endl;
  414. h.removeMinimum();
  415. h.printRoots(cout);
  416. }
  417.  
  418. cout << endl << endl;
  419. return 0;
  420.  
  421. } catch (string s) {
  422. cerr << endl << "ERROR: " << s << endl;
  423. return 1;
  424. }
  425. }
  426.  
  427. }; // FibonacciHeap
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435. /* ------ PART B: Graph with weighted edges -------- */
  436.  
  437. typedef map<uint,int> AdjacencyListWithWeights; // matches a target vertex to a weight.
  438. const uint MAXINT = 999999999u;
  439.  
  440. template <class Iterator> ostream& print (Iterator iFrom, Iterator iTo) {
  441. for (; iFrom!=iTo; ++iFrom) cout << *iFrom << " ";
  442. return (cout << endl);
  443. }
  444.  
  445. template <class T> ostream& operator<< (ostream& out, const vector<T>& theVector) {
  446. return print (theVector.begin(), theVector.end());
  447. }
  448.  
  449. template <class T> ostream& operator<< (ostream& out, const set<T>& theVector) {
  450. return print (theVector.begin(), theVector.end());
  451. }
  452.  
  453.  
  454.  
  455. /**
  456.  * A graph where the vertices are integers; stores edges in adjacency list.
  457.  */
  458. class GraphWithWeights {
  459. vector<AdjacencyListWithWeights> myVertices;
  460.  
  461. public:
  462. GraphWithWeights (uint nVertices): myVertices(nVertices) {}
  463. uint size() const { return myVertices.size(); }
  464.  
  465. GraphWithWeights& addEdge (uint iFrom, uint iTo, uint weight) {
  466. if (iFrom >= size()) {
  467. throw string("trying to add an edge from a nonexisting vertex index");
  468. }
  469. if (iTo >= size()) {
  470. throw string("trying to add an edge to a nonexisting vertex index");
  471. }
  472. myVertices[iFrom][iTo] = weight;
  473. return *this;
  474. }
  475.  
  476. void createClique() { // for testing
  477. for (uint iFrom=0; iFrom<size(); ++iFrom) {
  478. for (uint iTo=0; iTo<size(); ++iTo) {
  479. if (iFrom!=iTo)
  480. addEdge(iFrom,iTo, (iFrom-iTo)*(iFrom-iTo));
  481. }
  482. }
  483. }
  484.  
  485. void printOutgoingEdges(uint iFrom, ostream& out) const {
  486. out << iFrom << " -> ";
  487. const AdjacencyListWithWeights& targets = myVertices[iFrom];
  488. AdjacencyListWithWeights::const_iterator f;
  489. for (f=targets.begin(); f!=targets.end(); ++f) {
  490. out << " " << f->first << ":" << f->second;
  491. }
  492. out << endl;
  493. }
  494.  
  495. void printIncomingEdges(uint iTo, ostream& out) {
  496. out << iTo << " <- ";
  497. for (uint iFrom=0; iFrom<myVertices.size(); ++iFrom) {
  498. const AdjacencyListWithWeights& currentTargets = myVertices[iFrom];
  499. AdjacencyListWithWeights::const_iterator it = currentTargets.find(iTo);
  500. if (it != currentTargets.end()) {
  501. out << " " << iFrom << ":" << it->second;
  502. }
  503. }
  504. out << endl;
  505. }
  506.  
  507. // Print the graph, using the vertex indices
  508. void printIndices(ostream& out) const {
  509. for (uint i=0; i<size(); ++i)
  510. printOutgoingEdges (i, out);
  511. }
  512.  
  513. friend ostream& operator<< (ostream& out, const GraphWithWeights& g) {
  514. g.printIndices(out);
  515. return out;
  516. }
  517.  
  518.  
  519. /// @see http://w...content-available-to-author-only...t.org/doc/libs/1_44_0/libs/graph/doc/dijkstra_shortest_paths_no_color_map.html
  520. void shortest_paths(uint source, vector<uint>& distances, vector<uint>& previous) const {
  521. typedef FibonacciHeap<uint, uint> VertexQueue;
  522. // Data=vertex, Key=distance from source
  523. VertexQueue vertexQueue;
  524. //vertexQueue.debug = true;
  525.  
  526. distances.resize(size());
  527. previous.resize(size());
  528. vector<VertexQueue::PNode> vertexData(size()); // pointers to the inside the vertex queue, for decrease-key
  529.  
  530. fill(distances.begin(), distances.end(), MAXINT);
  531. fill(previous.begin(), previous.end(), MAXINT);
  532. fill(vertexData.begin(), vertexData.end(), (VertexQueue::PNode)NULL);
  533.  
  534. // Initlaize source:
  535. previous[source] = MAXINT;
  536. distances[source] = 0;
  537. vertexData[source] = vertexQueue.insert(source, 0);
  538.  
  539. // Loop over all vertices:
  540. while (!vertexQueue.empty()) {
  541. VertexQueue::PNode current = vertexQueue.minimum(); // finish a vertex
  542. uint currentVertex = current->data();
  543. uint currentDistance = current->key();
  544. vertexQueue.removeMinimum();
  545.  
  546. // Loop over all outgoing edges of the current vertex:
  547. const AdjacencyListWithWeights& nextTargets = myVertices[currentVertex];
  548. AdjacencyListWithWeights::const_iterator iNeighbour;
  549. for (iNeighbour=nextTargets.begin(); iNeighbour!=nextTargets.end(); ++iNeighbour) {
  550. uint neighbour = iNeighbour->first;
  551. uint distanceFromCurrentToNeighbour = iNeighbour->second;
  552. uint newDistanceToNeighbour = currentDistance + distanceFromCurrentToNeighbour;
  553. //cout << " visiting edge to " << neighbour << " with distance " << distanceFromCurrentToNeighbour
  554. // << " new distance " << newDistanceToNeighbour << " old distance " << distances[neighbour] << endl;
  555. if (newDistanceToNeighbour < distances[neighbour]) { // a tree edge
  556. distances[neighbour] = newDistanceToNeighbour;
  557. previous[neighbour] = currentVertex;
  558. if (vertexData[neighbour]) {
  559. vertexQueue.decreaseKey(vertexData[neighbour], newDistanceToNeighbour);
  560. }
  561. }
  562. if (!vertexData[neighbour]) {
  563. vertexData[neighbour] = vertexQueue.insert(neighbour, newDistanceToNeighbour);
  564. }
  565. }
  566. }
  567. }
  568.  
  569.  
  570. void print_shortest_paths_from(uint iFrom) {
  571. vector<uint> distances, previous;
  572. shortest_paths(iFrom, distances, previous);
  573. cout << "Dijkstra from " << iFrom << ": " << endl << "distances: " << distances << "previouses: " << previous << endl;
  574. }
  575. }; // end of class GraphWithWeights
  576.  
  577.  
  578.  
  579. int main() {
  580.  
  581. /*
  582. GraphWithWeights g(12);
  583. g.addEdge(0,1, 11).addEdge(1,2, 12).addEdge(0,2, 24);
  584. g.addEdge(3,4, 11).addEdge(4,5, 12).addEdge(3,5, 22);
  585. g.addEdge(8,7, 11).addEdge(7,6, 12).addEdge(8,6, 24);
  586. g.addEdge(11,10, 11).addEdge(10,9, 12).addEdge(11,9, 22);
  587. cout << g << endl;
  588. g.printIncomingEdges(0,cout);
  589. g.printIncomingEdges(1,cout);
  590. g.printIncomingEdges(2,cout);
  591. g.printIncomingEdges(3,cout);
  592. g.printIncomingEdges(4,cout);
  593. cout << endl;
  594.  
  595. g.print_shortest_paths_from(0); // shortest distance to 2 should be 23
  596. g.print_shortest_paths_from(3); // shortest distance to 5 should be 22
  597. g.print_shortest_paths_from(8); // shortest distance to 6 should be 23
  598. g.print_shortest_paths_from(11); // shortest distance to 9 should be 23
  599. return 1;
  600. */
  601. cout << "Enter number of nodes: ";
  602. uint nodeCount;
  603. cin >> nodeCount;
  604. GraphWithWeights clique(nodeCount);
  605. clique.createClique();
  606. if (nodeCount<=10)
  607. cout << clique << endl;
  608. //for (uint i=0; i<5; ++i)
  609. // clique.print_shortest_paths_from(i);
  610.  
  611. time_t before = time(NULL);
  612. clique.print_shortest_paths_from(clique.size()/4);
  613. time_t after = time(NULL);
  614. cout << "time for a single calculation = " << (after-before) << " seconds" << endl;
  615.  
  616. }
  617.  
  618.  
Success #stdin #stdout 0.07s 10672KB
stdin
500
stdout
Enter number of nodes: Dijkstra from 125: 
distances: 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 
previouses: 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 999999999 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 

time for a single calculation = 0 seconds