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


#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c)                                                                \\
  {                                                                            \\
    for (auto it = c.begin(); it != c.end(); ++it)                             \\
      if (it == c.begin())                                                     \\
        cout << *it;                                                           \\
      else                                                                     \\
        cout << ' ' << *it;                                                    \\
    cout << endl;                                                              \\
  }
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  LL X;
  cin >> X;

  vector<LL> factors;
  factors.push_back(1);
  for (LL f = 2; f * f <= X; ++f) {
    if (X % f == 0) {
      factors.push_back(f);
    }
  }

  bool done = false;
  int i = 0;
  while (!done) {
    for (int f : factors) {
      LL B = i;
      LL A = B + f;
      // cout << "+ " << A << ' ' << B << " => " << (A*A*A*A*A-B*B*B*B*B) <<
      // endl;
      if (A * A * A * A * A - B * B * B * B * B == X) {
        cout << A << ' ' << B << endl;
        done = true;
      }
      if (!done && i) {
        LL B = -i;
        LL A = B + f;
        // cout << "- " << A << ' ' << B << " => " << (A*A*A*A*A-B*B*B*B*B) <<
        // endl;
        if (A * A * A * A * A - B * B * B * B * B == X) {
          cout << A << ' ' << B << endl;
          done = true;
        }
      }
      if (done) {
        break;
      }
    }
    ++i;
  }

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c)                                                                \\
  {                                                                            \\
    for (auto it = c.begin(); it != c.end(); ++it)                             \\
      if (it == c.begin())                                                     \\
        cout << *it;                                                           \\
      else                                                                     \\
        cout << ' ' << *it;                                                    \\
    cout << endl;                                                              \\
  }
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  LL X;
  cin >> X;

  LL f = 2;
  if (X == 1) {
    f = 1;
  } else {
    while (X % f != 0)
      ++f;
  }

  //! A-B = f => A = B+f
  bool done = false;
  int i = 0;
  while (!done) {
    LL B = i;
    LL A = B + f;
    // cout << "+ " << A << ' ' << B << " => " << (A*A*A*A*A-B*B*B*B*B) << endl;
    if (A * A * A * A * A - B * B * B * B * B == X) {
      cout << A << ' ' << B << endl;
      done = true;
    }
    if (!done && i) {
      LL B = -i;
      LL A = B + f;
      // cout << "- " << A << ' ' << B << " => " << (A*A*A*A*A-B*B*B*B*B) <<
      // endl;
      if (A * A * A * A * A - B * B * B * B * B == X) {
        cout << A << ' ' << B << endl;
        done = true;
      }
    }
    ++i;
  }

  return 0;
}