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



#include <bits/stdc++.h>
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "at line " << __LINE__ << ": [" << #x << "] = [";                    \\
  _print(x)
#else
#define debug(x...)
#endif

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;


template<class T>
using ordered_set= tree<T, null_type,
                less<T>,
                rb_tree_tag, tree_order_statistics_node_update> ;

template<class T>
using ordered_mset= tree<T, null_type,
                less_equal<T>,
                rb_tree_tag, tree_order_statistics_node_update> ;

*/

/*

PBDS
-------------------------------------------------
                        0 based indexing
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // Number of items strictly smaller than value
4) *find_by_order(k) : K-th element in a set (counting from zero)

*/

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll power(ll b) {
  ll ans = 1;
  for (int i = 1; i <= 5; i++)
    ans *= b;

  return ans;
}

void solve() {
  ll n;
  cin >> n;

  if (n == 1) {
    cout << 1 << " " << 0;
    return;
  }

  vector<ll> fact;

  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      fact.push_back(i);
      if (i * i != n)
        fact.push_back(n / i);
    }
  }

  sort(fact.begin(), fact.end());

  for (auto it : fact) {
    for (int i = 0; i < 10000; i++) {
      ll a = i;
      ll b = i - it;

      if (power(a) - power(b) == n) {
        cout << a << " " << b;
        return;
      }

      if (power(b) - power(a) == n) {
        cout << b << " " << a;
        return;
      }
    }
  }
}

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

  solve();

#ifdef LOCAL
  cerr << "\\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\\n";
#endif

  return 0;
}


Error: TLE

Fixed submission:


/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/

#include <bits/stdc++.h>
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "at line " << __LINE__ << ": [" << #x << "] = [";                    \\
  _print(x)
#else
#define debug(x...)
#endif

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;


template<class T>
using ordered_set= tree<T, null_type,
                less<T>,
                rb_tree_tag, tree_order_statistics_node_update> ;

template<class T>
using ordered_mset= tree<T, null_type,
                less_equal<T>,
                rb_tree_tag, tree_order_statistics_node_update> ;

*/

/*

PBDS
-------------------------------------------------
                        0 based indexing
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // Number of items strictly smaller than value
4) *find_by_order(k) : K-th element in a set (counting from zero)

*/

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll power(ll b) {
  ll ans = 1;
  for (int i = 1; i <= 5; i++)
    ans *= b;

  return ans;
}

void solve() {
  ll n;
  cin >> n;

  if (n == 1) {
    cout << 1 << " " << 0;
    return;
  }

  vector<ll> fact;

  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (i != 1)
        fact.push_back(i);
      if (i * i != n)
        fact.push_back(n / i);
    }
  }

  sort(fact.begin(), fact.end());

  ll diff = fact[0];

  for (ll i = 0;; i++) {
    ll a = i;
    ll b = i - diff;

    if (power(a) - power(b) == n) {
      cout << a << " " << b;
      return;
    }

    if (power(b) - power(a) == n) {
      cout << b << " " << a;
      return;
    }
  }
}

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

  solve();

#ifdef LOCAL
  cerr << "\\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\\n";
#endif

  return 0;
}