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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <unordered_set>
#include <vector>

#define ar array
#define all(arr) (arr).begin(), (arr).end()
#define range(i, n) for (int i = 0; i < n; ++i)
#define rall(arr) (arr).rbegin(), (arr).rend()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const ll INF = 1e18;
const int INFi = 2e9;
const int maxN = 1e6 + 5;
const int md = 998244353;
const int md2 = 1e9 + 7;

double getTime() { return clock() / (double)CLOCKS_PER_SEC; };

pair<ll, pair<ll, ll>> gcd(ll a, ll b) {
  if (b == 0) {
    return {a, {1, 0}};
  }
  auto p = gcd(b, a % b);
  // a % b = a - (a / b) * b
  // b * k1 + (a - (a / b) * b) * l1 = g
  // b * k1 + a * l1 - (a/b) * b * l1 = g
  // b * (k1 - (a/b) * l1) + a * l1 = g
  return {p.first,
          {p.second.second, p.second.first - (a / b) * p.second.second}};
}

pair<ll, ll> solve_diof(ll a, ll b, ll c) {
  auto p = gcd(abs(a), abs(b));
  if (a < 0)
    p.second.first *= -1;
  if (b < 0)
    p.second.second *= -1;
  if (c % p.first != 0)
    return {INF, INF};
  p.second.first *= c / p.first;
  p.second.second *= c / p.first;
  ll b1 = b / p.first, a1 = a / p.first;
  if (p.second.first < 0) {
    ll k = (-p.second.first) / b1 + 1;
    p.second.first += b1 * k;
    p.second.second -= a1 * k;
  }
  ll k = p.second.first / b1;
  p.second.first -= b1 * k;
  p.second.second += a1 * k;
  return p.second;
}

void solve() {
  ll n;
  cin >> n;
  ll wewe = n;
  if (n == 1) {
    cout << "1\\n";
    return;
  }
  n *= 2;
  ll answer = INF;
  vector<ll> pk, ph;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      pk.emplace_back(1);
      while (n % i == 0) {
        pk.back() *= i;
        n /= i;
      }
      ph.emplace_back(pk.back() - pk.back() / i);
    }
  }
  if (n != 1) {
    pk.emplace_back(n);
    ph.emplace_back(n - 1);
  }
  int s = pk.size();
  if (s == 1) {
    cout << pk.back() - 1 << "\\n";
    return;
  }
  range(mask, 1 << s) {
    vector<ll> a(s);
    ll x1 = 1, x2 = 1;
    range(i, s) if ((1 << i) & mask) { x1 *= pk[i]; }
    else {
      x2 *= pk[i];
    }
    // - x1 * e + x2 * f = 1
    ll cur = solve_diof(-x1, x2, 1).first * x1;
    if (cur == 1 || cur == 0) {
      continue;
    }
    answer = min(answer, cur);
  }
  cout << answer << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // cout << setprecision(15) << fixed;
  int tests = 1;
  // cin >> tests;
  range(_, tests) { solve(); }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <unordered_set>
#include <vector>

#define ar array
#define all(arr) (arr).begin(), (arr).end()
#define range(i, n) for (int i = 0; i < n; ++i)
#define rall(arr) (arr).rbegin(), (arr).rend()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const ll INF = 1e18;
const int INFi = 2e9;
const int maxN = 1e6 + 5;
const int md = 998244353;
const int md2 = 1e9 + 7;

double getTime() { return clock() / (double)CLOCKS_PER_SEC; };

ll binpow(ll a, ll b, ll m) {
  if (b <= 0)
    return 1;
  if (b % 2)
    return (a * binpow(a, b - 1, m)) % m;
  ll mi = binpow(a, b / 2, m);
  return (mi * mi) % m;
}

void solve() {
  ll n = rand() + 5;
  // cin >> n;
  ll wewe = n;
  if (n == 1) {
    cout << "1\\n";
    return;
  }
  n *= 2;
  ll answer = INF;
  vector<ll> pk, ph;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      pk.emplace_back(1);
      while (n % i == 0) {
        pk.back() *= i;
        n /= i;
      }
      ph.emplace_back(pk.back() - pk.back() / i);
    }
  }
  if (n != 1) {
    pk.emplace_back(n);
    ph.emplace_back(n - 1);
  }
  int s = pk.size();
  if (s == 1) {
    cout << pk.back() - 1 << "\\n";
    return;
  }
  vector<vector<ll>> r(s, vector<ll>(s));
  range(i, s - 1) {
    for (int j = i + 1; j < s; ++j) {
      r[i][j] = binpow(pk[i], ph[j] - 1, pk[j]);
    }
  }
  range(mask, 1 << s) {
    vector<ll> a(s);
    range(i, s) if ((1 << i) & mask) a[i] = 1;
    vector<ll> x(s);
    range(i, s) {
      x[i] = a[i];
      range(j, i) {
        x[i] = r[j][i] * (x[i] - x[j]);
        x[i] = x[i] % pk[i];
        if (x[i] < 0)
          x[i] += pk[i];
      }
    }
    ll cur = 0;
    ll ads = 1;
    range(i, s) {
      cur += x[i] * ads;
      ads *= pk[i];
    }
    if (cur == 0 || cur == 1)
      continue;
    answer = min(answer, cur);
  }
  cout << answer - 1 << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // cout << setprecision(15) << fixed;
  int tests = 1e5;
  // cin >> tests;
  range(_, tests) { solve(); }
  return 0;
}