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

#include <bits/stdc++.h>

#define int long long
#define fast_io()                                                              \\
  ;                                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define vct vector
#define stk stack
#define qu queue
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define br break
#define con continue
#define re return
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define q(q)                                                                   \\
  int q;                                                                       \\
  cin >> q;                                                                    \\
  while (q--)
#define subs substr
#define er erase

const int pinf = ((int)2e18);
const int ninf = ((int)-2e18);
const int mod = 1000000007;

using namespace std;

int power(int x, int n, int M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return power((x * x) % M, n / 2, M);
  else
    return (x * power((x * x) % M, (n - 1) / 2, M)) % M;
}

int fact(int n, int M) {
  int r = 1;
  loop(i, 0, n) { r = (r * (i + 1)) % M; }
  re r;
}

int cons_freq(int a[], int n, int p, int q) {
  int c = 0;
  for (int i = p; i < n; i++) {
    if (a[i] == q)
      c++;
    else
      br;
  }
  re c;
}

int binary_search(int a[], int low, int high, int key) {
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (a[mid] < key)
      low = mid + 1;
    else if (a[mid] > key)
      high = mid - 1;
    else
      re mid;
  }
  re - 1;
}

bool check_perfect_square(int n) { re(sqrt(n) - floor(sqrt(n)) == 0); }

bool check_prime(int n) {
  if (n < 2)
    re false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      re false;
  }

  re true;
}

bool check_pali(string s) {
  string str = s;
  reverse(str.begin(), str.end());

  re str == s;
}

void dfs(vct<int> adj[], bool vis[], int s) {
  vis[s] = true;
  for (int i = 0; i < adj[s].size(); i++) {
    if (!vis[adj[s][i]])
      dfs(adj, vis, adj[s][i]);
  }
}

void bfs(vct<int> adj[], bool vis[], int s) {
  qu<int> q;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < adj[v].size(); i++) {
      if (!vis[adj[v][i]]) {

        vis[adj[v][i]] = true;
        q.push(adj[v][i]);
      }
    }
  }
}

void dijkstra(vct<pair<int, int>> adj[], int src, bool vis[], int dist[],
              int n) {
  // First is distance from source vertex, second is index of the vertex
  ms<pair<int, int>> s;
  s.insert(make_pair(0, src));
  while (!s.empty()) {
    pair<int, int> p = *s.begin();
    s.erase(s.begin());
    int d = p.fi, v = p.se;
    if (vis[v])
      continue;
    vis[v] = true;
    for (int i = 0; i < adj[v].size(); i++) {
      if (dist[v] + adj[v][i].se < dist[adj[v][i].fi]) {
        dist[adj[v][i].fi] = dist[v] + adj[v][i].se;
        s.insert(mp(dist[adj[v][i].fi], adj[v][i].fi));
      }
    }
  }
}

int Kadane(int a[], int n) {
  int dp[n];
  dp[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = max(dp[i + 1] + a[i], a[i]);
  }
  re *_max(dp, dp + n);
}

int digits(int n) {
  int c = 0;
  while (n) {
    c++;
    n /= 10;
  }
  re c;
}

int setbits(int n) { re __builtin_popcount(n); }

int lcm(int a, int b) { re(a * b) / __gcd(a, b); }

