Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <string>
#include <tuple>
namespace math {
std::map<uint64_t, uint32_t> g_tmp;
uint32_t gcd(uint32_t a, uint32_t b) {
  // const auto key = (uint64_t)a | ((uint64_t)b) << 32;
  // const auto end_it = g_tmp.end();
  // auto it = g_tmp.find(key);
  // if (it != end_it) {
  //	return it->second;
  // }
  for (;;) {
    if (a < b) {
      std::swap(a, b);
    }

    {
      if (a == b || b == 0) {
        //					g_tmp.insert(std::make_pair(key,
        //a));
        return a;
      }
      // const uint64_t key2 = (uint64_t)a| ((uint64_t)b)<<32;
      // auto it = g_tmp.find(key2);
      // if (it != end_it){
      //	g_tmp.insert(std::make_pair(key, it->second));
      //	return it->second;
      // }
    }

    int32_t v = a - b;
    const auto tmp = a;
    a = b;
    if (v > 0) {
      b = v;
    } else {
      b = tmp % b;
    }
  }
}

uint32_t gcd(uint32_t a, uint32_t b, uint32_t c) {
  const auto tmp_v = gcd(a, b);
  return tmp_v == c ? c : gcd(tmp_v, c);
}
} // namespace math

int main() {
  uint32_t n = 0;
  std::cin >> n;
  uint64_t result = 0;
  std::map<std::tuple<uint32_t, uint32_t, uint32_t>, uint32_t> vals_tbl;

  for (uint32_t a = 1; a <= n; ++a) {
    for (uint32_t b = 1; b <= n; ++b) {
      for (uint32_t c = 1; c <= n; ++c) {
        uint32_t tmp[]{a, b, c};
        std::sort(std::begin(tmp), std::end(tmp));
        // const auto key = std::make_tuple(tmp[0], tmp[1], tmp[2]);
        // auto it = vals_tbl.find(key);
        // if (it != vals_tbl.end()) {
        //	result += it->second;
        // }
        // else {
        const auto ret = math::gcd(tmp[0], tmp[1], tmp[2]);
        result += ret;
        //	vals_tbl.insert(std::make_pair(key, ret));
        //}
      }
    }
  }
  std::cout << result << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <string>
#include <tuple>
namespace math {
std::map<uint64_t, uint32_t> g_tmp;
uint32_t gcd(uint32_t a, uint32_t b) {
  const auto key = (uint64_t)a | ((uint64_t)b) << 32;
  const auto end_it = g_tmp.end();
  auto it = g_tmp.find(key);
  if (it != end_it) {
    return it->second;
  }
  for (;;) {
    if (a < b) {
      std::swap(a, b);
    }

    {
      if (a == b || b == 0) {
        g_tmp.insert(std::make_pair(key, a));
        return a;
      }
      const uint64_t key2 = (uint64_t)a | ((uint64_t)b) << 32;
      auto it = g_tmp.find(key2);
      if (it != end_it) {
        g_tmp.insert(std::make_pair(key, it->second));
        return it->second;
      }
    }

    int32_t v = a - b;
    const auto tmp = a;
    a = b;
    if (v > 0) {
      b = v;
    } else {
      b = tmp % b;
    }
  }
}

uint32_t gcd(uint32_t a, uint32_t b, uint32_t c) {
  const auto tmp_v = gcd(a, b);
  return tmp_v == c ? c : gcd(tmp_v, c);
}
} // namespace math

int main() {
  uint32_t n = 0;
  std::cin >> n;
  uint64_t result = 0;
  std::map<std::tuple<uint32_t, uint32_t, uint32_t>, uint32_t> vals_tbl;

  for (uint32_t a = 1; a <= n; ++a) {
    for (uint32_t b = 1; b <= n; ++b) {
      for (uint32_t c = 1; c <= n; ++c) {
        uint32_t tmp[]{a, b, c};
        std::sort(std::begin(tmp), std::end(tmp));
        const auto key = std::make_tuple(tmp[0], tmp[1], tmp[2]);
        auto it = vals_tbl.find(key);
        if (it != vals_tbl.end()) {
          result += it->second;
        } else {
          const auto ret = math::gcd(tmp[0], tmp[1], tmp[2]);
          result += ret;
          vals_tbl.insert(std::make_pair(key, ret));
        }
      }
    }
  }
  std::cout << result << std::endl;
  return 0;
}