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

#include <bits/stdc++.h>
#define LL long long
#define LD long double
using namespace std;

inline LL mul(LL x, LL y, LL p) {
  static LL r;
  r = x * y - p * (LL)((LD)x * y / p);
  if (r >= p)
    r -= p;
  else if (r < 0)
    r += p;
  return r;
}
inline LL power(LL x, LL y, LL p) {
  static LL r;
  r = 1;
  while (y) {
    if (y & 1)
      r = mul(r, x, p);
    x = mul(x, x, p), y >>= 1;
  }
  return r;
}
inline LL gcd(LL a, LL b) {
  return b ? gcd(b, a % b) : a;
  /*
  #define ctz __builtin_ctzll
  if (!a || !b) return a | b;
  static int t; t = ctz(a|b),a >>= ctz(a);
  while (a ^ b){ if (a < b) swap(a,b); a -= b,a >>= ctz(a); }
  return a << t;
  #undef ctz*/
}
const int chkp[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, len = 10;
inline bool Miller_Rabin(LL n) {
  if (n < 2)
    return 0;
  if (n == 2 || n == 3 || n == 5)
    return 1;
  if (!(n & 1) || !(n % 3) || !(n % 5))
    return 0;
  static int s, i;
  static LL d, a, t;
  s = 0, d = n - 1;
  while (!(d & 1))
    ++s, d >>= 1;
  for (i = 0; i < len && chkp[i] < n; ++i) {
    a = power(chkp[i], d, n);
    if (a != 1 && a != n - 1) {
      t = s - 1;
      while (t--) {
        a = mul(a, a, n);
        if (a == n - 1)
          break;
      }
      if (a ^ (n - 1))
        return 0;
    }
  }
  return 1;
}
LL t[100], cnt;
inline LL Pollard_solve(LL n) {
  LL x = 0, y = x, t = 1, q = 1, a = (rand() % (n - 1)) + 1;
  for (int k = 2;; k <<= 1, y = x, q = 1) {
    for (int i = 1; i <= k; ++i) {
      x = (mul(x, x, n) + a) % n, q = mul(q, abs(x - y), n);
      if (!(i & 127)) {
        t = gcd(q, n);
        if (t > 1)
          break;
      }
    }
    if (t > 1 || (t = gcd(q, n)) > 1)
      break;
  }
  if (t == n) {
    t = 1;
    while (t == 1)
      t = gcd(abs((x = (mul(x, x, n) + a) % n) - y), n);
  }
  return t;
}
inline void Pollard(LL n) {
  if (n == 1)
    return;
  if (Miller_Rabin(n)) {
    t[++cnt] = n;
    return;
  }
  LL p = n;
  while (p == 1 || p == n)
    p = Pollard_solve(n);
  Pollard(n / p), Pollard(p);
}
inline void divide(LL n) {
  LL i;
  for (i = 2; i <= 100000 && i * i <= n; ++i)
    while (n % i == 0)
      t[++cnt] = i, n /= i;
  Pollard(n);
}

LL n, ans;
inline void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
  if (!b) {
    d = a, x = 1, y = 0;
  } else {
    exgcd(b, a % b, d, y, x), y -= (a / b) * x;
  }
}
inline void solve(LL xxx, LL yyy) {
  if (gcd(xxx, yyy) > 1)
    return; // x * t1 - y * t2 == 1
  LL a, b, c, t, d, x0, y0;
  a = xxx, b = yyy, c = 1;
  exgcd(a, b, d, x0, y0);
  if (x0 <= 0 && y0 > 0) {
    d = (-x0) / b + 1;
    x0 += d * b;
    y0 -= d * a;
  }
  LL mnx, mxy;
  if (x0 < 0)
    t = (-x0) / b, x0 += t * b, y0 -= t * a;
  if (x0 > 0)
    t = (x0 - 1) / b, x0 -= t * b, y0 += t * a;
  while (x0 < 0)
    x0 += b, y0 -= a;
  while (x0 > b)
    x0 -= b, y0 += a;
  if (y0 == 0)
    y0 -= a, x0 += b;
  mnx = x0, mxy = y0;

  if ((LD)mnx * xxx > 0 && (LD)mnx * xxx < ans)
    ans = min(ans, mnx * xxx);
  // x = x0 + t * b;
  // y = y0 - t * a;
  // t \\subset Z
}
int nxt[100];
int CNT;
inline void bl(int dep, LL v) {
  if (dep > cnt) {
    solve(v, n / v);
    ++CNT;
    return;
  }
  if (t[dep] != t[dep - 1])
    bl(nxt[dep], v);
  bl(dep + 1, v * t[dep]);
}
int main() {
  LL i, j;
  cin >> n, n <<= 1;
  // for (i = 1; i <= 10000000; ++i) if (i * (i+1) % n == 0){ cout << i << '\\n';
  // return 0; }
  ans = n;
  divide(n);
  sort(t + 1, t + cnt + 1);
  for (i = 1; i <= cnt; ++i)
    for (nxt[i] = cnt + 1, j = i; j <= cnt; ++j)
      if (t[j] != t[i]) {
        nxt[i] = j;
        break;
      }
  bl(1, 1);
  cout << ans - 1 << '\\n';
  //	cerr << CNT << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define LD long double
using namespace std;

inline LL mul(LL x, LL y, LL p) {
  static LL r;
  r = x * y - p * (LL)((LD)x * y / p);
  if (r >= p)
    r -= p;
  else if (r < 0)
    r += p;
  return r;
}
inline LL power(LL x, LL y, LL p) {
  static LL r;
  r = 1;
  while (y) {
    if (y & 1)
      r = mul(r, x, p);
    x = mul(x, x, p), y >>= 1;
  }
  return r;
}
inline LL gcd(LL a, LL b) {
#define ctz __builtin_ctzll
  if (!a || !b)
    return a | b;
  static int t;
  t = ctz(a | b), a >>= ctz(a);
  while (a ^ b) {
    if (a < b)
      swap(a, b);
    a -= b, a >>= ctz(a);
  }
  return a << t;
#undef ctz
}
const int chkp[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, len = 10;
inline bool Miller_Rabin(LL n) {
  if (n < 2)
    return 0;
  if (n == 2 || n == 3 || n == 5)
    return 1;
  if (!(n & 1) || !(n % 3) || !(n % 5))
    return 0;
  static int s, i;
  static LL d, a, t;
  s = 0, d = n - 1;
  while (!(d & 1))
    ++s, d >>= 1;
  for (i = 0; i < len && chkp[i] < n; ++i) {
    a = power(chkp[i], d, n);
    if (a != 1 && a != n - 1) {
      t = s - 1;
      while (t--) {
        a = mul(a, a, n);
        if (a == n - 1)
          break;
      }
      if (a ^ (n - 1))
        return 0;
    }
  }
  return 1;
}
LL t[100], cnt;
inline LL Pollard_solve(LL n) {
  LL x = 0, y = x, t = 1, q = 1, a = (rand() % (n - 1)) + 1;
  for (int k = 2;; k <<= 1, y = x, q = 1) {
    for (int i = 1; i <= k; ++i) {
      x = (mul(x, x, n) + a) % n, q = mul(q, abs(x - y), n);
      if (!(i & 127)) {
        t = gcd(q, n);
        if (t > 1)
          break;
      }
    }
    if (t > 1 || (t = gcd(q, n)) > 1)
      break;
  }
  if (t == n) {
    t = 1;
    while (t == 1)
      t = gcd(abs((x = (mul(x, x, n) + a) % n) - y), n);
  }
  return t;
}
inline void Pollard(LL n) {
  if (n == 1)
    return;
  if (Miller_Rabin(n)) {
    t[++cnt] = n;
    return;
  }
  LL p = n;
  while (p == 1 || p == n)
    p = Pollard_solve(n);
  Pollard(n / p), Pollard(p);
}
inline void divide(LL n) {
  LL i;
  for (i = 2; i <= 100000 && i * i <= n; ++i)
    while (n % i == 0)
      t[++cnt] = i, n /= i;
  Pollard(n);
}

LL n, ans;
inline void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
  if (!b) {
    d = a, x = 1, y = 0;
  } else {
    exgcd(b, a % b, d, y, x), y -= (a / b) * x;
  }
}
inline void solve(LL xxx, LL yyy) {
  if (gcd(xxx, yyy) > 1)
    return; // x * t1 - y * t2 == 1
  LL a, b, c, t, d, x0, y0;
  a = xxx, b = yyy, c = 1;
  exgcd(a, b, d, x0, y0);
  a /= d, b /= d, c /= d, x0 *= c, y0 *= c;
  if (x0 > 0 && y0 <= 0) {
    d = (-y0) / a + 1;
    y0 += d * a;
    x0 -= d * b;
  } else if (x0 <= 0 && y0 > 0) {
    d = (-x0) / b + 1;
    x0 += d * b;
    y0 -= d * a;
  }
  LL mnx, mxy;
  if (x0 > 0)
    t = (x0 - 1) / b, x0 -= t * b, y0 += t * a;
  while (x0 < 0)
    x0 += b, y0 -= a;
  while (x0 > b)
    x0 -= b, y0 += a;
  if (y0 == 0)
    y0 -= a, x0 += b;
  mnx = x0, mxy = y0;

  if (mnx * xxx > 0)
    ans = min(ans, mnx * xxx); //,cerr << mnx << ' ' << xxx<<'\\n';
                               // cerr << mnx << ' ' <<xxx << '\\n';
  //  x = x0 + t * b;
  //  y = y0 - t * a;
  //  t \\subset Z
}
int nxt[100];
int CNT;
inline void bl(int dep, LL v) {
  if (dep > cnt) {
    solve(v, n / v);
    ++CNT;
    return;
  }
  if (t[dep] != t[dep - 1])
    bl(nxt[dep], v);
  bl(dep + 1, v * t[dep]);
}
int main() {
  LL i, j;
  cin >> n, n <<= 1;
  // for (i = 1; i <= 10000000; ++i) if (i * (i+1) % n == 0){ cout << i << '\\n';
  // return 0; }
  ans = n;
  divide(n);
  sort(t + 1, t + cnt + 1);
  for (i = 1; i <= cnt; ++i)
    for (nxt[i] = cnt + 1, j = i; j <= cnt; ++j)
      if (t[j] != t[i]) {
        nxt[i] = j;
        break;
      }
  bl(1, 1);
  cout << ans - 1 << '\\n';
  //	cerr << CNT << '\\n';
  return 0;
}