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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
#define F first
#define S second
int n, ans;
ll s;
map<ll, int> m;
int main(void) {
  scanf("%d", &n);
  m.insert(P(ll(0), 0));
  for (int i = 1; i <= n; i++) {
    ll a;
    scanf("%lld", &a);
    s += a;
    if (m.find(s) == m.end())
      m.insert(P(s, i));
    else
      ans = max(ans, i - m[s]);
  }
  printf("%d\\n", ans);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int, int> P;
P d[200003];
int n, ans = 0;
int main(void) {
  cin >> n;
  d[0] = P(0, 0);
  for (int i = 1; i <= n; i++) {
    cin >> d[i].first;
    d[i].first += d[i - 1].first;
    d[i].second = i;
  }
  sort(d, d + n + 1);
  for (int s = 0, t = s + 1; t <= n; s = t + 1, t = s + 1) {
    while (d[s].first == d[t].first) {
      t++;
    }
    t--;
    int k = d[t].second - d[s].second;
    if (k)
      ans = max(ans, k);
  }
  cout << ans << endl;
}
Problem p00324 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  vector<pair<long long, int>> vepa;
  pair<long long, int> pa;
  long long n, t, d, sta, end, max;
  long long sum;
  cin >> n;
  pa.first = 0;
  pa.second = 0;
  vepa.push_back(pa);
  t = 0;
  sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> d;
    sum += d;
    t++;
    pa.first = sum;
    pa.second = t;
    vepa.push_back(pa);
  }
  //  for(int i=0;i<vepa.size();i++)cout<<vepa[i].first<<"
  //  "<<vepa[i].second<<endl;////
  sort(vepa.begin(), vepa.end());
  // cout<<endl;//////////
  // for(int i=0;i<vepa.size();i++)cout<<vepa[i].first<<"
  // "<<vepa[i].second<<endl;////
  sta = vepa[0].second;
  end = 0;
  max = 0;
  for (int i = 0; i < vepa.size(); i++) {

    if (vepa[i].first != vepa[i + 1].first) {
      if (max <= vepa[i].second - sta) {
        max = vepa[i].second - sta;
        // cout<<max<<" "<<i<<"koredesu"<<endl;////////////
      }
      sta = vepa[i + 1].second;
    } else if (i == vepa.size() - 1) {
      if (max < vepa[i].second - sta)
        max = vepa[i].second - sta;
    }

    // cout<<sta<<"<==="<<endl;/////////
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, d, t, j, k, max;
  long long sum = 0;
  vector<vector<int>> veve;
  vector<int> ve;
  cin >> n;
  t = 0;
  ve.push_back(0);
  ve.push_back(0);
  ve.push_back(0);
  ve[0] = 0;
  ve[1] = 0;
  ve[2] = 0;
  veve.push_back(ve);
  for (int i = 0; i < n; i++) {
    cin >> d;
    sum += d;
    k = 0;
    t++;
    for (int i = 0; i < veve.size(); i++) {
      if (veve[i][0] == sum) {
        veve[i][2] = t;
        k = 1;
      }
    }
    if (k == 0) {
      ve[0] = sum;
      ve[1] = ve[2] = t;
      veve.push_back(ve);
    }
  }
  max = 0;
  for (int j = 0; j < veve.size(); j++) {
    if (max < veve[j][2] - veve[j][1])
      max = veve[j][2] - veve[j][1];
    // cout<<veve[j][0]<<" "<<veve[j][1]<<" "<<veve[j][2]<<endl;/////
  }
  cout << max << endl;
  return 0;
}
Problem p00325 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

map<string, int> ff;

void stop() {
  int ret = 0;
  for (auto &c : ff) {
    if (isalpha(c.first[0])) {
      ++ret;
      printf("%c=%d\\n", c.first[0], c.second);
    }
  }
  exit(0);
}

