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

#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define ll long long

using namespace std;

void A();
void B();
void C();
void D();
void E();
void F();

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  D();
}

void A() {
  string s;
  cin >> s;

  cout << (s == "ABC" ? "ARC" : "ABC") << '\\n';
}

void B() {
  int N, K;
  cin >> N >> K;

  set<int> have;

  forn(k, K) {

    int d;
    cin >> d;

    forn(dd, d) {

      int sn;
      cin >> sn;
      have.insert(sn);
    }
  }

  int c = 0;
  forn(i, N) {
    if (have.find(i + 1) == have.end())
      c++;
  }

  cout << c;
}

void C() {

  int N, M;
  cin >> N >> M;

  vector<int> h;
  forn(i, N) {
    int hh;
    cin >> hh;
    h.push_back(hh);
  }

  vector<vector<int>> adj(N);

  forn(m, M) {

    int a, b;
    cin >> a >> b;

    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }

  int r = 0;
  forn(i, N) {

    bool good = true;
    for (int ind : adj[i]) {
      if (h[ind] >= h[i])
        good = false;
    }

    if (good)
      r++;
  }

  cout << r;
}

void D() {
  int X;
  cin >> X;

  vector<ll> dp(1000);

  ll A, B;

  for (int i = -126; i < 126; i++) {

    if (dp[i + 130] == 0) {
      dp[i + 130] = pow(i, 5);
    }
    A = dp[i + 130];

    for (int j = -126; j < 126; j++) {

      if (dp[j + 130] == 0) {
        dp[j + 130] = pow(j, 5);
      }
      B = dp[j + 130];

      if (A - B == X) {
        cout << i << " " << j;
        return;
      }
    }
  }
}

void E() {
  int N;
  cin >> N;

  vector<int> e;
  map<int, int> m;
  map<int, int> m2;
  forn(i, N) {
    int ee;
    cin >> ee;
    e.push_back(ee);
    int dif1 = (i + 1) - ee;
    int dif2 = (i + 1) + ee;
    m[dif1]++;
    m2[dif2]++;
  }

  int count = 0;
  forn(i, N) {

    int ee = e[i];
    int dif1 = (i + 1) - ee;
    int dif2 = (i + 1) + ee;

    count += m2[dif1];
    count += m[dif2];
  }

  cout << count;
}

void F() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {

    int N;
    cin >> N;

    int result = 0;

    cout << "Case #" << t << ": ";

    cout << result << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define ll long long

using namespace std;

void A();
void B();
void C();
void D();
void E();
void F();

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  D();
}

void A() {
  string s;
  cin >> s;

  cout << (s == "ABC" ? "ARC" : "ABC") << '\\n';
}

void B() {
  int N, K;
  cin >> N >> K;

  set<int> have;

  forn(k, K) {

    int d;
    cin >> d;

    forn(dd, d) {

      int sn;
      cin >> sn;
      have.insert(sn);
    }
  }

  int c = 0;
  forn(i, N) {
    if (have.find(i + 1) == have.end())
      c++;
  }

  cout << c;
}

void C() {

  int N, M;
  cin >> N >> M;

  vector<int> h;
  forn(i, N) {
    int hh;
    cin >> hh;
    h.push_back(hh);
  }

  vector<vector<int>> adj(N);

  forn(m, M) {

    int a, b;
    cin >> a >> b;

    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }

  int r = 0;
  forn(i, N) {

    bool good = true;
    for (int ind : adj[i]) {
      if (h[ind] >= h[i])
        good = false;
    }

    if (good)
      r++;
  }

  cout << r;
}

void D() {
  int X;
  cin >> X;

  for (int A = 1;; A++) {
    int A_5 = pow(A, 5);
    int dif = A_5 - X;
    bool neg = dif < 0;
    if (neg)
      dif *= -1;
    double B = pow(dif, .2);
    if (abs(B - (int)B) < 0.0001) {
      cout << A << " " << (neg ? -1 * B : B) << '\\n';
      return;
    }
  }
}

void E() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {

    int N;
    cin >> N;

    int result = 0;

    cout << "Case #" << t << ": ";

    cout << result << endl;
  }
}

void F() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {

    int N;
    cin >> N;

    int result = 0;

    cout << "Case #" << t << ": ";

    cout << result << endl;
  }
}