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

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

long long ext_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  long long x2, y2;
  long long d = ext_gcd(b, a % b, x2, y2);
  x = y2;
  y = x2 - (a / b) * y2;
  return d;
}

int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  ll m = n;
  vector<ll> v;
  for (ll i = 2; i * i <= n; i++) {
    if (m % i == 0) {
      ll s = 1;
      while (m % i == 0) {
        m /= i;
        s *= i;
      }
      v.push_back(s);
    }
  }
  if (m > 1)
    v.push_back(m);
  int k = v.size();
  ll ans = n - 1;
  if (v[0] % 2 == 0) {
    ans = n * 2 - 1;
  }
  for (int i = 0; i < (1 << k); i++) {
    ll l = 1, r = 1;
    for (int j = 0; j < k; j++) {
      if ((i >> j) & 1)
        l *= v[j];
      else
        r *= v[j];
    }
    if (l > r) {
      if (l % 2 == 0)
        l *= 2;
      if (r % 2 == 0)
        r *= 2;
      ll x = 1, y = 0;
      ext_gcd(l, r, x, y);
      x = abs(x);
      y = abs(y);
      if (x == 0 || y == 0) {
        x += r;
        y += l;
      }
      ans = min(ans, min(l * x, r * y));
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  ll m = n;
  ll l = 1;
  vector<int> v;
  for (ll i = 2; i * i <= n; i++) {
    if (m % i == 0) {
      ll s = 1;
      while (m % i == 0) {
        m /= i;
        s *= i;
        v.push_back(i);
      }
      l = max(l, s);
    }
  }
  l = max(l, m);
  for (int i = (int)v.size() - 1; i >= 0; i--) {
    if (l % v[i] == 0)
      v.erase(v.begin() + i);
  }
  if (l == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (l == 2) {
    cout << 3 << endl;
    return 0;
  }
  if (l % 2 == 0) {
    for (ll i = 2; i <= n; i += 2) {
      ll a, b;
      bool f;
      a = i / 2, b = i * l - 1;
      f = true;
      for (int j : v) {
        if (a % j == 0)
          a /= j;
        else if (b % j == 0)
          b /= j;
        else {
          f = false;
          break;
        }
      }
      if (f) {
        cout << i * l - 1 << endl;
        return 0;
      }
      a = i / 2, b = i * l + 1;
      f = true;
      for (int j : v) {
        if (a % j == 0)
          a /= j;
        else if (b % j == 0)
          b /= j;
        else {
          f = false;
          break;
        }
      }
      if (f) {
        cout << i * l << endl;
        return 0;
      }
    }
  } else {
    for (ll i = 1; i <= n; i++) {
      ll a, b;
      bool f;
      if (i % 2) {
        a = i, b = (i * l - 1) / 2;
      } else {
        a = i / 2, b = i * l - 1;
      }
      f = true;
      for (int j : v) {
        if (a % j == 0)
          a /= j;
        else if (b % j == 0)
          b /= j;
        else {
          f = false;
          break;
        }
      }
      if (f) {
        cout << i * l - 1 << endl;
        return 0;
      }
      if (i % 2) {
        a = i, b = (i * l + 1) / 2;
      } else {
        a = i / 2, b = i * l + 1;
      }
      f = true;
      for (int j : v) {
        if (a % j == 0)
          a /= j;
        else if (b % j == 0)
          b /= j;
        else {
          f = false;
          break;
        }
      }
      if (f) {
        cout << i * l << endl;
        return 0;
      }
    }
  }
}