int main() {
  int N;
  int convert[1001];
  char buff[64], type[50][5], var1[50][5], var2[50][5], var3[50][5];
  memset(convert, -1, sizeof(convert));

  fgets(buff, sizeof(buff), stdin);
  sscanf(buff, "%d", &N);

  for (int i = 0; i < N; i++) {
    int line;
    fgets(buff, sizeof(buff), stdin);
    sscanf(buff, "%d %s %s %s %s", &line, type[i], var1[i], var2[i], var3[i]);
    convert[line] = i;
    ff[var1[i]];
    ff[var2[i]];
    ff[var3[i]];
  }
  for (int i = 0; i < 16; i++)
    ff[to_string(i)] = i;

  int i = 0;
  for (int c = 0; c < 30000000; c++) {
    if (type[i][0] == 'H')
      stop();
    int val;
    bool set = true;
    if (type[i][0] == 'A') {
      val = ff[var2[i]] + ff[var3[i]];
    } else if (type[i][1] == 'U') {
      val = ff[var2[i]] - ff[var3[i]];
    } else if (type[i][1] == 'E') {
      val = ff[var2[i]];
    } else {
      set = false;
    }

    if (set && (val < 0 || val >= 16))
      stop();
    if (set)
      ff[var1[i]] = val;

    if (type[i][0] == 'I' && ff[var1[i]] != 0) {
      i = convert[stoi(var2[i])];
    } else {
      i++;
    }

    if (i < 0 || i >= N)
      stop();
  }

  puts("inf");
}

Error: -6
terminate called after throwing an instance of 'int'


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

map<string, int> ff;

void stop() {
  for (auto &c : ff) {
    if (isalpha(c.first[0]))
      printf("%c=%d\\n", c.first[0], c.second);
  }
  exit(0);
}

int main() {
  int N;
  int convert[1001];
  char buff[64], type[50][5], var1[50][5], var2[50][5], var3[50][5];
  memset(convert, -1, sizeof(convert));

  fgets(buff, sizeof(buff), stdin);
  sscanf(buff, "%d", &N);

  for (int i = 0; i < N; i++) {
    int line;
    fgets(buff, sizeof(buff), stdin);
    sscanf(buff, "%d %s %s %s %s", &line, type[i], var1[i], var2[i], var3[i]);
    convert[line] = i;
    ff[var1[i]];
    ff[var2[i]];
    ff[var3[i]];
  }
  for (int i = 0; i < 1001; i++)
    ff[to_string(i)] = i;
  if (ff.size() > 1006)
    throw(0);

  int i = 0;
  for (int c = 0; c < 1000000; c++) {
    int rev = ff[var1[i]];
    if (type[i][0] == 'A') {
      ff[var1[i]] = ff[var2[i]] + ff[var3[i]];
    } else if (type[i][1] == 'U') {
      ff[var1[i]] = ff[var2[i]] - ff[var3[i]];
    } else if (type[i][1] == 'E') {
      ff[var1[i]] = ff[var2[i]];
    } else if (type[i][0] == 'H') {
      stop();
    }

    if (ff[var1[i]] < 0 || ff[var1[i]] >= 16) {
      ff[var1[i]] = rev;
      stop();
    }

    if (type[i][0] == 'I' && ff[var1[i]] != 0) {
      i = convert[ff[var2[i]]];
    } else {
      i++;
    }

    if (i < 0 || i >= N)
      stop();
  }

  puts("inf");
}
Problem p00325 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

unordered_map<string, int> ff;

void stop() {
  int ret = 0;
  vector<pair<char, int>> vv;
  for (auto &c : ff)
    if (isalpha(c.first[0]))
      vv.emplace_back(c.first[0], c.second);
  sort(begin(vv), end(vv));
  for (auto &c : vv)
    printf("%c=%d\\n", c.first, c.second);
  exit(0);
}

