Problem p02541 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define sqr(x) ((x) * (x))
#define fz1(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define fd1(i, n) for ((i) = (n); (i) >= 1; (i)--)
#define fz0g(i, n) for ((i) = 0; (i) <= (n); (i)++)
#define fd0g(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define fz0k(i, n) for ((i) = 0; (i) < (n); (i)++)
#define fd0k(i, n) for ((i) = (long long)((n)-1); (i) >= 0; (i)--)
#define fz(i, x, y) for ((i) = (x); (i) <= (y); (i)++)
#define fd(i, y, x) for ((i) = (y); (i) >= (x); (i)--)
#define fzin fz1(i, n)
#define fzim fz1(i, m)
#define fzjn fz1(j, n)
#define fzjm fz1(j, m)
#define ff(c, itr)                                                             \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#define pb push_back
#define mk make_pair
#define rdst(st, len)                                                          \\
  {                                                                            \\
    static char ss[len];                                                       \\
    scanf(" %s", ss);                                                          \\
    (st) = ss;                                                                 \\
  }
#define inc(x, y)                                                              \\
  {                                                                            \\
    x += (y);                                                                  \\
    if (x >= mod)                                                              \\
      x -= mod;                                                                \\
  }
#define dec(x, y)                                                              \\
  {                                                                            \\
    x -= (y);                                                                  \\
    if (x < 0)                                                                 \\
      x += mod;                                                                \\
  }
#define spln(i, n) (i == n ? '\\n' : ' ')
#define fac_init(n)                                                            \\
  {                                                                            \\
    fac[0] = fac[1] = inv[1] = fi[0] = fi[1] = 1;                              \\
    fz(i, 2, n) {                                                              \\
      fac[i] = 1ll * fac[i - 1] * i % mod;                                     \\
      inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;                     \\
      fi[i] = 1ll * fi[i - 1] * inv[i] % mod;                                  \\
    }                                                                          \\
  }
