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

#include <stdio.h>
#include <string.h>
int main() {
  int n;
  scanf("%d", &n);
  for (int j = 0; j < n; j++) {
    char pro[1000];
    scanf(" %s", pro);
    int num, b[1000] = {};
    num = strlen(pro);

    int a[1000] = {};
    for (int i = 0; pro[i] != '\\0'; i++) {
      a[i] = pro[i] - '0';
    }
    int ans = 0, count = 0;
    for (int i = 0; i < 1000; i++)
      ans += a[i];
    while (ans > 9) {
      ans -= 9;
      count++;
    }
    int x;
    x = (num - 1 + count);
    if (x % 2 == 0)
      printf("Audrey wins.\\n");
    else
      printf("Fabre wins.\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
int main() {
  int n;
  scanf("%d", &n);
  for (int j = 0; j < n; j++) {
    char pro[100];
    scanf(" %s", pro);
    int num, a[100] = {};
    num = strlen(pro);
    for (int i = 0; pro[i] != '\\0'; i++) {
      a[i] = pro[i] - '0';
    }
    int ans = 0;
    for (int i = 0; i < 100; i++)
      ans += a[i];
    ans /= 10;
    int x;
    x = (num - 1 + ans);
    if (x % 2 == 0)
      printf("Audrey wins.\\n");
    else
      printf("Fabre wins.\\n");
  }
  return 0;
}
Problem p01307 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    string N;
    cin >> N;
    const int d = N.size();
    int p = -1;
    for (const char c : N) {
      p += c - '0';
    };
    p /= 9;
    cout << ((p + d) % 2 == 0 ? "Fabre wins." : "Audrey wins.") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    string N;
    cin >> N;
    const int d = N.size();
    int p = 0;
    for (const char c : N) {
      p += c - '0';
    }
    p %= 9;
    cout << ((p + d + 1) % 2 == 0 ? "Audrey wins." : "Fabre wins.") << endl;
  }
  return 0;
}
Problem p01307 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <unordered_map>

using namespace std;

unordered_map<string, bool> memo;

inline string Sum(int i, string s) {
  int sum = (s[i] - '0') + (s[i + 1] - '0');
  string res = s.substr(0, i);

  if (sum / 10 == 0)
    res += (char)(sum + '0');
  else {
    res += (char)((sum / 10) + '0');
    res += (char)((sum % 10) + '0');
  }

  if (s.size() == i + 1)
    return res;
  else
    return res + s.substr(i + 2, s.size() - (i + 2));
}

int main() {
  int t;

  cin >> t;
  while (t--) {
    string s;
    int ft = 0;

    cin >> s;
    while (1 < s.size()) {
      s = Sum(0, s);
      ft = 1 - ft;
    }

    if (ft == 1)
      cout << "Fabre wins.\\n";
    else
      cout << "Audrey wins.\\n";
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <unordered_map>

using namespace std;

unordered_map<string, bool> memo;

inline string Sum(int i, string s) {
  int sum = (s[i] - '0') + (s[i + 1] - '0');
  string res = s.substr(0, i);

  if (sum / 10 == 0)
    res += (char)(sum + '0');
  else {
    res += (char)((sum / 10) + '0');
    res += (char)((sum % 10) + '0');
  }

  if (s.size() == i + 1)
    return res;
  else
    return res + s.substr(i + 2, s.size() - (i + 2));
}

bool Rec(int p, string s) {
  string tmp = (char)('0' + p) + s;
  if (memo.find(tmp) != memo.end())
    return memo[tmp];

  if (s.size() == 1)
    return (p == 1);

  bool res;
  if (p == 0) {
    res = false;
    for (int i = 0; i < s.size() - 1; ++i)
      res |= Rec(1, Sum(i, s));
  } else {
    res = true;
    for (int i = 0; i < s.size() - 1; ++i)
      res &= Rec(0, Sum(i, s));
  }

  return memo[tmp] = res;
}

int main() {
  int t;

  cin >> t;
  while (t--) {
    string s;
    bool ft = true;

    cin >> s;
    if (Rec(0, s))
      cout << "Fabre wins.\\n";
    else
      cout << "Audrey wins.\\n";
  }

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 50000;
const int MAX_M = 50000;

const int NSC = 12;

const string scs[NSC] = {"C",  "C#", "D",  "D#", "E",  "F",
                         "F#", "G",  "G#", "A",  "A#", "B"};

/* typedef */

typedef map<string, int> msi;

/* global variables */

msi scmap;
int n, m;
int tis[MAX_N + 1], sis[MAX_M + 1];

/* subroutines */

inline int mod(int k) { return (k + NSC) % NSC; }

bool check(int ti) {
  for (int si = m - 1; si >= 0; si--) {
    int &t = tis[ti];
    int &s = sis[si];

    if (t == s)
      ti--;
    else if (mod(t + 1) == s)
      ti -= 2;
    else if (mod(t - 1) == s)
      ti++;
    else
      return false;
  }

  return (ti == -1);
}

/* main */

int main() {
  for (int i = 0; i < NSC; i++)
    scmap[scs[i]] = i;

  int tn;
  cin >> tn;

  while (tn--) {
    cin >> n >> m;
    string sc;

    for (int i = 0; i < n; i++) {
      cin >> sc;
      tis[i] = scmap[sc];
    }

    for (int i = 0; i < m; i++) {
      cin >> sc;
      sis[i] = scmap[sc];
    }

    bool ok = check(n - 1) || check(n - 2);
    cout << (ok ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 50000;
const int MAX_M = 50000;

const int NSC = 12;

const string scs[NSC] = {"C",  "C#", "D",  "D#", "E",  "F",
                         "F#", "G",  "G#", "A",  "A#", "B"};

/* typedef */

typedef map<string, int> msi;

/* global variables */

msi scmap;
int n, m;
int tis[MAX_N], sis[MAX_M];

/* subroutines */

bool rec(int ti, int si) {
  if (si >= m - 1)
    return (ti >= n - 2);

  int s1 = sis[si + 1];

  if (ti > 0 && (tis[ti - 1] + NSC - 1) % NSC == s1 && rec(ti - 1, si + 1))
    return true;
  if (ti < n - 1 && tis[ti + 1] == s1 && rec(ti + 1, si + 1))
    return true;
  if (ti < n - 2 && (tis[ti + 2] + 1) % NSC == s1 && rec(ti + 2, si + 1))
    return true;

  return false;
}

/* main */

int main() {
  for (int i = 0; i < NSC; i++)
    scmap[scs[i]] = i;

  int tn;
  cin >> tn;

  while (tn--) {
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
      string sc;
      cin >> sc;
      tis[i] = scmap[sc];
    }

    for (int i = 0; i < m; i++) {
      string sc;
      cin >> sc;
      sis[i] = scmap[sc];
    }

    bool ok = rec(-1, -1);
    cout << (ok ? "Yes" : "No") << endl;
  }

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

#include <algorithm>
#include <stdio.h>
using namespace std;
int key[] = {0, 2, 3, 5, 7, 8, 10};
char in[5];
int p[110000];
int q[110000];
int n;
int solve(int a, int b) {
  if (a < 0 && b == -1)
    return 1;
  if (a < 0)
    return 0;
  if (b < 0)
    return 0;
  if (b >= n)
    return 0;
  if (p[b] == q[a])
    return solve(a - 1, b - 1);
  if ((p[b] + 1) % 12 == q[a])
    return solve(a - 1, b - 2);
  if ((p[b] + 11) % 12 == q[a])
    return solve(a - 1, b + 1);
  return 0;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int a, b;
    scanf("%d%d", &a, &b);
    n = a;
    for (int i = 0; i < a; i++) {
      scanf("%s", in);
      p[i] = key[in[0] - 'A'];
      if (in[1] == '#')
        p[i]++;
    }
    for (int i = 0; i < b; i++) {
      scanf("%s", in);
      q[i] = key[in[0] - 'A'];
      if (in[1] == '#')
        q[i]++;
    }
    if (solve(b - 1, a - 1))
      printf("Yes\\n");
    else if (solve(b - 1, a - 2))
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <set>
#include <stdio.h>
using namespace std;
int key[] = {0, 2, 3, 5, 7, 8, 10};
char in[5];
int p[110000];
int q[110000];

int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int a, b;
    scanf("%d%d", &a, &b);
    for (int i = 0; i < a; i++) {
      scanf("%s", in);
      p[i] = key[in[0] - 'A'];
      if (in[1] == '#')
        p[i]++;
    }
    for (int i = 0; i < b; i++) {
      scanf("%s", in);
      q[i] = key[in[0] - 'A'];
      if (in[1] == '#')
        q[i]++;
    }
    set<int> now;
    if (p[0] == q[0])
      now.insert(0);
    if ((p[1] + 1) % 12 == q[0])
      now.insert(1);
    for (int i = 1; i < b; i++) {
      set<int> to;
      for (set<int>::iterator it = now.begin(); it != now.end(); it++) {
        int at = *it;
        if (at + (b - i) * 2 < a)
          continue;
        if (at < a - 1 && p[at + 1] == q[i])
          to.insert(at + 1);
        if (at < a - 2 && (p[at + 2] + 1) % 12 == q[i])
          to.insert(at + 2);
        if (at && (p[at - 1] + 11) % 12 == q[i])
          to.insert(at - 1);
      }
      now = to;
    }
    if (now.count(a - 2) || now.count(a - 1))
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}
Problem p01308 (C++) — buggy submission, verdict: Time Limit Exceeded

// 47
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    string t[50000];
    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }
    string s[50000];
    for (int i = 0; i < m; i++) {
      cin >> s[i];
    }
    int i;
    for (i = 0; i <= 1; i++) {
      int x = n - 1 - i;
      int j;
      for (j = m - 1; j >= 0; j--) {
        if (x < 0 || n <= x)
          break;
        static string sn[] = {"C",  "C#", "D",  "D#", "E",  "F",
                              "F#", "G",  "G#", "A",  "A#", "B"};
        int c = find(sn, sn + 12, t[x]) - sn;
        if (sn[c] == s[j]) {
          x--;
        } else if (sn[(c + 1) % 12] == s[j]) {
          x -= 2;
        } else if (sn[(c + 11) % 12] == s[j]) {
          x++;
        } else {
          break;
        }
      }
      if (j < 0 && x == -1) {
        break;
      }
    }
    cout << ((i > 1) ? "No" : "Yes") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 27
#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int s[50002];
    string sn[] = {"C",  "C#", "D",  "D#", "E",  "F",
                   "F#", "G",  "G#", "A",  "A#", "B"};
    for (int i = 1; i <= n; i++) {
      string ss;
      cin >> ss;
      s[i] = find(sn, sn + 12, ss) - sn;
    }
    set<int> st[2];
    st[0].insert(0);
    for (int i = 0; i < m; i++) {
      st[!(i & 1)].clear();
      string ssn;
      cin >> ssn;
      int sd = find(sn, sn + 12, ssn) - sn;
      for (set<int>::iterator it = st[i & 1].begin(); it != st[i & 1].end();
           it++) {
        int step[] = {1, 2, -1};
        int d[] = {0, 1, 11};
        for (int j = 0; j < 3; j++) {
          int nx = *it + step[j];
          if (max(0, n - 1 - (m - i) * 2) < nx && nx <= n &&
              (s[nx] + d[j]) % 12 == sd) {
            st[!(i & 1)].insert(nx);
          }
        }
      }
    }
    cout << ((st[m & 1].count(n) || st[m & 1].count(n - 1)) ? "Yes" : "No")
         << endl;
  }
  return 0;
}
Problem p01308 (C++) — buggy submission, verdict: Runtime Error

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

const int LIM = 60000;

map<string, int> tone;

void init() {
  tone["C"] = 0;
  tone["C#"] = 1;
  tone["D"] = 2;
  tone["D#"] = 3;
  tone["E"] = 4;
  tone["F"] = 5;
  tone["F#"] = 6;
  tone["G"] = 7;
  tone["G#"] = 8;
  tone["A"] = 9;
  tone["A#"] = 10;
  tone["B"] = 11;
}

int st[LIM + 1], me[LIM + 1];
int S, N, M;

bool dfs(int pos, int num) {
  if (pos == N)
    return num == M;
  if (num >= M || pos >= N)
    return 0;
  if (me[num] == st[pos])
    return dfs(pos + 1, num + 1);
  if (me[num] == (st[pos] + 1) % 12)
    return dfs(pos + 2, num + 1);
  if (pos > 0 && me[num] == (st[pos] + 11) % 12)
    return dfs(pos - 1, num + 1);
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  init();
  cin >> S;
  for (; S--;) {
    cin >> N >> M;
    string str;
    for (int x = 0; x < N; x++) {
      cin >> str;
      st[x] = tone[str];
    }
    for (int x = 0; x < M; x++) {
      cin >> str;
      me[x] = tone[str];
    }
    reverse(st, st + N);
    reverse(me, me + M);
    bool res = false;
    res = dfs(0, 0) || dfs(1, 0);
    if (res) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

const int LIM = 50000;

map<string, int> tone;

void init() {
  tone["C"] = 0;
  tone["C#"] = 1;
  tone["D"] = 2;
  tone["D#"] = 3;
  tone["E"] = 4;
  tone["F"] = 5;
  tone["F#"] = 6;
  tone["G"] = 7;
  tone["G#"] = 8;
  tone["A"] = 9;
  tone["A#"] = 10;
  tone["B"] = 11;
}

string stair[LIM], melody[LIM];
int S, N, M;

int pos, num;

bool dfs(void) {
  if (num >= M)
    return pos == N;
  if (pos >= N)
    return false;
  if ((M - num) * 2 + pos < N)
    return false;
  if (tone[melody[num]] == tone[stair[pos]]) {
    ++pos;
    ++num;
    if (dfs())
      return true;
    --pos;
    --num;
  }
  if ((tone[melody[num]] - tone[stair[pos]] + 12) % 12 == 1) {
    pos += 2;
    ++num;
    if (dfs())
      return true;
    pos -= 2;
    --num;
  }
  if (pos > 0 && (tone[melody[num]] - tone[stair[pos]] + 12) % 12 == 11) {
    --pos;
    ++num;
    if (dfs())
      return true;
    ++pos;
    --num;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  init();
  cin >> S;
  for (; S--;) {
    cin >> N >> M;
    for (int x = 0; x < N; x++) {
      cin >> stair[x];
    }
    for (int x = 0; x < M; x++) {
      cin >> melody[x];
    }
    reverse(stair, stair + N);
    reverse(melody, melody + N);
    bool res = false;
    pos = 0;
    num = 0;
    res = res || dfs();
    pos = 1;
    num = 0;
    res = res || dfs();
    if (res) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p01308 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string c[12] = {"C",  "C#", "D",  "D#", "E",  "F",
                  "F#", "G",  "G#", "A",  "A#", "B"};
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    string s[n], t[m];
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < m; i++)
      cin >> t[i];
    bool ck = 0;
    for (int k = 1; k < 3; k++) {
      int x = n - k;
      for (int i = m - 1; i >= 0; i--) {
        if (x < 0 || x >= n) {
          x = 1 << 29;
          break;
        }
        int p;
        for (p = 0; p < 12; p++)
          if (c[p] == s[x])
            break;
        if (t[i] == c[p])
          x--;
        else if (t[i] == c[(p + 1) % 12])
          x -= 2;
        else if (t[i] == c[(p + 11) % 12])
          x++;
        else {
          x = 1 << 29;
          break;
        }
      }
      if (x == -1)
        ck = 1;
    }
    if (ck)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string c[12] = {"C",  "C#", "D",  "D#", "E",  "F",
                  "F#", "G",  "G#", "A",  "A#", "B"};
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    string s[n], t[m];
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < m; i++)
      cin >> t[i];
    bool dp[2][n];
    memset(dp, 0, sizeof(dp));
    for (int k = 0; k < 2; k++) {
      int x = k;
      if (x < 0 || x >= n)
        continue;
      int p = 0;
      for (int l = 0; l < 12; l++) {
        if (c[l] == s[x])
          p = l;
      }
      p = (p + 12 + k) % 12;
      if (t[0] == c[p])
        dp[0][x] = 1;
    }

    for (int i = 1; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (!dp[0][j])
          continue;
        for (int k = -1; k <= 2; k++) {
          if (!k)
            continue;
          int x = j + k;
          if (x < 0 || x >= n)
            continue;
          int p = 0;
          for (int l = 0; l < 12; l++) {
            if (c[l] == s[x])
              p = l;
          }
          int q = k;
          if (q > 0)
            q--;
          p = (p + 12 + q) % 12;
          if (t[i] == c[p])
            dp[1][x] = 1;
        }
      }
      for (int j = 0; j < n; j++) {
        dp[0][j] = dp[1][j];
        dp[1][j] = 0;
      }
    }
    bool ck = 0;
    for (int i = 0; i < 2; i++)
      ck |= dp[0][n - i - 1];
    if (ck)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01308 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int input() {
  char str[10];
  scanf("%s", str);

  if (str[0] == 'B') {
    return 11;
  }

  if (str[0] == 'A') {
    if (str[1] == '#')
      return 10;
    return 9;
  }

  if (str[0] == 'G') {
    if (str[1] == '#')
      return 8;
    return 7;
  }

  if (str[0] == 'F') {
    if (str[1] == '#')
      return 6;
    return 5;
  }

  if (str[0] == 'E') {
    return 4;
  }

  if (str[0] == 'D') {
    if (str[1] == '#')
      return 3;
    return 2;
  }

  if (str[0] == 'C') {
    if (str[1] == '#')
      return 1;
    return 0;
  }

  assert(0);
  return -1;
}

int n, m;
int t[50005];
int u[50005];

int dfs(int depth, int pos) {
  if (depth == m && n < pos + 2)
    return 1;
  if (depth == m)
    return 0;

  if (1 <= pos - 1 && (t[pos - 1] + 11) % 12 == u[depth]) {
    if (dfs(depth + 1, pos - 1))
      return 1;
  }

  if (pos + 1 <= n && t[pos + 1] == u[depth]) {
    if (dfs(depth + 1, pos + 1))
      return 1;
  }

  if (pos + 2 <= n && (t[pos + 2] + 1) % 12 == u[depth]) {
    if (dfs(depth + 1, pos + 2))
      return 1;
  }

  return 0;
}

int main() {
  int Tc;
  scanf("%d", &Tc);
  while (Tc--) {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
      t[i] = input();
    for (int i = 0; i < m; i++)
      u[i] = input();

    int ans = dfs(0, 0);
    if (ans)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int input() {
  char str[10];
  scanf("%s", str);

  if (str[0] == 'B') {
    return 11;
  }

  if (str[0] == 'A') {
    if (str[1] == '#')
      return 10;
    return 9;
  }

  if (str[0] == 'G') {
    if (str[1] == '#')
      return 8;
    return 7;
  }

  if (str[0] == 'F') {
    if (str[1] == '#')
      return 6;
    return 5;
  }

  if (str[0] == 'E') {
    return 4;
  }

  if (str[0] == 'D') {
    if (str[1] == '#')
      return 3;
    return 2;
  }

  if (str[0] == 'C') {
    if (str[1] == '#')
      return 1;
    return 0;
  }

  assert(0);
  return -1;
}

int n, m;
int t[50005];
int u[50005];

map<int, bool> vd[50005];

int dfs(int depth, int pos) {
  if (depth == m && n < pos + 2)
    return 1;

  if (vd[depth][pos])
    return 0;
  vd[depth][pos] = 1;

  if (pos + (m - depth + 1) * 2 < n)
    return 0;

  if (1 <= pos - 1 && (t[pos - 1] + 11) % 12 == u[depth]) {
    if (dfs(depth + 1, pos - 1))
      return 1;
  }

  if (pos + 1 <= n && t[pos + 1] == u[depth]) {
    if (dfs(depth + 1, pos + 1))
      return 1;
  }

  if (pos + 2 <= n && (t[pos + 2] + 1) % 11 == u[depth]) {
    if (dfs(depth + 1, pos + 2))
      return 1;
  }

  return 0;
}

int main() {
  int Tc;
  scanf("%d", &Tc);
  while (Tc--) {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
      t[i] = input();
    for (int i = 0; i < m; i++)
      u[i] = input();

    for (int i = 0; i <= n + 1; i++)
      vd[i].clear();
    int ans = dfs(0, 0);
    if (ans)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}
Problem p01314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, i, j, count;
  while (1) {
    count = 0;
    cin >> n;
    if (n == 0)
      break;

    for (i = 1; i <= n; i++) {
      int total = 0;
      total += i;
      for (j = i + 1; j <= n; j++) {
        total += j;

        if (total == n) {
          count++;
          break;
        }
      }
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, i, j, count;
  while (1) {
    count = 0;
    cin >> n;
    if (n == 0)
      break;

    for (i = 1; i <= n; i++) {
      int total = 0;
      total += i;
      for (j = i + 1; j <= n; j++) {
        total += j;
        cout << "total" << total << endl;
        if (total == n) {
          count++;
          break;
        }
      }

      cout << count << endl;
    }
  }

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

#include <iostream>
using namespace std;
int main() {

  int input, count;

  while (cin >> input, input) {
    count = 0;
    for (int i = 1; i < input; i++) {
      int sum = 0;
      for (int j = i; j < input; j++) {
        sum += j;
        if (input == sum) {
          count++;
          break;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

int main() {

  int input, count;

  while (std::cin >> input, input) {
    count = 0;
    for (int i = 1; i < input; i++) {
      int sum = 0;
      for (int j = i; j << input; j++) {
        sum += j;
        if (input == sum) {
          count++;
          break;
        }
      }
    }

    std::cout << count << std::endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int Solve(int n) {
  int result = 0;
  for (int div = 2; div <= n; div++) {
    if (div % 2 == 1) {
      if (n % div == 0) {
        int median = n / div;
        if (median - (div / 2) >= 1) {
          // cerr << div << endl;
          result++;
        }
      }
    } else {
      if (n % div == div / 2) {
        int median = n / div;
        if (median - (div / 2 - 1) >= 1) {
          // cerr << div << endl;
          result++;
        }
      }
    }
  }
  return result;
}

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << Solve(n) << endl;
  }

  return 0;
}


Error: 0
2
3
5
8
25


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int Solve(int n) {
  int result = 0;
  for (int div = 2; div <= n / 2; div++) {
    if (div % 2 == 1) {
      if (n % div == 0) {
        int median = n / div;
        if (median - (div / 2) >= 1) {
          cerr << div << endl;
          result++;
        }
      }
    } else {
      if (n % div == div / 2) {
        int median = n / div;
        if (median - (div / 2 - 1) >= 1) {
          cerr << div << endl;
          result++;
        }
      }
    }
  }
  return result;
}

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << Solve(n) << endl;
  }

  return 0;
}