int main() {
  int N;
  int convert[1001];
  char buff[64], type[50][5], var1[50][5], var2[50][5], var3[50][5];
  memset(convert, -1, sizeof(convert));

  fgets(buff, sizeof(buff), stdin);
  sscanf(buff, "%d", &N);

  for (int i = 0; i < N; i++) {
    int line;
    fgets(buff, sizeof(buff), stdin);
    sscanf(buff, "%d %s %s %s %s", &line, type[i], var1[i], var2[i], var3[i]);
    convert[line] = i;
    ff[var1[i]];
    ff[var2[i]];
    ff[var3[i]];
  }
  for (int i = 0; i < 16; i++)
    ff[to_string(i)] = i;

  int i = 0;
  for (int c = 0; c < 30000000; c++) {
    if (type[i][0] == 'H')
      stop();
    int val;
    bool set = true;
    if (type[i][0] == 'A') {
      val = ff[var2[i]] + ff[var3[i]];
    } else if (type[i][1] == 'U') {
      val = ff[var2[i]] - ff[var3[i]];
    } else if (type[i][1] == 'E') {
      val = ff[var2[i]];
    } else {
      set = false;
    }

    if (set && (val < 0 || val >= 16))
      stop();
    if (set)
      ff[var1[i]] = val;

    if (type[i][0] == 'I' && ff[var1[i]] != 0) {
      i = convert[stoi(var2[i])];
    } else {
      i++;
    }

    if (i < 0 || i >= N)
      stop();
  }

  puts("inf");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

map<string, int> ff;

void stop() {
  int ret = 0;
  for (auto &c : ff) {
    if (isalpha(c.first[0])) {
      ++ret;
      printf("%c=%d\\n", c.first[0], c.second);
    }
  }
  exit(0);
}

int main() {
  int N;
  int convert[1001];
  char buff[64], type[50][5], var1[50][5], var2[50][5], var3[50][5];
  memset(convert, -1, sizeof(convert));

  fgets(buff, sizeof(buff), stdin);
  sscanf(buff, "%d", &N);

  for (int i = 0; i < N; i++) {
    int line;
    fgets(buff, sizeof(buff), stdin);
    sscanf(buff, "%d %s %s %s %s", &line, type[i], var1[i], var2[i], var3[i]);
    convert[line] = i;
    ff[var1[i]];
    ff[var2[i]];
    ff[var3[i]];
  }
  for (int i = 0; i < 16; i++)
    ff[to_string(i)] = i;

  int i = 0;
  for (int c = 0; c < 30000000; c++) {
    if (type[i][0] == 'H')
      stop();
    if (type[i][0] == 'A') {
      ff[var1[i]] = ff[var2[i]] + ff[var3[i]];
    } else if (type[i][1] == 'U') {
      ff[var1[i]] = ff[var2[i]] - ff[var3[i]];
    } else if (type[i][1] == 'E') {
      ff[var1[i]] = ff[var2[i]];
    }
    if (ff[var1[i]] < 0 || ff[var1[i]] >= 16)
      stop();
    if (type[i][0] == 'I' && ff[var1[i]] != 0) {
      i = convert[stoi(var2[i])];
    } else {
      i++;
    }
    if (i < 0 || i >= N)
      stop();
  }

  puts("inf");
}
Problem p00327 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  int a[100];
  bool dp[105][105] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    dp[i][i] = true;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < n - i + 1; j++) {
      for (int k = j + 1; k <= j + i - 1; k++) {
        if (dp[j][k] && dp[k][j + i])
          dp[j][j + i] = true;
      }
      if (a[j] != a[j + i - 1])
        continue;
      int num = a[j];
      int l = j, r = j + i - 1;
      while (l < r && a[l] == num)
        l++;
      while (l < r && a[r] == num)
        r--;
      if (l == r && a[l] == num)
        dp[j][j + i] = true;
      if (dp[l][r + 1])
        dp[j][j + i] = true;
      for (int k = j + 1; k <= j + i - 2; k++) {
        if (a[k] == num && dp[j + 1][k] && dp[k + 1][j + i - 1])
          dp[j][j + i] = true;
      }
    }
  }
  /*for(int i = 0;i <= n;i++){
          for(int j = 0;j <= n;j++) cout << dp[i][j] << " ";
          cout << endl;
  }*/
  if (dp[0][n])
    cout << "yes" << endl;
  else
    cout << "no" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  const string delimiter = "\\n";
  REP(i, v.size()) {
    os << v[i];
    if (i != v.size() - 1)
      os << delimiter;
  }
  return os;
}
/*--------------------template--------------------*/

