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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// a x + b y = gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

long long InvMod(long long a, long long mod) {
  long long x, y;
  if (extgcd(a, mod, x, y) == 1) {
    return (x + mod) % mod;
  }
  return 0;
}

pair<ll, ll> LinearCongruence(const vector<ll> &A, const vector<ll> &B,
                              const vector<ll> &M) {
  ll x = 0;
  ll m = 1;
  for (ll i = 0; i < (ll)A.size(); i++) {
    ll a = A[i] * m;
    ll b = B[i] - A[i] * x;
    ll d = gcd(M[i], a);
    if (b % d != 0) {
      return make_pair(0, -1);
    }
    ll t = b / d * InvMod(a / d, M[i] / d) % (M[i] / d);
    x = x + m * t;
    m *= M[i] / d;
  }
  x %= m;
  if (x < 0) {
    x += m;
  }
  return make_pair(x % m, m);
}

ll solve(ll l1, ll l2, ll r1, ll r2, ll m1, ll m2) {
  vector<ll> a, b, m;
  if (l1 == 0 && r1 != 0) {
    return -1;
  }
  if (l2 == 0 && r2 != 0) {
    return -1;
  }

  if (l1 != 0) {
    a.push_back(l1);
    b.push_back(r1);
    m.push_back(m1);
  }
  if (l2 != 0) {
    a.push_back(l2);
    b.push_back(r2);
    m.push_back(m2);
  }
  if (a.size() == 0) {
    return 1;
  }
  pair<ll, ll> nret = LinearCongruence(a, b, m);
  if (nret.second > 0) {
    if (nret.first == 0) {
      nret.first += nret.second;
    }
    return nret.first;
  }
  return -1;
}

ll n, m, a, b, c, d;
int main() {
  while (scanf("%lld %lld %lld %lld %lld %lld", &m, &n, &a, &b, &c, &d) > 0) {
    a %= m;
    c %= m;
    b %= n;
    d %= n;
    ll e = 0;
    if (a == 0 && b == 0) {
      swap(a, c);
      swap(b, d);
    }
    if (a == 0 && b == 0) {
      puts("0");
      continue;
    } else if (a == 0) {
      e = n / gcd(b, n);
    } else if (b == 0) {
      e = m / gcd(a, m);
    } else {
      e = lcm(m / gcd(a, m), n / gcd(b, n));
    }
    if (c == 0 && d == 0) {
      printf("%lld\\n", e - 1);
      continue;
    }

    ll sqnm = sqrt(n * m) + EPS;
    ll f = 1LL << 60;
    if (e <= sqrt(n * m)) {
      REP(g, e) {
        ll v = solve(c, d, g * a % m, g * b % n, m, n);
        if (v == -1) {
          continue;
        }
        assert(v > 0);
        f = min(f, v);
      }
    } else {
      f = 1;
      FOREQ(i, 1, sqnm + 5) {
        ll v = solve(a, b, i * c % m, i * d % n, m, n);
        if (v == -1) {
          continue;
        }
        f = i;
        break;
      }
    }
    printf("%lld\\n", e * f - 1);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// a x + b y = gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

long long InvMod(long long a, long long mod) {
  long long x, y;
  if (extgcd(a, mod, x, y) == 1) {
    return (x + mod) % mod;
  }
  return 0;
}

pair<ll, ll> LinearCongruence(const vector<ll> &A, const vector<ll> &B,
                              const vector<ll> &M) {
  ll x = 0;
  ll m = 1;
  for (ll i = 0; i < (ll)A.size(); i++) {
    ll a = A[i] * m;
    ll b = B[i] - A[i] * x;
    ll d = gcd(M[i], a);
    if (b % d != 0) {
      return make_pair(0, -1);
    }
    ll t = b / d * InvMod(a / d, M[i] / d) % (M[i] / d);
    x = x + m * t;
    m *= M[i] / d;
  }
  return make_pair(x % m, m);
}

ll solve(ll l1, ll l2, ll r1, ll r2, ll m1, ll m2) {
  vector<ll> a, b, m;
  if (l1 == 0 && r1 != 0) {
    return -1;
  }
  if (l2 == 0 && r2 != 0) {
    return -1;
  }

  if (l1 != 0) {
    a.push_back(l1);
    b.push_back(r1);
    m.push_back(m1);
  }
  if (l2 != 0) {
    a.push_back(l2);
    b.push_back(r2);
    m.push_back(m2);
  }
  if (a.size() == 0) {
    return 1;
  }
  pair<ll, ll> nret = LinearCongruence(a, b, m);
  if (nret.first == 0) {
    nret.first += nret.second;
  }
  if (nret.second > 0) {
    return nret.first;
  }
  return -1;
}

ll n, m, a, b, c, d;
int main() {
  while (scanf("%lld %lld %lld %lld %lld %lld", &m, &n, &a, &b, &c, &d) > 0) {
    a %= m;
    c %= m;
    b %= n;
    d %= n;
    ll e = 0;
    if (a == 0 && b == 0) {
      swap(a, c);
      swap(b, d);
    }
    if (a == 0 && b == 0) {
      puts("0");
      continue;
    } else if (a == 0) {
      e = n / gcd(b, n);
    } else if (b == 0) {
      e = m / gcd(a, m);
    } else {
      e = lcm(m / gcd(a, m), n / gcd(b, n));
    }
    if (c == 0 && d == 0) {
      printf("%lld\\n", e - 1);
      continue;
    }

    ll sqnm = sqrt(n * m) + EPS;
    ll f = 1LL << 60;
    if (e <= sqrt(n * m)) {
      REP(g, e) {
        ll v = solve(c, d, g * a % m, g * b % n, m, n);
        if (v == -1) {
          continue;
        }
        assert(v > 0);
        f = min(f, v);
      }
    } else {
      f = 1;
      FOREQ(i, 1, sqnm + 5) {
        ll v = solve(a, b, i * c % m, i * d % n, m, n);
        if (v == -1) {
          continue;
        }
        f = i;
        break;
      }
    }
    printf("%lld\\n", e * f - 1);
  }
}