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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

void divs(ll &d, ll &v) {
  for (ll i = 2; i <= min(d, v); i++) {
    if (d % i == 0 && v % i == 0) {
      d /= i;
      v /= i;
      i = 1;
    }
  }
}

int main() {

  ll n;
  while (cin >> n && n != 0) {
    ll d, v;
    vector<ll> ds;
    vector<ll> vs;
    bool oo = false;
    for (ll i = 0; i < n; i++) {
      cin >> d >> v;

      divs(d, v);

      ds.push_back(d);
      vs.push_back(v);
    }

    ll z = 1;
    ll nums[11];
    ll allv = 1;

    bool same = true;
    for (ll i = 1; i < n; i++) {
      ll n1 = ds[i - 1] * vs[i];
      ll n2 = ds[i] * vs[i - 1];
      //	ll ret=lcm(n1,n2);
      ll ret = n1 * n2;
      ll r1 = ret / n1;
      ll r2 = ret / n2;
      if (ds[i - 1] == ds[i] && vs[i - 1] == vs[i]) {
        z = lcm(z, 1);
        same = true;
      } else {
        //	if(vs[i-1]!=0)
        //	if(r1*ds[i-1]<vs[i-1]){
        //	z=1;
        //	}
        //	else{
        z = lcm(z, r1 * ds[i - 1] / vs[i - 1]);
        //	}
        //				else
        //					z=1;
        same = false;
      }
    }
    vector<ll> vll;
    ll m;
    if (!same) {
      for (ll i = 0; i < n; i++) {
        // if(ds[i]!=0){
        vll.push_back(z * vs[i] / ds[i]);
        if (i == 0) {
          m = z * vs[i] / ds[i];
        } else {
          m = gcd(m, z * vs[i] / ds[i]);
        }
        ///	}
        //	else{
        //	vll.push_back(1);
        //	m=1;
        //	}
      }
      for (int i = 0; i < n; i++) {
        cout << vll[i] / m << endl;
      }
    } else {
      for (int i = 0; i < n; i++) {
        cout << 1 << endl;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

void divs(ll &d, ll &v) {
  for (ll i = 2; i <= min(d, v); i++) {
    if (d % i == 0 && v % i == 0) {
      d /= i;
      v /= i;
      i = 1;
    }
  }
}

int main() {

  ll n;
  while (cin >> n && n != 0) {
    ll d, v;
    vector<ll> ds;
    vector<ll> vs;
    bool oo = false;
    for (ll i = 0; i < n; i++) {
      cin >> d >> v;

      divs(d, v);

      ds.push_back(d);
      vs.push_back(v);
    }

    ll z = 1;
    ll nums[11];
    ll allv = 1;

    bool same = true;
    for (ll i = 1; i < n; i++) {
      ll n1 = ds[i - 1] * vs[i];
      ll n2 = ds[i] * vs[i - 1];
      ll ret = lcm(n1, n2);
      ll r1 = ret / n1;
      ll r2 = ret / n2;
      if (ds[i - 1] == ds[i] && vs[i - 1] == vs[i]) {
        z = lcm(z, 1);
        same = true;
      } else {
        //	if(vs[i-1]!=0)
        z = lcm(z, r1 * ds[i - 1] / vs[i - 1]);
        //				else
        //					z=1;
        same = false;
      }
    }
    vector<ll> vll;
    ll m;
    if (!same) {
      for (ll i = 0; i < n; i++) {
        // if(ds[i]!=0){
        vll.push_back(z * vs[i] / ds[i]);
        if (i == 0) {
          m = z * vs[i] / ds[i];
        } else {
          m = gcd(m, z * vs[i] / ds[i]);
        }
        ///	}
        //	else{
        //	vll.push_back(1);
        //	m=1;
        //	}
      }
      for (int i = 0; i < n; i++) {
        cout << vll[i] / m << endl;
      }
    } else {
      for (int i = 0; i < n; i++) {
        cout << 1 << endl;
      }
    }
  }

  return 0;
}