std::random_device rnd;
std::mt19937 mt(rnd());
std::uniform_int_distribution<> ran(0, 1e9);
int r[111];

int hs(const vi &v) {
  int res = 0;
  REP(i, v.size()) { res += r[i] * v[i]; }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  for (int i = 0; i < 111; ++i) {
    r[i] = ran(mt);
  }
  int n;
  cin >> n;
  vi v(n);
  cin >> v;
  set<int> st;
  queue<vi> que;
  que.push(v);
  while (!que.empty()) {
    vi t = que.front();
    que.pop();
    REP(i, t.size() - 1) {
      if (i > 0 && t[i] == t[i - 1])
        continue;
      if (t[i] == t[i + 1]) {
        int p = i + 1;
        while (p < t.size() - 1 && t[p + 1] == t[i])
          p++;
        vi nex;
        REP(j, i) nex.push_back(t[j]);
        FOR(j, p + 1, t.size()) nex.push_back(t[j]);
        if (nex.size() == 0) {
          cout << "yes" << endl;
          return 0;
        }
        int h = hs(nex);
        if (st.count(h))
          continue;
        st.insert(h);
        que.push(nex);
      }
    }
  }
  cout << "no" << endl;
  return 0;
  return 0;
}
Problem p00327 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
const int N = 1e2 + 10;
bool ans[N][N], went[N][N];
int a[N];
bool dp(int l, int r) {
  if (l - r == 1)
    return true;
  else if (r < l)
    return false;
  if (went[l][r])
    return ans[l][r];
  went[l][r] = true;
  if (r == l)
    return ans[l][r] = false;
  else if (r - l == 1)
    return ans[l][r] = a[l] == a[r];
  else if (r - l == 2)
    return ans[l][r] = a[l] == a[l + 1] && a[l] == a[r];
  else {
    for (int i = l; i <= r; i++)
      for (int j = i + 1; j <= r; j++)
        if (a[i] == a[j]) {
          ans[l][r] = dp(l, i - 1) && dp(i + 1, j - 1) && dp(j + 1, r);
          if (ans[l][r])
            return true;
        }
    for (int i = l; i <= r; i++)
      for (int j = i + 1; j <= r; j++)
        if (a[i] == a[j])
          for (int k = j + 1; k <= r; k++)
            if (a[j] == a[k]) {
              ans[l][r] = dp(l, i - 1) && dp(i + 1, j - 1) &&
                          dp(j + 1, k - 1) && dp(k + 1, r);
              if (ans[l][r])
                return true;
            }
    return ans[l][r] = false;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
      went[i][j] = false;
  if (dp(0, n - 1))
    printf("yes\\n");
  else
    printf("no\\n");
}


Error: TLE

Fixed submission:


#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> pi;
#define F first
#define S second
#define MP make_pair
#define PB push_back
map<int, int> m;
int a[20];
bool check(int n) {
  if (m.empty())
    return true;
  map<int, int>::iterator last = m.lower_bound(n), now;
  int val, nxt;
  vector<pi> temp;
  now = last;
  val = last->S;
  while (last != m.begin()) {
    last--;
    if (val != last->S) {
      last++;
      break;
    }
  }
  while (last != m.end()) {
    val = last->S;
    while (now->S == val) {
      now++;
      if (now == m.end())
        break;
    }
    if (now == m.end()) {
      if (last == m.begin())
        return true;
      else
        return false;
    }
    if (distance(last, now) > 1) {
      nxt = now->F;
      temp.clear();
      a[val] -= distance(last, now);
      if (a[val] == 1) {
        a[val] += distance(last, now);
        last = now;
        continue;
      }
      for (map<int, int>::iterator u = last; u != now; u++)
        temp.PB(*u);
      for (int i = 0; i < temp.size(); i++)
        m.erase(temp[i].F);
      if (check(nxt))
        return true;
      for (int i = 0; i < temp.size(); i++)
        m.insert(temp[i]);
      last = m.lower_bound(nxt);
    } else
      last++;
    now = last;
  }
  return false;
}
int main() {
  int n, temp;
  for (int i = 0; i < 10; i++)
    a[i] = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &temp);
    a[temp]++;
    m.insert(MP(i, temp));
  }
  if (check(0))
    printf("yes\\n");
  else
    printf("no\\n");
}
Problem p00329 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 1000

