alexjercan/bugnet huggingface dataset:
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
long long int a[100 + 2], b[100 + 2], c[100 + 2], d[100 + 2], w[100 + 2], n;

long long int s(int x) {
  long long int ans;
  if (w[x] != -1)
    return w[x];
  else {
    long long int e = a[x] * s(c[x]), f = b[x] * s(d[x]);
    long long int g = e, h = f, gcd;
    while (1) {
      g %= h;
      if (g == 0)
        break;
      h %= g;
      if (h == 0)
        break;
    }
    gcd = e * f / max(g, h);
    ans = gcd / a[x] + gcd / b[x];
  }
  w[x] = ans;
  return ans;
}

int main() {
  while (1) {
    long long int an = 0, aaa;
    scanf("%lld", &n);
    if (n == 0)
      break;
    w[0] = 1;
    for (int i = 1; i <= n; i++) {
      scanf("%lld%lld%lld%lld", &a[i], &b[i], &c[i], &d[i]);
      w[i] = -1;
    }
    for (int i = 1; i <= n; i++) {
      if (w[i] == -1)
        aaa = s(i);
      if (an < w[i])
        an = w[i];
    }
    printf("%lld\\n", an);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int a[100 + 2], b[100 + 2], c[100 + 2], d[100 + 2], w[100 + 2], n;

int s(int x) {
  int ans;
  if (w[x] != -1)
    return w[x];
  else {
    int e = a[x] * s(c[x]), f = b[x] * s(d[x]);
    int g = e, h = f, gcd;
    while (1) {
      g %= h;
      if (g == 0)
        break;
      h %= g;
      if (h == 0)
        break;
    }
    gcd = e * f / max(g, h);
    ans = gcd / a[x] + gcd / b[x];
  }
  w[x] = ans;
  return ans;
}

int main() {
  while (1) {
    int an = 0, aaa;
    scanf("%d", &n);
    if (n == 0)
      break;
    w[0] = 1;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
      w[i] = -1;
    }
    for (int i = 1; i <= n; i++) {
      if (w[i] == -1)
        aaa = s(i);
      if (an < w[i])
        an = w[i];
    }
    printf("%d\\n", an);
  }
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int N;

typedef long long int ll;
typedef pair<ll, ll> P;

const int LEAF = 0;
vector<P> pv;

class T {
public:
  int p, q, r, b;
  int par;
  T() { par = -1; }
};

vector<T> vec;

void init() {
  vec.clear();
  pv.clear();
}

void input() {

  for (int i = 0; i <= N; i++) {
    T t;
    vec.push_back(t);
  }

  for (int i = 1; i <= N; i++) {
    cin >> vec[i].p >> vec[i].q >> vec[i].r >> vec[i].b;
    ll gcd = __gcd(vec[i].p, vec[i].q);
    vec[i].p /= gcd;
    vec[i].q /= gcd;

    vec[vec[i].r].par = i;
    vec[vec[i].b].par = i;
  }
}

void dfs(int now, P p) {

  if (now == LEAF) {
    pv.push_back(p);
    return;
  }

  P np = P(p.first * vec[now].p, p.second * (vec[now].p + vec[now].q));
  ll gcd = __gcd(np.first, np.second);
  np.first /= gcd;
  np.second /= gcd;
  dfs(vec[now].b, np);

  np = P(p.first * vec[now].q, p.second * (vec[now].p + vec[now].q));
  gcd = __gcd(np.first, np.second);
  np.first /= gcd;
  np.second /= gcd;
  dfs(vec[now].r, np);
}

ll LCM(ll a, ll b) { return a / __gcd(a, b) * b; }

void solve() {
  init();
  input();
  int root;
  for (int i = 1; i <= N; i++) {
    if (vec[i].par == -1)
      root = i;
  }

  dfs(root, P(1, 1));

  vector<ll> av;

  for (int i = 0; i < pv.size(); i++) {
    av.push_back(LCM(pv[i].first, pv[i].second) / pv[i].first);
  }

  ll res = 1;
  sort(av.begin(), av.end());
  for (int i = 0; i < av.size(); i++) {
    res = LCM(res, av[i]);
  }
  cout << res << endl;
}

int main() {
  while (cin >> N, N)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int N;

typedef long long int ll;
typedef pair<ll, ll> P;

const int LEAF = 0;
vector<P> pv;

class T {
public:
  int p, q, r, b;
  int par;
  T() { par = -1; }
};

vector<T> vec;

void init() {
  vec.clear();
  pv.clear();
}

void input() {

  for (int i = 0; i <= N; i++) {
    T t;
    vec.push_back(t);
  }

  for (int i = 1; i <= N; i++) {
    cin >> vec[i].p >> vec[i].q >> vec[i].r >> vec[i].b;
    vec[vec[i].r].par = i;
    vec[vec[i].b].par = i;
  }
}

void dfs(int now, P p) {

  if (now == LEAF) {
    pv.push_back(p);
    return;
  }

  dfs(vec[now].r,
      P(p.first * vec[now].p, p.second * (vec[now].p + vec[now].q)));
  dfs(vec[now].b,
      P(p.first * vec[now].q, p.second * (vec[now].p + vec[now].q)));
}

ll LCM(ll a, ll b) { return a * b / __gcd(a, b); }

void solve() {
  init();
  input();
  int root;
  for (int i = 1; i <= N; i++) {
    if (vec[i].par == -1)
      root = i;
  }

  dfs(root, P(1, 1));

  vector<ll> av;

  for (int i = 0; i < pv.size(); i++) {
    av.push_back(LCM(pv[i].first, pv[i].second) / pv[i].first);
  }

  ll res = 1;
  for (int i = 0; i < av.size(); i++) {
    res = LCM(res, av[i]);
  }
  cout << res << endl;
}

int main() {
  while (cin >> N, N)
    solve();
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

struct mobile {
  long long lm, rm;
  long long lc, rc, ll, rl;
  mobile(long long lc, long long rc, long long ll, long long rl)
      : lc(lc), rc(rc), ll(ll), rl(rl) {
    lm = rm = -1;
  }
};

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }

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

int dfs(int pos, vector<mobile> &vm) {
  mobile now = vm[pos];
  now.lm = (now.lc == -1) ? 1 : dfs(now.lc, vm);
  now.rm = (now.rc == -1) ? 1 : dfs(now.rc, vm);
  int g = lcm(now.ll * now.lm, now.rl * now.rm);
  now.rm = g / now.rl;
  now.lm = g / now.ll;
  return now.rm + now.lm;
}

int main() {
  int n, i, p, q, r, b, root;
  while (cin >> n, n) {
    vector<mobile> vm;
    vector<bool> isroot(n + 1, true);
    root = -1;
    rep(i, n) {
      cin >> p >> q >> r >> b;
      r--, b--;
      if (r >= 0)
        isroot[r] = false;
      if (b >= 0)
        isroot[b] = false;
      vm.push_back(mobile(r, b, p, q));
    }

    rep(i, n) if (isroot[i] == true) root = i;
    cout << dfs(root, vm) << endl;
  }
}

Error: -8

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

struct mobile {
  long long lm, rm;
  long long lc, rc, ll, rl;
  mobile(long long lc, long long rc, long long ll, long long rl)
      : lc(lc), rc(rc), ll(ll), rl(rl) {
    lm = rm = -1;
  }
};

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }

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

int dfs(int pos, vector<mobile> &vm) {
  mobile now = vm[pos];
  now.lm = (now.lc == -1) ? 1 : dfs(now.lc, vm);
  now.rm = (now.rc == -1) ? 1 : dfs(now.rc, vm);
  int g = lcm(now.ll * now.lm, now.rl * now.rm);
  now.rm = g / now.rl;
  now.lm = g / now.ll;
  return now.rm + now.lm;
}

int main() {
  int n, i, p, q, r, b, root;
  while (cin >> n, n) {
    vector<mobile> vm;
    vector<bool> isroot(n + 1, true);
    root = -1;
    rep(i, n) {
      cin >> p >> q >> r >> b;
      isroot[r] = isroot[b] = false;
      r--, b--;
      vm.push_back(mobile(r, b, p, q));
    }

    rep(i, n) if (isroot[i] == true) root = i - 1;
    cout << dfs(root, vm) << endl;
  }
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

struct mobile {
  long long lm, rm;
  long long lc, rc, ll, rl;
  mobile(long long lc, long long rc, long long ll, long long rl)
      : lc(lc), rc(rc), ll(ll), rl(rl) {
    lm = rm = -1;
  }
};

long long gcd(long long a, long long b) { return b != 0 ? gcd(b, a % b) : a; }

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

int dfs(int pos, vector<mobile> &vm) {
  mobile now = vm[pos];
  now.lm = (now.lc == -1) ? 1 : dfs(now.lc, vm);
  now.rm = (now.rc == -1) ? 1 : dfs(now.rc, vm);
  int g = lcm(now.ll * now.lm, now.rl * now.rm);
  now.rm = g / now.rl;
  now.lm = g / now.ll;
  return now.rm + now.lm;
}

int main() {
  int n, i, p, q, r, b;
  while (cin >> n, n) {
    vector<mobile> vm;
    vector<bool> isroot(n, true);
    rep(i, n) {
      cin >> p >> q >> r >> b;
      r--, b--;
      if (r >= 0)
        isroot[r] = false;
      if (b >= 0)
        isroot[b] = false;
      vm.push_back(mobile(r, b, p, q));
    }

    int root = -1;
    rep(i, n) if (isroot[i] == true) root = i;

    cout << dfs(root, vm) << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

struct mobile {
  int lm, rm;
  int lc, rc, ll, rl;
  mobile() { ; }
  mobile(int lc, int rc, int ll, int rl) : lc(lc), rc(rc), ll(ll), rl(rl) {
    lm = rm = -1;
  }
};

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }

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

int dfs(int pos, vector<mobile> &vm) {
  // cout<<"now "<<pos<<endl;
  mobile now = vm[pos];
  // cout<<"    "<<now.lc<<" : "<<now.rc<<endl;

  if (now.lc < 0 && now.rc < 0) {
    // cout<<"lets calc"<<endl;
    int g = lcm(now.ll, now.rl);
    now.lm = g / now.ll;
    now.rm = g / now.rl;
    // cout<<pos<<" : "<<now.ll<<" "<<now.rl<<" -> "<<g<<"
    // "<<now.lm+now.rm<<endl;
    return now.lm + now.rm;
  }
  if (now.lc != -1)
    now.lm = dfs(now.lc, vm);
  if (now.rc != -1)
    now.rm = dfs(now.rc, vm);
  if (now.lm == -1)
    now.lm = now.rm * now.rl / now.ll;
  if (now.rm == -1)
    now.rm = now.lm * now.ll / now.rl;
  if (now.lm * now.ll != now.rm * now.rl) {
    // cout<<"unbalance!!"<<endl;
    int g = lcm(now.lm * now.ll, now.rm * now.rl);
    now.lm *= g / (now.lm * now.ll);
    now.rm *= g / (now.rm * now.rl);
    // if(now.lm*now.ll != now.rm*now.rl) cout<<"Nooooooooooooooooooooo"<<endl;
  }
  // cout<<pos<<" "<<now.lm<<" "<<now.rm<<endl;
  return now.rm + now.lm;
}

int main() {
  int n, i, p, q, r, b;
  while (cin >> n, n) {
    vector<mobile> vm;
    vector<bool> isroot(n, true);
    rep(i, n) {
      cin >> p >> q >> r >> b;
      r--, b--;
      if (r >= 0)
        isroot[r] = false;
      if (b >= 0)
        isroot[b] = false;
      vm.push_back(mobile(r, b, p, q));
    }

    int root = -1;
    rep(i, n) if (isroot[i] == true) root = i;

    cout << dfs(root, vm) << endl;
  }
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int x[100][2], w[100][2], n;
bool cal[110];
int COU(int a, int b) {
  while (true) {
    if (a < 2 || b < 2)
      return 1;
    if (a < b) {
      int t = a;
      a = b, b = t;
    }
    a %= b;
    if (!a)
      return b;
  }
}
long long DFS(int q) {
  if (!q)
    return 1;
  q--;
  long long a = DFS(w[q][0]), b = DFS(w[q][1]);
  return (a * b * (x[q][0] + x[q][1])) / COU(a * x[q][0], b * x[q][1]);
}
int main() {
  while (true) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cal[i] = false;
    for (int i = 0; i < n; i++) {
      cin >> x[i][0] >> x[i][1] >> w[i][0] >> w[i][1];
      cal[w[i][0] - 1] = true, cal[w[i][1] - 1] = true;
    }
    for (int i = 0; i < n; i++)
      if (!cal[i])
        cout << DFS(i + 1) << endl;
  }
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n;
int a[100][2], p[100][2];
bool cal[100];
int GCD(int x, int y) {
  while (true) {
    if (y > x) {
      int t = x;
      x = y, y = t;
    }
    x %= y;
    if (x < 2)
      break;
  }
  return min(x, y);
}
int DFS(int x) {
  if (!x)
    return 1;
  x--;
  int b = DFS(p[x][0]), c = DFS(p[x][1]);
  int s = b * a[x][0] * c * a[x][1] / GCD(b * a[x][1], c * a[x][0]);
  int ret = 0;
  if (a[x][0])
    ret += s / a[x][0];
  if (a[x][0])
    ret += s / a[x][1];
  // ret=s/a[x][0]+s/a[x][1];
  return ret;
}
int main() {
  while (true) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cal[i] = true;
    for (int i = 0; i < n; i++) {
      cin >> a[i][0] >> a[i][1];
      int t = GCD(a[i][0], a[i][1]);
      a[i][0] /= t, a[i][1] /= t;
      cin >> p[i][0] >> p[i][1];
      cal[p[i][0] - 1] = false, cal[p[i][1] - 1] = false;
    }
    for (int i = 0; i < n; i++)
      if (cal[i])
        cout << DFS(i + 1) << endl;
  }
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;

struct stick {
  int l_d, r_d;
  int par, l, r;
};

stick s[101];

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

ll dfs(ll st) {
  if (st == 0)
    return 1;
  ll l_w = dfs(s[st].l);
  ll r_w = dfs(s[st].r);
  ll res = (l_w * r_w * s[st].l_d * s[st].r_d) /
           gcd(l_w * s[st].l_d, r_w * s[st].r_d);
  res = res / s[st].l_d + res / s[st].r_d;
  return res;
}

int main(void) {
  int n;

  while (cin >> n && n) {
    memset(s, 0, sizeof s);
    FOR(i, 1, n + 1) { s[i].par = i; }
    FOR(i, 1, n + 1) {
      int a;
      cin >> s[i].l_d >> s[i].r_d >> s[i].l >> s[i].r;
      a = gcd(s[i].l_d, s[i].r_d);
      s[i].l_d /= a;
      s[i].r_d /= a;
      s[s[i].l].par = i;
      s[s[i].r].par = i;
    }
    int start = 1;
    while (s[start].par != start) {
      start = s[start].par;
    }
    cout << dfs(start) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;

struct stick {
  int l_d, r_d;
  int par, l, r;
};

stick s[101];

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int dfs(int st) {
  if (st == 0)
    return 1;
  int l_w = dfs(s[st].l);
  int r_w = dfs(s[st].r);
  // int res = l_w*r_w*(s[st].l_d+s[st].r_d)/gcd(l_w, s[st].l_d)/gcd(r_w,
  // s[st].r_d);
  int res = 0;
  int cnt = 1;
  while (s[st].l_d * cnt % l_w != 0 || s[st].r_d * cnt % r_w != 0)
    cnt++;
  return s[st].l_d * cnt + s[st].r_d * cnt;
}

int main(void) {
  int n;

  while (cin >> n && n) {
    memset(s, 0, sizeof s);
    FOR(i, 1, n + 1) { s[i].par = i; }
    FOR(i, 1, n + 1) {
      int a;
      cin >> s[i].l_d >> s[i].r_d >> s[i].l >> s[i].r;
      a = gcd(s[i].l_d, s[i].r_d);
      s[i].l_d /= a;
      s[i].r_d /= a;
      s[s[i].l].par = i;
      s[s[i].r].par = i;
    }
    int start = 1;
    while (s[start].par != start) {
      start = s[start].par;
    }
    cout << dfs(start) << endl;
  }
}
Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
long long gcd(long long p, long long q) {
  while (q) {
    p %= q;
    swap(p, q);
  }
  return p;
}
long long f(long long a[], long long b[], long long c[], long long d[], int p) {
  long long tm, tp;
  if (p == 0)
    return 1;
  tm = f(a, b, c, d, c[p]);
  tp = f(a, b, c, d, d[p]);
  return (a[p] + b[p]) * tm * tp / gcd(tm * a[p], tp * b[p]);
}
int main() {
  int i;
  int e[101], n;
  long long a[101], b[101], c[101], d[101];
  while (cin >> n && n) {
    memset(e, 0, sizeof(e));
    for (i = 1; i < n + 1; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
      e[c[i]] = 1;
      e[d[i]] = 1;
    }
    for (i = 0; e[i]; i++)
      ;
    cout << f(a, b, c, d, i) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
long long gcd(long long p, long long q) {
  while (q) {
    p %= q;
    swap(p, q);
  }
  return p;
}
long long f(long long a[], long long b[], long long c[], long long d[],
            long long p) {
  if (p == 0)
    return 1;
  return f(a, b, c, d, c[p]) * b[p] * f(a, b, c, d, d[p]) /
             gcd(a[p] * f(a, b, c, d, c[p]), b[p] * f(a, b, c, d, d[p])) +
         a[p] * f(a, b, c, d, c[p]) * f(a, b, c, d, d[p]) /
             gcd(a[p] * f(a, b, c, d, c[p]), b[p] * f(a, b, c, d, d[p]));
  return 0;
}
int main() {
  int i;
  long long a[101], b[101], c[101], d[101], e[101], n;
  while (cin >> n && n) {
    memset(e, 0, sizeof(e));
    for (i = 1; i < n + 1; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
      e[c[i]] = 1;
      e[d[i]] = 1;
    }
    for (i = 0; e[i]; i++)
      ;
    cout << f(a, b, c, d, i) << endl;
  }
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
long long int n, mem[110][5], sum;
long long int F(int n1, int n2) {
  long long int s1, s2;
  s1 = min(n1, n2);
  s2 = max(n1, n2);
  if (s1 == 0)
    return s2;
  return F(s2 % s1, s1);
}
long long int min_w(int n_mobile) {
  long long int n1, n2, lim, longth, s1, s2;
  if (mem[n_mobile][2] == 0)
    n1 = 1;
  else
    n1 = min_w(mem[n_mobile][2] - 1);
  if (mem[n_mobile][3] == 0)
    n2 = 1;
  else
    n2 = min_w(mem[n_mobile][3] - 1);
  longth = F(mem[n_mobile][0], mem[n_mobile][1]);
  // printf("%d %d %d\\n",n_mobile,lim,longth);
  s1 = n1 / F(n1, mem[n_mobile][1] / longth);
  s2 = n2 / F(n2, mem[n_mobile][0] / longth);
  lim = F(min(s1, s2), max(s1, s2));
  return ((s1 * s2) / lim) *
         ((mem[n_mobile][0] / longth) + (mem[n_mobile][1] / longth));
}
int main() {
  while (scanf("%d", &n), n) {
    sum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 4; j++) {
        scanf("%d", &mem[i][j]);
      }
      sum += mem[i][2] + mem[i][3];
    }
    printf("%lld\\n", min_w((n * (n + 1)) / 2 - sum - 1));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int n, mem[110][5], sum;
int F(int n1, int n2) {
  if (n1 == 0)
    return n2;
  return F(n2 % n1, n1);
}
int min_w(int n_mobile) {
  int n1, n2, lim, longth, s1, s2;
  if (mem[n_mobile][2] == 0)
    n1 = 1;
  else
    n1 = min_w(mem[n_mobile][2] - 1);
  if (mem[n_mobile][3] == 0)
    n2 = 1;
  else
    n2 = min_w(mem[n_mobile][3] - 1);
  longth = F(min(mem[n_mobile][0], mem[n_mobile][1]),
             max(mem[n_mobile][0], mem[n_mobile][1]));
  // printf("%d %d %d\\n",n_mobile,lim,longth);
  s1 = n1 / F(n2, mem[n_mobile][0] / longth);
  s2 = n2 / F(n1, mem[n_mobile][1] / longth);
  lim = F(min(s1, s2), max(s1, s2));
  return ((s1 * s2) / lim) *
         ((mem[n_mobile][0] / longth) + (mem[n_mobile][1] / longth));
}
int main() {
  while (scanf("%d", &n), n) {
    sum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 4; j++) {
        scanf("%d", &mem[i][j]);
      }
      sum += mem[i][2] + mem[i][3];
    }
    printf("%d\\n", min_w((n * (n + 1)) / 2 - sum - 1));
  }
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
using namespace std;

int dp[110], K[110], O[110], L[110], R[110];

int gcd(int a, int b) {
  int tmp;
  if (a < b)
    swap(a, b);
  while ((tmp = a % b) != 0) {
    a = b;
    b = tmp;
  }
  return b;
}

int mobi(int bou) {
  if (bou == 0)
    return 1;
  if (dp[bou] != 0)
    return dp[bou];
  int l, r, li, ri, gc2, a, b;
  l = mobi(L[bou]);
  r = mobi(R[bou]);
  li = K[bou];
  ri = O[bou];
  gc2 = gcd(li * l, ri * r);
  b = (li * l) / gc2;
  a = (ri * r) / gc2;
  dp[bou] = (a * l + b * r);
  return dp[bou];
}

int main() {
  int N, k, o, g;
  int res;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    memset(dp, 0, sizeof(dp));
    K[0] = O[0] = L[0] = R[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> k >> o >> L[i] >> R[i];
      g = gcd(k, o);
      K[i] = k / g;
      O[i] = o / g;
    }
    res = 0;
    for (int i = 1; i <= N; i++) {
      res = max(res, mobi(i));
    }
    /*
    for(int i=1;i<=N;i++){
            cout<<i<<" "<<dp[i]<<endl;//
    }
    */
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
using namespace std;

int dp[110], K[110], O[110], L[110], R[110];

int gcd(int a, int b) {
  int tmp;
  if (a < b)
    swap(a, b);
  while ((tmp = a % b) != 0) {
    a = b;
    b = tmp;
  }
  return b;
}

int mobi(int bou) {
  if (bou == 0)
    return 1;
  if (dp[bou] != 0)
    return dp[bou];
  int l, r, li, ri, gc2, a, b;
  l = mobi(L[bou]);
  r = mobi(R[bou]);
  ri = K[bou];
  li = O[bou];
  gc2 = gcd(li * l, ri * r);
  b = li * l / gc2;
  a = ri * r / gc2;
  dp[bou] = (a * l + b * r);
  return dp[bou];
}

int main() {
  int N, k, o, g;
  int res;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    memset(dp, 0, sizeof(dp));
    K[0] = O[0] = L[0] = R[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> k >> o >> L[i] >> R[i];
      g = gcd(k, o);
      K[i] = k / g;
      O[i] = o / g;
    }
    res = 0;
    for (int i = 1; i <= N; i++) {
      res = max(res, mobi(i));
    }
    /*
    for(int i=1;i<=N;i++){
            cout<<i<<" "<<dp[i]<<endl;//
    }
    */
    cout << res << endl;
  }
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int GCD(int a, int b) {
  if (b > a)
    swap(a, b);
  return (b == 0) ? a : GCD(b, a % b);
}
int LCM(int a, int b) { return a / GCD(a, b) * b; }
int l[101], r[101], w1[101], w2[101];
int memo[101];
int dfs(int x) {
  if (x == 0)
    return 1;
  if (memo[x] != -1)
    return memo[x];
  int red = l[x] * dfs(w1[x]);
  int blue = r[x] * dfs(w2[x]);
  int lcm = LCM(red, blue);
  return memo[x] = lcm / l[x] + lcm / r[x];
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    bool flag[101];
    fill(flag, flag + n + 1, true);
    fill(memo, memo + n + 1, -1);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d%d", &l[i], &r[i], &w1[i], &w2[i]);
      flag[w1[i]] = flag[w2[i]] = false;
    }
    for (int i = 1; i <= n; i++) {
      if (flag[i]) {
        printf("%d\\n", dfs(i));
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int GCD(int a, int b) {
  if (b > a)
    swap(a, b);
  return (b == 0) ? a : GCD(b, a % b);
}
int LCM(int a, int b) { return a * b / GCD(a, b); }
int l[101], r[101], w1[101], w2[101];
int dfs(int x) {
  if (x == 0)
    return 1;
  int red = l[x] * dfs(w1[x]);
  int blue = r[x] * dfs(w2[x]);
  int lcm = LCM(red, blue);
  return lcm / l[x] + lcm / r[x];
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    bool flag[101];
    fill(flag, flag + n + 1, true);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d%d", &l[i], &r[i], &w1[i], &w2[i]);
      flag[w1[i]] = flag[w2[i]] = false;
    }
    for (int i = 1; i <= n; i++) {
      if (flag[i]) {
        printf("%d\\n", dfs(i));
        break;
      }
    }
  }
  return 0;
}