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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC__
typedef __int64 ll;
#else
typedef long long ll;
#endif

typedef pair<ll, ll> pll;

int n;
vector<int> d;
vector<int> s;
bool input() {
  cin >> n;
  if (n == 0)
    return false;
  d = vector<int>(n);
  s = vector<int>(n);
  rep(i, n) { cin >> d[i] >> s[i]; }
  return true;
}

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (1) {
    ll c = a % b;
    if (c == 0)
      return b;
    a = b;
    b = c;
  }
}

void tubun(ll *a, ll *b) {
  ll c = gcd(*a, *b);
  *a /= c;
  *b /= c;
}

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

pll lcmexp(ll a, ll b, ll c, ll d) {
  tubun(&a, &b);
  tubun(&c, &d);

  ll bunbo = b * d;
  ll A = a * d;
  ll B = b * c;

  // ll gcA = gcd(A, bunbo); A/=gcA; bunbo/=gcA;
  // ll gcB = gcd(B, bunbo); B/=gcB; bunbo/=gcB;

  ll C = gcd(A, B);
  ll bunshi = A / C * B / C * C;

  // cout<<"bunbo="<<bunbo<<" bunshi="<<bunshi<<"  A="<<A<<"  B="<<B<<endl;
  tubun(&bunbo, &bunshi);

  return pll(bunshi, bunbo);
}

vector<int> solve() {
  vector<int> ret;

  pll lcme = lcmexp(d[0], s[0], d[1], s[1]);
  reps(i, 2, n) { lcme = lcmexp(lcme.first, lcme.second, d[i], s[i]); }

  // cout<<"aaa"<<lcme.first<<" "<<lcme.second<<endl;

  rep(i, n) { ret.push_back(lcme.first * s[i] / d[i] / lcme.second); }

  return ret;
}

void c_solve() {
  vector<int> ans = solve();
  rep(i, ans.size()) cout << ans[i] << endl;
}

int main() {
  while (input())
    c_solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC__
typedef __int64 ll;
#else
typedef long long ll;
#endif

int n;
vector<int> d;
vector<int> s;
bool input() {
  cin >> n;
  if (n == 0)
    return false;
  d = vector<int>(n);
  s = vector<int>(n);
  rep(i, n) { cin >> d[i] >> s[i]; }
  return true;
}

typedef pair<ll, ll> pll;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);

  while (1) {
    ll c = a % b;

    if (c == 0)
      return b;
    a = b;
    b = c;
  }
  return -1;
}
pll tubun(pll a) {
  ll gc = gcd(a.first, a.second);
  a.first /= gc;
  a.second /= gc;
  return a;
}
ll lcm(ll a, ll b, ll c, ll d) {
  pll tes1 = tubun(pll(a, b));
  pll tes2 = tubun(pll(c, d));

  a = tes1.first;
  b = tes1.second;
  c = tes2.first;
  d = tes2.second;

  ll bunbo = b * d;
  ll A = a * d;
  ll B = b * c;
  ll C = gcd(A, B);
  ll bunshi = A / C * B / C * C;

  while (bunbo > bunshi)
    printf("heee");
  return bunshi / bunbo;
}
vector<int> solve() {
  vector<int> ret;

  rep(i, n) {
    pll hoge = tubun(pll(d[i], s[i]));
    d[i] = hoge.first;
    s[i] = hoge.second;
  }

  ll mygcd = gcd(s[0], s[1]);

  reps(i, 2, n) { mygcd = gcd(mygcd, s[i]); }
  rep(i, n) s[i] /= mygcd;

  ll mylcm = lcm(d[0], s[0], d[1], s[1]);
  reps(i, 2, n) { mylcm = lcm(mylcm, 1, d[i], s[i]); }

  rep(i, n) { ret.push_back((int)(mylcm / d[i] * s[i])); }

  return ret;
}

void c_solve() {
  vector<int> ans = solve();
  rep(i, ans.size()) cout << ans[i] << endl;
}

int main() {
  while (input())
    c_solve();
}