fork(1) download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. int const N = 10000000;
  5. class mypair {
  6. public:
  7. int m, n;
  8. mypair(int m, int n) { this->m = m; this->n = n; }
  9. bool operator<(const mypair &r) const {
  10. return (this->m * N + this->n) < (r.m * N + r.n);
  11. }
  12. };
  13.  
  14. std::map<mypair, int> mpair;
  15. static int c = 0;
  16. static int xmax = 0;
  17. int ack(std::map<mypair, int> &mpair, int m, int n, int x) {
  18. c++;
  19. if (xmax < x)
  20. xmax = x;
  21.  
  22. if (mpair.find(mypair(m, n)) != mpair.end()) {
  23. /* goto label; */
  24. int r;
  25. r = mpair[mypair(m, n)];
  26. return r;
  27. } else {
  28. label:
  29. int r;
  30. if (m == 0)
  31. r = n + 1;
  32. else if (n == 0)
  33. r = ack(mpair, m - 1, 1, x + 1);
  34. else { r = ack(mpair, m - 1, ack(mpair, m, n - 1, x + 1), x + 1); }
  35. mpair[mypair(m, n)] = r;
  36. return r;
  37. }
  38. }
  39.  
  40. int main() {
  41. std::cout << ack(mpair, 4, 1, 0) << std::endl;
  42. std::cout << "calls:" << c << ", nest:" << xmax << std::endl;
  43. return 0;
  44. }
  45. /* end */
  46.  
Success #stdin #stdout 0.1s 9040KB
stdin
Standard input is empty
stdout
65533
calls:196625, nest:16388