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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
#define VREP(it, v)                                                            \\
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;

template <typename T>
using pbds_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename K, typename V>
using pbds_map =
    tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
ll n;
map<ll, ll> m; // maps primes to its power
ll arr[14];

ll power(ll a, ll b, ll m) {
  a %= m;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll half = power(a, b / 2, m);
  half *= half;
  half %= m;
  if (b % 2 == 1)
    half *= a;
  return half % m;
}

ll inv(ll a, ll m) {
  // cout << "inv " << a << " " << m << endl;
  ll m0 = m, t, q;
  ll x0 = 0, x1 = 1;

  if (m == 1) {
    // cout << "done" << endl;
    return 0;
  }

  while (a > 1) {
    // cout << "m : " << m << endl;
    q = a / m;
    t = m;
    m = a % m, a = t;
    t = x0;
    x0 = x1 - q * x0;
    x1 = t;
  }
  if (x1 < 0)
    x1 += m0;
  // cout << "done" << endl;
  return x1;
}

ll qmult(ll a, ll b, ll m) {
  // cout << a << " " << b << " " << m << endl;
  a %= m;
  b %= m;
  if (a < b)
    swap(a, b);
  if (b == 0)
    return 0;
  if (b == 1)
    return a;
  ll half = qmult(a, b / 2, m);
  half += half;
  half %= m;
  if (b % 2 == 1)
    half += a;
  return half % m;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  ll ans = 2 * n - 1;
  if (n % 2 == 0)
    n *= 2;
  // cout << n << endl;
  while (n % 2 == 0) {
    n /= 2;
    if (m.find(2) != m.end())
      m[2]++;
    else
      m[2] = 1;
  }
  for (ll i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      n /= i;
      if (m.find(i) != m.end())
        m[i]++;
      else
        m[i] = 1;
    }
  }
  if (n != 1)
    m[n] = 1;
  ll tot = m.size();
  ll c = 0;
  ll prod = 1;
  for (map<ll, ll>::iterator it = m.begin(); it != m.end(); it++) {
    arr[c] = 1;
    REP(i, 0, it->second) arr[c] *= (it->first);
    prod *= arr[c];
    c++;
  }

  for (ll i = 0; i < (1 << tot); i++) {
    // cout << "up" << endl;
    ll x = 1;
    ll y = 1;
    for (ll j = 0; j < tot; j++) {
      if ((i & (1 << j)) > 0)
        x *= arr[j];
      else
        y *= arr[j];
    }
    // cout << x << " " << y << endl;
    if (y == 1) {
      ans = min(ans, x);
      continue;
    }

    ans =
        min(ans, (qmult(qmult(y - 1, prod / y, prod), inv(prod / y, y), prod)));
    /*
    if (y >= x){
            for (ll j = 1; j <= (x + 1); j++){
                    if ((j*y - 1) == 0) continue;
                    if ((j*y - 1)%x == 0){
                            ans = min(ans, j*y - 1);
                            break;
                    }
            }
    } else {
            for (ll j = 1; j <= y; j++){
                    if ((j*x + 1)%y == 0){
                            ans = min(ans, j*x);
                            break;
                    }
            }
    }*/
  }
  cout << ans << endl;
}

// cout.flush()
// cout << fixed << setprecision(9) << x << endl;

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
#define VREP(it, v)                                                            \\
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;

template <typename T>
using pbds_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename K, typename V>
using pbds_map =
    tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
ll n;
map<ll, ll> m; // maps primes to its power
ll arr[14];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  ll ans = 2 * n - 1;
  if (n % 2 == 0)
    n *= 2;
  // cout << n << endl;
  while (n % 2 == 0) {
    n /= 2;
    if (m.find(2) != m.end())
      m[2]++;
    else
      m[2] = 1;
  }
  for (ll i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      n /= i;
      if (m.find(i) != m.end())
        m[i]++;
      else
        m[i] = 1;
    }
  }
  if (n != 1)
    m[n] = 1;
  ll tot = m.size();
  ll c = 0;
  for (map<ll, ll>::iterator it = m.begin(); it != m.end(); it++) {
    arr[c] = 1;
    REP(i, 0, it->second) arr[c] *= (it->first);
    c++;
  }

  for (ll i = 0; i < (1 << tot); i++) {
    ll x = 1;
    ll y = 1;
    for (ll j = 0; j < tot; j++) {
      if ((i & (1 << j)) > 0)
        x *= arr[j];
      else
        y *= arr[j];
    }
    if (y >= x) {
      for (ll j = 1; j <= (x + 1); j++) {
        if ((j * y - 1) == 0)
          continue;
        if ((j * y - 1) % x == 0) {
          ans = min(ans, j * y - 1);
          break;
        }
      }
    } else {
      for (ll j = 1; j <= y; j++) {
        if ((j * x + 1) % y == 0) {
          ans = min(ans, j * x);
          break;
        }
      }
    }
  }
  cout << ans << endl;
}

// cout.flush()
// cout << fixed << setprecision(9) << x << endl;