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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 27;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define repD(i, n) for (int i = n; i >= 0; i--)

ll v[100];
ll d[100];
ll ratio[100];

ll gcd(ll u, ll v) {
  while (u > 0) {
    if (v > u)
      swap(u, v);
    u %= v;
  }
  return v;
}

ll lcm(ll u, ll v) { return u / gcd(u, v) * v; }

int main() {
  int n;
  while (cin >> n && n) {
    rep(i, n) { cin >> d[i] >> v[i]; }
    fill(ratio, ratio + 10, 1);
    rep(i, n) {
      ll b = v[i] * d[0];
      ll a = v[0] * d[i];
      ll c = gcd(a, b);
      a /= c;
      b /= c;
      ll next = lcm(ratio[0], a);
      ll mul = next / ratio[0];
      rep(j, i) ratio[j] *= mul;
      ratio[i] = next / a * b;
    }
    rep(i, n) cout << ratio[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 27;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define repD(i, n) for (int i = n; i >= 0; i--)

ll v[100];
ll d[100];
ll ratio[100];

ll gcd(ll u, ll v) {
  while (u > 0) {
    if (v > u)
      swap(u, v);
    u %= v;
  }
  return v;
}

ll lcm(ll u, ll v) { return u / gcd(u, v) * v; }

int main() {
  int n;
  while (cin >> n && n) {
    ll tv = 1LL;
    rep(i, n) {
      cin >> d[i] >> v[i];
      tv *= v[i];
    }
    rep(i, n) ratio[i] = d[i] * tv / v[i];
    ll c = ratio[0];
    ll m = 1LL;
    rep(i, n) { c = gcd(c, ratio[i]); }
    rep(i, n) ratio[i] /= c;
    rep(i, n) m = lcm(m, ratio[i]);
    rep(i, n) cout << m / ratio[i] << endl;
  }
  return 0;
}