using namespace std;
inline void read(int &x) {
  char c;
  int f = 1;
  while (!isdigit(c = getchar()))
    if (c == '-')
      f = -1;
  x = (c & 15);
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
long long n, m, i, j, ans = 3e18;
vector<long long> s;
long long mul(long long x, long long y) {
  if ((x < 0) ^ (y < 0))
    return 0;
  if (ans / x < y)
    return ans + 1;
  return x * y;
}
long long mul(long long x, long long y, long long mod) {
  long long tmp = ((long double)x) * y / mod;
  long long s = (x * y - tmp * mod) % mod;
  if (s < 0)
    s += mod;
  return s;
}
long long exgcd(long long n, long long m, long long &x, long long &y) {
  if (m == 0) {
    x = 1;
    y = 0;
    return n;
  }
  long long ans = exgcd(m, n % m, y, x);
  y = y - n / m * x;
  return ans;
}
long long p[11] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
long long qp(long long x, long long y, long long mod) {
  long long z = 1;
  while (y) {
    if (y & 1)
      z = mul(z, x, mod);
    x = mul(x, x, mod);
    y /= 2;
  }
  return z;
}
long long isp(long long x) {
  if (x < 2)
    return 0;
  long long i, j;
  fz1(i, 10) if (x == p[i]) return 1;
  fz1(i, 10) if (p[i] % x == 0) return 0;
  long long t = x - 1, s = 0;
  while (t % 2 == 0) {
    t /= 2;
    s++;
  }
  fz1(i, 10) {
    long long tt = qp(p[i], t, x);
    if (tt == x - 1 || tt == 1)
      continue;
    fz1(j, s) {
      tt = mul(tt, tt, x);
      if (tt == x - 1)
        break;
    }
    if (tt != x - 1)
      return 0;
  }
  return 1;
}
long long fnd(long long x) {
  if (x % 2 == 0)
    return 2;
  for (;;) {
    long long a = 2, b = 2, c = rand() % x;
    b = (mul(b, b, x) + c) % x;
    while (a != b) {
      long long d = __gcd(x, abs(a - b));
      if (d > 1 && d < x)
        return d;
      a = (mul(a, a, x) + c) % x;
      b = (mul(b, b, x) + c) % x;
      b = (mul(b, b, x) + c) % x;
    }
  }
}
void fjzys(long long x, vector<long long> &s) {
  if (x == 1)
    return;
  if (isp(x)) {
    s.push_back(x);
    return;
  }
  long long ans = fnd(x);
  fjzys(ans, s);
  fjzys(x / ans, s);
}
int main() {
  cin >> n;
  /*	if(n==1){
                  puts("1");
                  return 0;
          }
          if(n==2){
                  puts("3");
                  return 0;
          }*/
  n *= 2;
  fjzys(n, s);
  sort(s.begin(), s.end());
  vector<long long> s2;
  int i, j;
  for (i = 0; i < s.size(); i = j) {
    long long t = 1;
    for (j = i; j < s.size() && s[i] == s[j]; j++) {
      t *= s[j];
    }
    s2.push_back(t);
  }
  fz0k(i, (1 << s2.size())) {
    long long x = 1, y = 1;
    fz0k(j, s2.size()) {
      if ((i >> j) & 1)
        x *= s2[j];
      else
        y *= s2[j];
    }
    if (x == 1) {
      ans = min(ans, y);
      continue;
    }
    long long a, b;
    exgcd(x, y, a, b);
    if (a == 0) {
      a -= y;
      b += x;
    }
    if (b == 0) {
      a += y;
      b -= x;
    }
    ans = min(ans, max(mul(a, x), mul(b, y)));
  }
  cout << ans - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define sqr(x) ((x) * (x))
#define fz1(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define fd1(i, n) for ((i) = (n); (i) >= 1; (i)--)
#define fz0g(i, n) for ((i) = 0; (i) <= (n); (i)++)
#define fd0g(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define fz0k(i, n) for ((i) = 0; (i) < (n); (i)++)
#define fd0k(i, n) for ((i) = (long long)((n)-1); (i) >= 0; (i)--)
#define fz(i, x, y) for ((i) = (x); (i) <= (y); (i)++)
#define fd(i, y, x) for ((i) = (y); (i) >= (x); (i)--)
#define fzin fz1(i, n)
#define fzim fz1(i, m)
#define fzjn fz1(j, n)
#define fzjm fz1(j, m)
#define ff(c, itr)                                                             \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#define pb push_back
#define mk make_pair
#define rdst(st, len)                                                          \\
  {                                                                            \\
    static char ss[len];                                                       \\
    scanf(" %s", ss);                                                          \\
    (st) = ss;                                                                 \\
  }
#define inc(x, y)                                                              \\
  {                                                                            \\
    x += (y);                                                                  \\
    if (x >= mod)                                                              \\
      x -= mod;                                                                \\
  }
#define dec(x, y)                                                              \\
  {                                                                            \\
    x -= (y);                                                                  \\
    if (x < 0)                                                                 \\
      x += mod;                                                                \\
  }
#define spln(i, n) (i == n ? '\\n' : ' ')
#define fac_init(n)                                                            \\
  {                                                                            \\
    fac[0] = fac[1] = inv[1] = fi[0] = fi[1] = 1;                              \\
    fz(i, 2, n) {                                                              \\
      fac[i] = 1ll * fac[i - 1] * i % mod;                                     \\
      inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;                     \\
      fi[i] = 1ll * fi[i - 1] * inv[i] % mod;                                  \\
    }                                                                          \\
  }
using namespace std;
inline void read(int &x) {
  char c;
  int f = 1;
  while (!isdigit(c = getchar()))
    if (c == '-')
      f = -1;
  x = (c & 15);
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
long long n, m, i, j, ans = 3e18;
vector<long long> s;
long long mul(long long x, long long y) {
  if ((x < 0) ^ (y < 0))
    return 0;
  if (ans / x < y)
    return ans + 1;
  return x * y;
}
long long mul(long long x, long long y, long long mod) {
  long long tmp = ((long double)x) * y / mod;
  long long s = (x * y - tmp * mod) % mod;
  if (s < 0)
    s += mod;
  return s;
}
long long exgcd(long long n, long long m, long long &x, long long &y) {
  if (m == 0) {
    x = 1;
    y = 0;
    return n;
  }
  long long ans = exgcd(m, n % m, y, x);
  y = y - n / m * x;
  return ans;
}
long long p[11] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
long long qp(long long x, long long y, long long mod) {
  long long z = 1;
  while (y) {
    if (y & 1)
      z = mul(z, x, mod);
    x = mul(x, x, mod);
    y /= 2;
  }
  return z;
}
long long isp(long long x) {
  if (x < 2)
    return 0;
  long long i, j;
  fz1(i, 10) if (x == p[i]) return 1;
  fz1(i, 10) if (p[i] % x == 0) return 0;
  long long t = x - 1, s = 0;
  while (t % 2 == 0) {
    t /= 2;
    s++;
  }
  fz1(i, 10) {
    long long tt = qp(p[i], t, x);
    if (tt == x - 1 || tt == 1)
      continue;
    fz1(j, s) {
      tt = mul(tt, tt, x);
      if (tt == x - 1)
        break;
    }
    if (tt != x - 1)
      return 0;
  }
  return 1;
}
long long fnd(long long x) {
  if (x % 2 == 0)
    return 2;
  for (;;) {
    long long a = 2, b = 2, c = rand() % x;
    b = (mul(b, b, x) + c) % x;
    while (a != b) {
      long long d = __gcd(x, abs(a - b));
      if (d > 1 && d < x)
        return d;
      a = (mul(a, a, x) + c) % x;
      b = (mul(b, b, x) + c) % x;
      b = (mul(b, b, x) + c) % x;
    }
  }
}
void fjzys(long long x, vector<long long> &s) {
  if (x == 1)
    return;
  if (isp(x)) {
    s.push_back(x);
    return;
  }
  long long ans = fnd(x);
  fjzys(ans, s);
  fjzys(x / ans, s);
}
int main() {
  srand(time(NULL));
  cin >> n;
  /*	if(n==1){
                  puts("1");
                  return 0;
          }
          if(n==2){
                  puts("3");
                  return 0;
          }*/
  n *= 2;
  fjzys(n, s);
  vector<long long> s2;
  int i, j;
  for (i = 0; i < s.size(); i = j) {
    long long t = 1;
    for (j = i; j < s.size() && s[i] == s[j]; j++) {
      t *= s[j];
    }
    s2.push_back(t);
  }
  fz0k(i, (1 << s2.size())) {
    long long x = 1, y = 1;
    fz0k(j, s2.size()) {
      if ((i >> j) & 1)
        x *= s2[j];
      else
        y *= s2[j];
    }
    if (x == 1) {
      ans = min(ans, y);
      continue;
    }
    if (y == 1) {
      ans = min(ans, x);
      continue;
    }
    long long a, b;
    exgcd(x, y, a, b);
    ans = min(ans, max(mul(a, x), mul(b, y)));
  }
  cout << ans - 1 << endl;
  return 0;
}