int32_t main() {
  fast_io();

  // FILE *fptr1=freopen("in.txt","r",stdin);
  // FILE *fptr2=freopen("out.txt","w",stdout);

  int n;
  cin >> n;
  for (int a = 1;; a++) {
    for (int b = 0;; b++) {
      if (a * a * a * a * a - b * b * b * b * b == n) {
        cout << a << " " << b;
        return 0;
      }
      if (b == 1000) {

        br;
      }
    }
    if (a == 1000)
      br;
  }
  for (int a = 1;; a++) {
    for (int b = 0;; b--) {
      if (a * a * a * a * a - b * b * b * b * b == n) {
        cout << a << " " << b;
        return 0;
      }
      if (b == -1000)
        br;
    }
    if (a == 1000)
      br;
  }
  re 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define fast_io()                                                              \\
  ;                                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define vct vector
#define stk stack
#define qu queue
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define br break
#define con continue
#define re return
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define q(q)                                                                   \\
  int q;                                                                       \\
  cin >> q;                                                                    \\
  while (q--)
#define subs substr
#define er erase

const int pinf = ((int)2e18);
const int ninf = ((int)-2e18);
const int mod = 1000000007;

using namespace std;

int power(int x, int n, int M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return power((x * x) % M, n / 2, M);
  else
    return (x * power((x * x) % M, (n - 1) / 2, M)) % M;
}

int fact(int n, int M) {
  int r = 1;
  loop(i, 0, n) { r = (r * (i + 1)) % M; }
  re r;
}

int cons_freq(int a[], int n, int p, int q) {
  int c = 0;
  for (int i = p; i < n; i++) {
    if (a[i] == q)
      c++;
    else
      br;
  }
  re c;
}

int binary_search(int a[], int low, int high, int key) {
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (a[mid] < key)
      low = mid + 1;
    else if (a[mid] > key)
      high = mid - 1;
    else
      re mid;
  }
  re - 1;
}

bool check_perfect_square(int n) { re(sqrt(n) - floor(sqrt(n)) == 0); }

bool check_prime(int n) {
  if (n < 2)
    re false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      re false;
  }

  re true;
}

bool check_pali(string s) {
  string str = s;
  reverse(str.begin(), str.end());

  re str == s;
}

void dfs(vct<int> adj[], bool vis[], int s) {
  vis[s] = true;
  for (int i = 0; i < adj[s].size(); i++) {
    if (!vis[adj[s][i]])
      dfs(adj, vis, adj[s][i]);
  }
}

void bfs(vct<int> adj[], bool vis[], int s) {
  qu<int> q;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < adj[v].size(); i++) {
      if (!vis[adj[v][i]]) {

        vis[adj[v][i]] = true;
        q.push(adj[v][i]);
      }
    }
  }
}

void dijkstra(vct<pair<int, int>> adj[], int src, bool vis[], int dist[],
              int n) {
  // First is distance from source vertex, second is index of the vertex
  ms<pair<int, int>> s;
  s.insert(make_pair(0, src));
  while (!s.empty()) {
    pair<int, int> p = *s.begin();
    s.erase(s.begin());
    int d = p.fi, v = p.se;
    if (vis[v])
      continue;
    vis[v] = true;
    for (int i = 0; i < adj[v].size(); i++) {
      if (dist[v] + adj[v][i].se < dist[adj[v][i].fi]) {
        dist[adj[v][i].fi] = dist[v] + adj[v][i].se;
        s.insert(mp(dist[adj[v][i].fi], adj[v][i].fi));
      }
    }
  }
}

int Kadane(int a[], int n) {
  int dp[n];
  dp[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = max(dp[i + 1] + a[i], a[i]);
  }
  re *_max(dp, dp + n);
}

int digits(int n) {
  int c = 0;
  while (n) {
    c++;
    n /= 10;
  }
  re c;
}

int setbits(int n) { re __builtin_popcount(n); }

int lcm(int a, int b) { re(a * b) / __gcd(a, b); }

int32_t main() {
  fast_io();

  // FILE *fptr1=freopen("in.txt","r",stdin);
  // FILE *fptr2=freopen("out.txt","w",stdout);

  int n;
  cin >> n;
  for (int a = 0;; a++) {
    for (int b = 0;; b++) {
      if (a * a * a * a * a - b * b * b * b * b == n) {
        cout << a << " " << b;
        return 0;
      }
      if (a * a * a * a * a - b * b * b * b * b < n) {
        br;
      }
    }
    if (a * a * a * a * a > n)
      br;
  }
  for (int a = 0;; a++) {
    for (int b = 0;; b--) {
      if (a * a * a * a * a - b * b * b * b * b == n) {
        cout << a << " " << b;
        return 0;
      }
      if (a * a * a * a * a - b * b * b * b * b > n) {
        br;
      }
    }
  }
  re 0;
}