int N, a, cc = 1;
vector<int> G[MAX_V];
int match[MAX_V];
int used[MAX_V];

bool dfs(int v) {
  used[v] = cc;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || (used[w] != cc && dfs(w))) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < N; v++) {
    if (match[v] < 0) {
      cc++;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int main() {
  scanf("%d", &N);
  N--;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%d", &a);
      if (a == 1)
        G[j].push_back(N + i);
    }
  }
  for (int i = 0; i < N; i++)
    sort(G[i].begin(), G[i].end());
  int f = bipartite_matching();
  printf(f == N ? "yes\\n" : "no\\n");
  for (int i = 0; i < N && f == N; i++) {
    match[match[i]] = -1;
    match[i] = -1;
    cc++;
    dfs(i);
    G[i].clear();
    G[i].push_back(match[i]);
    printf("%d\\n", match[i] - N + 1);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 1000

int N, a, cc = 1;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];

bool dfs(int v) {
  used[v] = cc;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || (used[w] != cc && dfs(w))) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < N; v++) {
    if (match[v] < 0) {
      cc++;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int main() {
  scanf("%d", &N);
  N--;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%d", &a);
      if (a == 1)
        G[j].push_back(N + i);
    }
  }
  for (int i = 0; i < N; i++)
    sort(G[i].begin(), G[i].end());
  int f = bipartite_matching();
  printf(f == N ? "yes\\n" : "no\\n");
  for (int i = 0; i < N && f == N; i++) {
    match[match[i]] = -1;
    match[i] = -1;
    cc++;
    dfs(i);
    G[i].clear();
    G[i].push_back(match[i]);
    printf("%d\\n", match[i] - N + 1);
  }
  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll gcd(ll x, ll y) {
  if (x < y)
    swap(x, y);
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
int main() {
  ll n, m, c;
  cin >> n >> m >> c;
  cout << n * m * c / gcd(n, m) / gcd(n, m) << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll gcd(ll x, ll y) {
  if (x < y)
    swap(x, y);
  if (x % y)
    return y;
  return gcd(y, x % y);
}
int main() {
  ll n, m, c;
  cin >> n >> m >> c;
  cout << n * m * c / gcd(n, m) << endl;
  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int W, H, C, c;
  cin >> W >> H >> C;
  c = min(W, H);
  while (1) {
    if (W % c == 0 && H % c == 0)
      break;
    c--;
  }
  cout << (W / c) * (H / c) * C << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <stdio.h>
int main(void) {
  int W, H, C, a, b, c, d, e, h;
  char g, f;
  c = 0;
  scanf("%d %d %d", &W, &H, &C);
  a = W * H;
  for (;;) {
    c = c + 2;
    d = W / c;
    e = W % 1;
    if (e == 0) {
      f = H / d;
      g = f % 1;
      if (g == 0) {
        if (f == d) {
          h = c * C;
          break;
        }
      }
    }
  }
  printf("%d\\n", h);
  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int gcd(int m, int n) {
  if (n == 0) {
    return m;
  }
  return gcd(n, m % n);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;
  int g = gcd(W, H);
  cout << W / g * H / g * C << endl;

  return 0;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;

int gcd(int m, int n) {
  if (n == 0) {
    return 0;
  }
  return gcd(n, m % n);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;
  int g = gcd(W, H);
  cout << W / (g * H) / (g * C) << endl;

  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

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

int main() {
  int W, H, C;
  cin >> W >> H >> C;
  int GCD = gcd(W, H);
  cout << ((W / GCD) * (H / GCD) * C) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

int main() {
  int W, H, C;
  cin >> W >> H >> C;
  int GCD = gcd(W, H);
  cout << ((W / GCD) * (H / GCD) * C) << endl;

  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int w, h, C;
  cin >> w >> h >> C;
  int a = w, b = h, c;
  if (a < b)
    swap(a, b);
  while (a % b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  cout << (w / b) * (h / b) * C << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int func(int W, int H) {
  int x;
  if (W > H) {
    x = H;
    while (W % H != 0) {
      x = W % H;
      W = H;
      H = x;
    }
  } else if (W < H) {
    x = W;
    while (H % W != 0) {
      x = H % W;
      H = W;
      W = x;
    }
  }
  return x;
}

int main() {
  int w, h, C;
  cin >> w >> h >> C;
  int num = func(w, h);
  cout << (w / num) * (h / num) * C << endl;
  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int W, H, C;
  int mi, ma, ans;

  cin >> W >> H >> C;

  mi = min(W, H);
  ma = max(W, H);

  for (int i = 1; i <= mi; i++) {
    if (ma % i == 0 && mi % i == 0)
      ans = i;
  }

  ans = (W * H) / (ans * ans);

  cout << ans * C << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int S(int W, int H, int i) {
  int j = 1;
  if (W == H)
    return i + 1;

  while (j < W && j < H) {
    j++;
  }

  i = S(W - j, H - j, j);

  return i;
}
int main() {
  int W, H, C, a;

  cin >> W >> H >> C;

  a = S(W, H, 0);

  cout << a * C << endl;

  return 0;
}
Problem p00333 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int w, h, c, i, num, hd, wd;
  int sum = 0;
  cin >> w >> h >> c;
  hd = h;
  wd = w;
  if (hd == wd)
    cout << c << endl;
  else {
    while (1) {
      if (w > h) {
        if (w % h == 0) {
          num = h;
          break;
        }
        w = w % h;
      }
      if (h > w) {
        if (h % w == 0) {
          num = w;
          break;
        }
        h = h % w;
      }
    }

    cout << ((hd * wd) / (num * num)) * c << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int w, h, c, i, num, hd, wd;
  int sum = 0;
  cin >> w >> h >> c;
  hd = h;
  wd = w;
  if (h == w)
    cout << c << endl;
  while (1) {
    if (w > h) {
      if (w % h == 0) {
        num = h;
        break;
      }
      w = w % h;
    }
    if (h > w) {
      if (h % w == 0) {
        num = w;
        break;
      }
      h = h % w;
    }
  }

  cout << ((hd * wd) / (num * num)) * c << endl;
}
Problem p00333 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int gcd(int m, int n) {
  if (n == 0)
    return m;
  return gcd(n, m % n);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;

  int g = gcd(W, H);
  cout << W / g * H / g * C << endl;

  return 0;
}


Error: -8

Fixed submission:

#include <iostream>
using namespace std;

int gcd(int m, int n) {
  if (n == 0)
    return 0;
  return gcd(n, m % n);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;

  int g = gcd(W, H);
  cout << W / g * H / g * C << endl;

  return 0;
}