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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const double eps = 1e-7;
const int MOD = 1000000007;
const int MOD_MAX = 200000;

using ll = long long;
using vi = vector<int>;
using vi2 = vector<vi>;
using vi3 = vector<vi2>;
using vll = vector<ll>;
using vll2 = vector<vll>;
using vll3 = vector<vll2>;
using vd = vector<double>;
using vd2 = vector<vd>;
using vd3 = vector<vd2>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vpi = vector<pi>;
using vpll = vector<pll>;

#define loopi(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define loop(i, n) for (int i = 0; i < (int)n; i++)
#define loopeq(i, n) for (int i = 0; i <= (int)n; i++)
#define loopll(i, n) for (ll i = 0; i < (ll)n; i++)
#define looplleq(i, n) for (ll i = 0; i <= (ll)n; i++)
#define rloopi(i, a, b) for (int i = (int)a - 1; i >= (int)b; i--)
#define rloop(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define rloopeq(i, n) for (int i = (int)n - 1; i >= 1; i--)
#define rloopll(i, n) for (int i = (ll)n - 1; i >= 0; i--)
#define rlooplleq(i, n) for (int i = (ll)n - 1; i >= 1; i--)
#define all(a) (a).begin(), (a).end()

bool feq(float x, float y) { return abs(x - y) <= eps; }
bool deq(double x, double y) { return abs(x - y) <= eps; }
int ceillog2(ll x) { return int(ceil(log2(x))); }
int floorlog2(ll x) { return int(floor(log2(x))); }
template <class T> T reversed(T container) {
  reverse(container.begin(), container.end());
  return container;
}
template <class T> T sort(T container) {
  sort(container.begin(), container.end());
  return container;
}
ll gcd(ll m, ll n) {
  if (m > n)
    swap(m, n);
  if (n == 0)
    return m;
  else
    return gcd(n, m % n);
}
ll lcm(ll m, ll n) { return m * n / gcd(m, n); }

ll euclidean_gcd(ll a, ll b) {
  if (a < b)
    return euclidean_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

ll alg(ll n, ll k) {
  if (n == k)
    cout << 0 << endl;
  // else if (n < k) cout << n << endl;
  else {
    ll r = n % k;
    ll s = k - r;
    if (min(s, r) < k)
      cout << min(s, r) << endl;
    else
      alg(min(s, r), k);
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  alg(n, k);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const double eps = 1e-7;
const int MOD = 1000000007;
const int MOD_MAX = 200000;

using ll = long long;
using vi = vector<int>;
using vi2 = vector<vi>;
using vi3 = vector<vi2>;
using vll = vector<ll>;
using vll2 = vector<vll>;
using vll3 = vector<vll2>;
using vd = vector<double>;
using vd2 = vector<vd>;
using vd3 = vector<vd2>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vpi = vector<pi>;
using vpll = vector<pll>;

#define loopi(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define loop(i, n) for (int i = 0; i < (int)n; i++)
#define loopeq(i, n) for (int i = 0; i <= (int)n; i++)
#define loopll(i, n) for (ll i = 0; i < (ll)n; i++)
#define looplleq(i, n) for (ll i = 0; i <= (ll)n; i++)
#define rloopi(i, a, b) for (int i = (int)a - 1; i >= (int)b; i--)
#define rloop(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define rloopeq(i, n) for (int i = (int)n - 1; i >= 1; i--)
#define rloopll(i, n) for (int i = (ll)n - 1; i >= 0; i--)
#define rlooplleq(i, n) for (int i = (ll)n - 1; i >= 1; i--)
#define all(a) (a).begin(), (a).end()

bool feq(float x, float y) { return abs(x - y) <= eps; }
bool deq(double x, double y) { return abs(x - y) <= eps; }
int ceillog2(ll x) { return int(ceil(log2(x))); }
int floorlog2(ll x) { return int(floor(log2(x))); }
template <class T> T reversed(T container) {
  reverse(container.begin(), container.end());
  return container;
}
template <class T> T sort(T container) {
  sort(container.begin(), container.end());
  return container;
}
ll gcd(ll m, ll n) {
  if (m > n)
    swap(m, n);
  if (n == 0)
    return m;
  else
    return gcd(n, m % n);
}
ll lcm(ll m, ll n) { return m * n / gcd(m, n); }

ll euclidean_gcd(ll a, ll b) {
  if (a < b)
    return euclidean_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  ll n, k;
  cin >> n >> k;
  cout << euclidean_gcd(n, k) << endl;
}