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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
const int MAX_N = 100010;

ll pow5(ll n) {
  ll res = 1;
  REP(i, 5) { res *= n; }
  return res;
}

int X;

vector<pair<int, int>> bf() {
  vector<pair<int, int>> ret;
  for (int a = -100; a <= 100; ++a) {
    for (int b = -100; b <= 100; ++b) {
      if (pow5(a) - pow5(b) == X) {
        ret.push_back(make_pair(a, b));
      }
    }
  }
  return ret;
}

pair<int, int> solve() {
  for (int a = 1; a <= 1000; ++a) {
    for (int b = -1000; b <= 1000; ++b) {
      if (pow5(a) - pow5(b) == X) {
        return make_pair(a, b);
      }
    }
  }
  return make_pair(1, 1);
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
#if 1
  cin >> X;
  pair<int, int> ans = solve();
  // pair<int, int> ans = bf();
  printf("%lld %lld\\n", ans.first, ans.second);
#else
  for (X = 1; X <= 2000; ++X) {
    auto res = bf();
    if (res.size() > 0) {
      printf("X: %lld\\n", X);
      for (auto &v : res) {
        printf("  %lld %lld\\n", v.first, v.second);
      }
    }
  }
#endif

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
const int MAX_N = 100010;

ll pow5(ll n) {
  ll res = 1;
  REP(i, 5) { res *= n; }
  return res;
}

int X;

vector<pair<int, int>> bf() {
  vector<pair<int, int>> ret;
  for (int a = -100; a <= 100; ++a) {
    for (int b = -100; b <= 100; ++b) {
      if (pow5(a) - pow5(b) == X) {
        ret.push_back(make_pair(a, b));
      }
    }
  }
  return ret;
}

pair<int, int> solve() {
  for (int a = 1; a <= 100; ++a) {
    for (int b = -100; b <= 100; ++b) {
      if (pow5(a) - pow5(b) == X) {
        return make_pair(a, b);
      }
    }
  }
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
#if 1
  cin >> X;
  pair<int, int> ans = solve();
  // pair<int, int> ans = bf();
  printf("%lld %lld\\n", ans.first, ans.second);
#else
  for (X = 1; X <= 2000; ++X) {
    auto res = bf();
    if (res.size() > 0) {
      printf("X: %lld\\n", X);
      for (auto &v : res) {
        printf("  %lld %lld\\n", v.first, v.second);
      }
    }
  }
#endif

  return 0;
}