fork download
  1. #include <iostream>
  2. #include <tuple>
  3. #include <map>
  4. #include <cstdint>
  5.  
  6. using Tiii = std::tuple<int, int, int>;
  7. using Mti = std::map<Tiii, int>;
  8.  
  9. Mti mti;
  10. std::uint64_t count = 0;
  11.  
  12. int Tarai(int x, int y, int z)
  13. {
  14. count++;
  15.  
  16. if (x <= y)
  17. return y;
  18. else {
  19. auto tp = std::make_tuple(x, y, z);
  20. auto tiiip = mti.find(tp);
  21. if (tiiip != mti.end())
  22. return tiiip->second;
  23. else {
  24. int xx = mti[std::make_tuple(x - 1, y, z)] = Tarai(x - 1, y, z);
  25. int yy = mti[std::make_tuple(y - 1, z, x)] = Tarai(y - 1, z, x);
  26. int zz = mti[std::make_tuple(z - 1, x, y)] = Tarai(z - 1, x, y);
  27. return mti[std::make_tuple(xx, yy, zz)] = Tarai(xx, yy, zz);
  28. }
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. int x = 100, y = 50, z = 0;
  35.  
  36. std::cout << "Tarai(" << x << "," << y << "," << z <<") = " << Tarai(x, y, z) << std::endl;
  37. std::cout << "Tarai() called " << count << " time(s)." << std::endl;
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
Tarai(100,50,0) = 0
Tarai() called 5 time(s).