prithivMLmods/Coder-Stat huggingface dataset:
Problem p00622 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

queue<char> to_queue(string s) {
  queue<char> q;
  for (int i = 0; i < (int)s.size(); ++i) {
    q.push(s[i]);
  }
  return q;
}

int main() {
  string red, green, down;
  while (cin >> red, red != "-") {
    cin >> green >> down;

    queue<char> r, g, d;
    r = to_queue(red);
    g = to_queue(green);
    d = to_queue(down);

    string res = "";
    char c = g.front();
    g.pop();
    while (!r.empty() || !g.empty()) {
      cout << c << endl;
      if (c == d.front()) {
        d.pop();
        c = r.front();
        r.pop();
      } else {
        res += c;
        c = g.front();
        g.pop();
      }
    }
    res += c;
    cout << res << endl;
  }
  return 0;
}


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

queue<char> to_queue(string s) {
  queue<char> q;
  for (int i = 0; i < (int)s.size(); ++i) {
    q.push(s[i]);
  }
  return q;
}

int main() {
  string red, green, down;
  while (cin >> red, red != "-") {
    cin >> green >> down;

    queue<char> r, g, d;
    r = to_queue(red);
    g = to_queue(green);
    d = to_queue(down);

    string res = "";
    char c = g.front();
    g.pop();
    while (!r.empty() || !g.empty()) {
      if (c == d.front()) {
        d.pop();
        c = r.front();
        r.pop();
      } else {
        res += c;
        c = g.front();
        g.pop();
      }
    }
    res += c;
    cout << res << endl;
  }
  return 0;
}
Problem p00623 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
string expr;
int n, sweet[4], s, ans;
int eval(int b, int e) {
  if (b + 1 == e)
    return sweet[expr[b] - '1'];
  int l, o, r, d;
  for (o = b, d = 0; o < e; o++) {
    char c = expr[o];
    if (c == '(')
      d++;
    if (c == ')')
      d--;
    if (d == 0 && (c == '&' || c == '|' || c == '^'))
      break;
  }
  if (o == e && expr[b] == '(')
    return eval(b + 1, e - 1);
  l = eval(b, o), r = eval(o + 1, e);
  char c = expr[o];
  return c != '&' ? c == '^' ? l ^ r : l | r : l & r;
}
void op(int c) {
  for (; c < s; c++)
    if (expr[c] == ' ')
      break;
  if (c < s)
    rep(i, 3) {
      expr[c] = "&|^"[i];
      op(c + 1);
      expr[c] = ' ';
    }
  else if (eval(0, s) == 15)
    ans++;
}
int main() {
  while (getline(cin, expr), expr != "END") {
    s = expr.size(), ans = 0;
    cin >> n;
    rep(i, n) {
      int t;
      sweet[i] = 0;
      rep(j, 4) cin >> t, sweet[i] <<= 1, sweet[i] += t;
    }
    op(0);
    cout << ans << endl;
    cin.ignore();
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
string expr;
int n, sweet[10], s, ans;
int eval(int b, int e) {
  if (b + 1 == e)
    return sweet[expr[b] - '1'];
  int l, o, r, d;
  for (o = b, d = 0; o < e; o++) {
    char c = expr[o];
    if (c == '(')
      d++;
    if (c == ')')
      d--;
    if (d == 0 && (c == '&' || c == '|' || c == '^'))
      break;
  }
  if (o == e && expr[b] == '(')
    return eval(b + 1, e - 1);
  l = eval(b, o), r = eval(o + 1, e);
  char c = expr[o];
  return c != '&' ? c == '^' ? l ^ r : l | r : l & r;
}
void op(int c) {
  for (; c < s; c++)
    if (expr[c] == ' ')
      break;
  if (c < s)
    rep(i, 3) {
      expr[c] = "&|^"[i];
      op(c + 1);
      expr[c] = ' ';
    }
  else if (eval(0, s) == 15)
    ans++;
}
int main() {
  while (getline(cin, expr), expr != "END") {
    s = expr.size(), ans = 0;
    cin >> n;
    rep(i, n) {
      int t;
      sweet[i] = 0;
      rep(j, 4) cin >> t, sweet[i] <<= 1, sweet[i] += t;
    }
    op(0);
    cout << ans << endl;
    cin.ignore();
  }
  return 0;
}
Problem p00623 (C++)

Buggy submission (status: Runtime Error):
#include <ctype.h>
#include <map>
#include <stdio.h>
#include <vector>
typedef std::map<int, int> M;
typedef M::iterator I;
typedef std::pair<int, int> P;
std::vector<P> v;
int x[10];
int F(char *&p, int n) {
  if (isdigit(*p))
    return -*p + '0';
  v.push_back(P(0, 0));
  int s = v.size();
  v[n].first = F(++p, s);
  s = v.size();
  v[n].second = F(p += 2, s);
  ++p;
  return n;
}
M G(int i) {
  M m;
  if (i < 0)
    m[x[-i]] = 1;
  else {
    M a(G(v[i].first)), b(G(v[i].second));
    for (I p = a.begin(); p != a.end(); ++p)
      for (I q = b.begin(); q != b.end(); ++q) {
        int a = p->first, b = q->first, c = p->second * q->second;
        m[a & b] += c;
        m[a | b] += c;
        m[a ^ b] += c;
      }
  }
  return m;
}
int main() {
  int n, i, j, t;
  char *p, s[512];
  while (fgets(s, 512, stdin), *s - 'E') {
    v.clear();
    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
      for (x[i] = j = 0; j < 4; ++j)
        scanf("%d", &t), x[i] = x[i] * 2 + t;
    t = F(p = s, 0);
    printf("%d\n", t ? x[-t] == 15 : G(0)[15]);
    fgets(s, 512, stdin);
  }
  return 0;
}

Fixed submission:
#include <ctype.h>
#include <map>
#include <stdio.h>
#include <vector>
typedef std::map<int, int> M;
typedef M::iterator I;
typedef std::pair<int, int> P;
std::vector<P> v;
int x[10];
int F(char *&p, int n) {
  if (isdigit(*p))
    return -*p + '0';
  v.push_back(P(0, 0));
  int s = v.size();
  v[n].first = F(++p, s);
  s = v.size();
  v[n].second = F(p += 2, s);
  ++p;
  return n;
}
M G(int i) {
  M m;
  if (i < 0)
    m[x[-i]] = 1;
  else {
    M a(G(v[i].first)), b(G(v[i].second));
    for (I p = a.begin(); p != a.end(); ++p)
      for (I q = b.begin(); q != b.end(); ++q) {
        int a = p->first, b = q->first, c = p->second * q->second;
        m[a & b] += c;
        m[a | b] += c;
        m[a ^ b] += c;
      }
  }
  return m;
}
int main() {
  int n, i, j, t;
  char *p, s[512];
  v.reserve(64);
  while (fgets(s, 512, stdin), *s - 'E') {
    v.clear();
    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
      for (x[i] = j = 0; j < 4; ++j)
        scanf("%d", &t), x[i] = x[i] * 2 + t;
    t = F(p = s, 0);
    printf("%d\n", t ? x[-t] == 15 : G(0)[15]);
    fgets(s, 512, stdin);
  }
  return 0;
}
Problem p00627 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int s = 0, a;
    while (n--) {
      cin >> a;
      s += a;
    }
    cout << s << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int s = 0, a;
    while (n) {
      n -= 4;
      cin >> a;
      s += a;
    }
    cout << s << endl;
  }
}
Problem p00627 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int n, k[5], ans = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n / 4; ++i) {
      cin >> k[i];
      ans += k[i];
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int n, k[100005], ans = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n / 4; ++i) {
      cin >> k[i];
      ans += k[i];
    }
    cout << ans << endl;
  }
}
Problem p00627 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n, a, sum;
int main() {
  while (true) {
    sum = 0;
    cin >> n;
    n /= 4;
    for (int i = 0; i < n; i++) {
      cin >> a;
      sum += a;
    }
    cout << sum << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, a, sum;
int main() {
  while (true) {
    sum = 0;
    cin >> n;
    if (!n) {
      break;
    }
    n /= 4;
    for (int i = 0; i < n; i++) {
      cin >> a;
      sum += a;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00627 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

const int mod = 1000000007;

int main() {
  int n;
  while (cin >> n, n) {
    int a, b;
    while (n--) {
      cin >> a;
      b += a;
    }
    cout << b << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

const int mod = 1000000007;

int main() {
  int n;
  while (cin >> n, n) {
    int a = 0, b = 0;
    n /= 4;
    while (n--) {
      cin >> a;
      b += a;
    }
    cout << b << endl;
  }
}
Problem p00627 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main(void) {
  int s, n, i, x;
  while (1) {
    scanf("%d", &n);
    s = 0;
    for (i = 0; i < n / 4; i++) {
      scanf("%d", &x);
      s += x;
    }
    printf("%d\n", s);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main(void) {
  int s, n, i, x;
  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    s = 0;
    for (i = 0; i < n / 4; i++) {
      scanf("%d", &x);
      s += x;
    }
    printf("%d\n", s);
  }
  return 0;
}
Problem p00627 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n;
  for (; cin >> n, n;) {
    int sum = 0;
    for (;; n = n / 5) {
      int tmp;
      cin >> tmp;
      sum += tmp;
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  for (; cin >> n, n;) {
    int sum = 0;
    for (; n > 0; n = n - 4) {
      int tmp;
      cin >> tmp;
      sum += tmp;
    }
    cout << sum << endl;
  }
}
Problem p00628 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
string S, T;
int chain;
int main() {
  while (true) {
    getline(cin, S);
    S += ' ';
    T = "";
    if (S == "END OF INPUT") {
      break;
    }
    for (int i = 0; i < S.size(); i++) {
      if (S[i] != ' ') {
        chain++;
      } else {
        T += to_string(chain);
        chain = 0;
      }
    }
    cout << T << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
string S, T;
int chain;
int main() {
  while (true) {
    getline(cin, S);
    S += ' ';
    T = "";
    if (S == "END OF INPUT ") {
      break;
    }
    for (int i = 0; i < S.size(); i++) {
      if (S[i] != ' ') {
        chain++;
      } else {
        T += to_string(chain);
        chain = 0;
      }
    }
    cout << T << endl;
  }
}
Problem p00629 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  vector<int> I, U, A, P, C;

  while (true) {
    cin >> N;

    if (N == 0) {
      break;
    }

    for (int i = 0; i < N; i++) {
      cin >> I[i] >> U[i] >> A[i] >> P[i];
    }

    C = vector<int>(1001, 0);

    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (A[i] < A[j] || (A[i] == A[j] && P[i] > P[j])) {
          swap(A[i], A[j]);
          swap(P[i], P[j]);
          swap(I[i], I[j]);
          swap(U[i], U[j]);
        }
      }
    }

    int count_ = 0;

    for (int i = 0; i < N; i++) {
      if ((count_ < 10 && C[U[i]] < 3) || (count_ < 20 && C[U[i]] < 2) ||
          (count_ < 26 && C[U[i]] == 0)) {
        cout << I[i] << endl;

        C[U[i]]++;
        count_++;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  vector<int> I, U, A, P, C;

  while (true) {
    cin >> N;

    if (N == 0) {
      break;
    }

    I = vector<int>(N);
    U = vector<int>(N);
    A = vector<int>(N);
    P = vector<int>(N);

    for (int i = 0; i < N; i++) {
      cin >> I[i] >> U[i] >> A[i] >> P[i];
    }

    C = vector<int>(1001, 0);

    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (A[i] < A[j] || (A[i] == A[j] && P[i] > P[j])) {
          swap(A[i], A[j]);
          swap(P[i], P[j]);
          swap(I[i], I[j]);
          swap(U[i], U[j]);
        }
      }
    }

    int count_ = 0;

    for (int i = 0; i < N; i++) {
      if ((count_ < 10 && C[U[i]] < 3) || (count_ < 20 && C[U[i]] < 2) ||
          (count_ < 26 && C[U[i]] == 0)) {
        cout << I[i] << endl;

        C[U[i]]++;
        count_++;
      }
    }
  }

  return 0;
}
Problem p00630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

void UpperCamelCase(string &);
void LowerCamelCase(string &);
void UnderScoreCase(string &);
bool IsUpperCase(char);

int main() {
  char c;
  string str;

  while (1) {
    cin >> str >> c;

    if (c == 'X')
      break;
    else if (c == 'U')
      UpperCamelCase(str);
    else if (c == 'L')
      LowerCamelCase(str);
    else if (c == 'D')
      UnderScoreCase(str);

    cout << str << endl;
  }

  return 0;
}

void UpperCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'a' && s[0] <= 'z')
    s[0] = toupper(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void LowerCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void UnderScoreCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find_if(i, s.end(), IsUpperCase);
    if (i == s.end())
      break;
    *i = tolower(*i);
    s.insert(i, '_');
  }
}

bool IsUpperCase(char c) {
  if (c >= 'A' && c <= 'Z')
    return true;
  else
    return false;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

void UpperCamelCase(string &);
void LowerCamelCase(string &);
void UnderScoreCase(string &);
bool IsUpperCase(char);

int main() {
  char c;
  string str;

  while (1) {
    cin >> str >> c;

    if (c == 'X')
      break;
    else if (c == 'U')
      UpperCamelCase(str);
    else if (c == 'L')
      LowerCamelCase(str);
    else if (c == 'D')
      UnderScoreCase(str);

    cout << str << endl;
  }

  return 0;
}

void UpperCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'a' && s[0] <= 'z')
    s[0] = toupper(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void LowerCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void UnderScoreCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find_if(i, s.end(), IsUpperCase);
    if (i == s.end())
      break;
    *i = tolower(*i);
    i = s.insert(i, '_');
    ++i;
  }
}

bool IsUpperCase(char c) {
  if (c >= 'A' && c <= 'Z')
    return true;
  else
    return false;
}
Problem p00630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
bool C(char c) { return isupper(c); }
int main() {
  string s;
  string::iterator i;
  char c;
  int x;
  while (cin >> s >> c, c - 'X') {
    s[0] = toupper(s[0]);
    while (x = s.find('_'), x != string::npos)
      s.erase(x, 1), s[x] = toupper(s[x]);
    if (c - 'U')
      s[0] = tolower(s[0]);
    if (c == 'D')
      for (i = s.begin(); (i = find_if(i, s.end(), ptr_fun(C))) != s.end(); ++i)
        *i = tolower(*i), s.insert(i, '_');
    cout << s << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
bool C(char c) { return isupper(c); }
int main() {
  string s;
  string::iterator i;
  char c;
  int x;
  while (cin >> s >> c, c - 'X') {
    s[0] = toupper(s[0]);
    while (x = s.find('_'), x != string::npos)
      s.erase(x, 1), s[x] = toupper(s[x]);
    if (c - 'U')
      s[0] = tolower(s[0]);
    if (c == 'D')
      for (i = s.begin(); (i = find_if(i, s.end(), ptr_fun(C))) != s.end(); ++i)
        *i = tolower(*i), i = s.insert(i, '_');
    cout << s << endl;
  }
  return 0;
}
Problem p00631 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, sum;
  vector<int> a;

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    a = vector<int>(n);
    sum = 0;

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    int min_ = (1 << 29);

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

      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          sum2 += a[j];
        }
      }

      cout << sum2 << endl;

      min_ = min(abs(2 * sum2 - sum), min_);
    }

    cout << min_ << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, sum;
  vector<int> a;

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    a = vector<int>(n);
    sum = 0;

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    int min_ = (1 << 29);

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

      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          sum2 += a[j];
        }
      }

      min_ = min(abs(2 * sum2 - sum), min_);
    }

    cout << min_ << endl;
  }

  return 0;
}
Problem p00632 (C++)

Buggy submission (status: Time Limit Exceeded):
#import <queue>
#import <stdio.h>
int main() {
  std::queue<int> q;
  int w, h, f[484], i, j, a, b, t, x, z;
  char p[15];
  for (; scanf("%d%d ", &h, &w), h;) {
    for (j = 0; j < w + 2; j++)
      f[j] = f[j - ~h * 22] = -1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++)
        f[x = i * 22 + j] = -2, z = getchar(),
                       z - 35 ? z > 65   ? b = x
                                : z > 64 ? a = x
                                         : 1
                              : --f[x];
      f[i * 22] = f[i * 22 - ~w] = -9;
      getchar();
    }
    f[a] = t = 0;
    q.push(0);
    for (q.push(a); a = q.front(), q.size() > 1; q.pop())
      if (!a)
        ++t, q.push(0);
      else
        for (i = 4; i--;)
          if (f[x = a + " 57L"[i] - 54] == -2)
            f[x] = t, q.push(x);
    q.pop();
    scanf("%s", p);
    for (a = t = 0; !a;) {
      x = b;
      z = 0;
      for (i = 0; !a && p[i]; f[b] >= 0 && ++z)
        ++t, ~f[j = b - 54 + "L65676 "[p[i++] - 50]] ? b = j : 0,
                                                       f[b] >= 0 & t >= f[b]
                                                           ? a = b
                                                           : 0;
      !z & !a & !p[i] & b == x & f[b] < 0 && --a;
    }
    printf(~a ? "%d %d %d\n" : "impossible\n", t, a / 22 - 1, a % 22 - 1);
  }
}

Fixed submission:
#import <queue>
#import <stdio.h>
int main() {
  std::queue<int> q;
  int w, h, f[484], i, j, a, b, t, x, z;
  char p[15];
  for (; scanf("%d%d ", &h, &w), h;) {
    for (j = 0; j < w + 2; j++)
      f[j] = f[j - ~h * 22] = -1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++)
        f[x = i * 22 + j] = -2, z = getchar(),
                       z - 35 ? z > 65   ? b = x
                                : z > 64 ? a = x
                                         : 1
                              : --f[x];
      f[i * 22] = f[i * 22 - ~w] = -1;
      getchar();
    }
    f[a] = t = 0;
    q.push(0);
    for (q.push(a); a = q.front(), q.size() > 1; q.pop())
      if (!a)
        ++t, q.push(0);
      else
        for (i = 4; i--;)
          if (f[x = a + " 57L"[i] - 54] == -2)
            f[x] = t, q.push(x);
    q.pop();
    scanf("%s", p);
    for (a = t = 0; !a;) {
      x = b;
      z = 0;
      for (i = 0; !a && p[i]; f[b] >= 0 && ++z)
        ++t, ~f[j = b - 54 + "L65676 "[p[i++] - 50]] ? b = j : 0,
                                                       f[b] >= 0 & t >= f[b]
                                                           ? a = b
                                                           : 0;
      !z & !a & !p[i] & b == x & f[b] < 0 && --a;
    }
    printf(~a ? "%d %d %d\n" : "impossible\n", t, a / 22 - 1, a % 22 - 1);
  }
}
Problem p00638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int n;
  while (1) {
    vector<pair<int, int>> v;
    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> v[i].second >> v[i].first;
    }
    sort(v.begin(), v.end());

    int ans = 0;
    int judge = 0;
    for (int i = 0; i < n; i++) {
      ans += v[i].second;
      if (ans > v[i].first) {
        judge = 1;
        break;
      }
    }

    if (judge == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

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

int main() {

  int n;
  while (1) {
    vector<pair<int, int>> v;
    cin >> n;
    if (n == 0)
      break;
    v.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i].second >> v[i].first;
    }
    sort(v.begin(), v.end());

    int ans = 0;
    int judge = 0;
    for (int i = 0; i < n; i++) {
      ans += v[i].second;
      if (ans > v[i].first) {
        judge = 1;
        break;
      }
    }

    if (judge == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p00638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int main() {
  for (int N; cin >> N, N;) {
    vector<P> v;
    for (int i = 0; i < N; i++) {
      int a, b;
      cin >> a >> b;
      v.push_back(make_pair(b, a));
    }
    sort(v.begin(), v.end());

    vector<int> w1, w2;
    for (int i = 0; i < N; i++) {
      w1.push_back(v[i].first);
      w2.push_back(v[i].second);
    }
    partial_sum(w2.begin(), w2.end(), w2.begin());

    cerr << "-----" << endl;
    for (int i : w2)
      cerr << i << endl;
    cerr << "-----" << endl;

    bool f = true;
    for (int i = 0; i < N; i++)
      if (w1[i] < w2[i])
        f = false;
    cout << (f ? "Yes" : "No") << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int main() {
  for (int N; cin >> N, N;) {
    vector<P> v;
    for (int i = 0; i < N; i++) {
      int a, b;
      cin >> a >> b;
      v.push_back(make_pair(b, a));
    }
    sort(v.begin(), v.end());

    vector<int> w1, w2;
    for (int i = 0; i < N; i++) {
      w1.push_back(v[i].first);
      w2.push_back(v[i].second);
    }
    partial_sum(w2.begin(), w2.end(), w2.begin());

    bool f = true;
    for (int i = 0; i < N; i++)
      if (w1[i] < w2[i])
        f = false;
    cout << (f ? "Yes" : "No") << endl;
  }
}
Problem p00640 (C++)

Buggy submission (status: Runtime Error):
// AOJ1054 Distorted Love

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  int buf[100];
  string name[100];
  int b[100], x1[100][100], y1[100][100], x2[100][100], y2[100][100],
      ptr[100][100];
  string link[100][100];
  while (cin >> n, n) {
    int x, y;
    cin >> x >> y;
    int cur = 0, size = 0;
    buf[cur] = 0;
    for (int i = 0; i < n; i++) {
      cin >> name[i] >> b[i];
      for (int j = 0; j < b[i]; j++)
        cin >> x1[i][j] >> y1[i][j] >> x2[i][j] >> y2[i][j] >> link[i][j];
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < b[j]; k++)
          if (link[j][k] == name[i])
            ptr[j][k] = i;
    cin >> n;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      if (s == "click") {
        cin >> x >> y;
        for (int j = 0; j < b[buf[cur]]; j++) {
          if (x1[buf[cur]][j] <= x && x <= x2[buf[cur]][j] &&
              y1[buf[cur]][j] <= y && y <= y2[buf[cur]][j]) {
            cur++;
            size = cur;
            buf[cur] = ptr[buf[cur - 1]][j];
            break;
          }
        }
      }
      if (s == "forward" && cur != size)
        cur++;
      if (s == "back" && cur != 0)
        cur--;
      if (s == "show")
        cout << name[buf[cur]] << endl;
    }
  }
}

Fixed submission:
// AOJ1054 Distorted Love

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  int buf[1000000];
  string name[100];
  int b[100], x1[100][100], y1[100][100], x2[100][100], y2[100][100],
      ptr[100][100];
  string link[100][100];
  while (cin >> n, n) {
    int x, y;
    cin >> x >> y;
    int cur = 0, size = 0;
    buf[cur] = 0;
    for (int i = 0; i < n; i++) {
      cin >> name[i] >> b[i];
      for (int j = 0; j < b[i]; j++)
        cin >> x1[i][j] >> y1[i][j] >> x2[i][j] >> y2[i][j] >> link[i][j];
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < b[j]; k++)
          if (link[j][k] == name[i])
            ptr[j][k] = i;
    cin >> n;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      if (s == "click") {
        cin >> x >> y;
        for (int j = 0; j < b[buf[cur]]; j++) {
          if (x1[buf[cur]][j] <= x && x <= x2[buf[cur]][j] &&
              y1[buf[cur]][j] <= y && y <= y2[buf[cur]][j]) {
            cur++;
            size = cur;
            buf[cur] = ptr[buf[cur - 1]][j];
            break;
          }
        }
      }
      if (s == "forward" && cur != size)
        cur++;
      if (s == "back" && cur != 0)
        cur--;
      if (s == "show")
        cout << name[buf[cur]] << endl;
    }
  }
}
Problem p00640 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int N, W, H, Q, x, y, c[109], xa[109][109], ya[109][109], xb[109][109],
    yb[109][109];
string s[109], t[109][109], tp;
int main() {
  while (cin >> N, N) {
    cin >> W >> H;
    for (int i = 0; i < N; i++) {
      cin >> s[i] >> c[i];
      for (int j = 0; j < c[i]; j++) {
        cin >> xa[i][j] >> ya[i][j] >> xb[i][j] >> yb[i][j] >> t[i][j];
      }
    }
    cin >> Q;
    vector<int> res;
    res.push_back(0);
    int pos = 0;
    for (int i = 0; i < Q; i++) {
      cin >> tp;
      if (tp == "click") {
        cin >> x >> y;
        for (int j = 0; j < c[res[pos]]; j++) {
          if (xa[res[pos]][j] <= x && x <= xb[res[pos]][j] &&
              ya[res[pos]][j] <= y && y <= yb[res[pos]][j]) {
            int ptr = -1;
            for (int k = 0; k < c[res[pos]]; k++) {
              if (s[k] == t[res[pos]][j])
                ptr = k;
            }
            res.resize(pos + 2);
            res[++pos] = ptr;
            break;
          }
        }
      }
      if (tp == "show")
        cout << s[res[pos]] << endl;
      if (tp == "forward" && pos + 1 != res.size())
        pos++;
      if (tp == "back" && pos != 0)
        pos--;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int N, W, H, Q, x, y, c[109], xa[109][109], ya[109][109], xb[109][109],
    yb[109][109];
string s[109], t[109][109], tp;
int main() {
  while (cin >> N, N) {
    cin >> W >> H;
    for (int i = 0; i < N; i++) {
      cin >> s[i] >> c[i];
      for (int j = 0; j < c[i]; j++) {
        cin >> xa[i][j] >> ya[i][j] >> xb[i][j] >> yb[i][j] >> t[i][j];
      }
    }
    cin >> Q;
    vector<int> res;
    res.push_back(0);
    int pos = 0;
    for (int i = 0; i < Q; i++) {
      cin >> tp;
      if (tp == "click") {
        cin >> x >> y;
        for (int j = 0; j < c[res[pos]]; j++) {
          if (xa[res[pos]][j] <= x && x <= xb[res[pos]][j] &&
              ya[res[pos]][j] <= y && y <= yb[res[pos]][j]) {
            int ptr = -1;
            for (int k = 0; k < N; k++) {
              if (s[k] == t[res[pos]][j])
                ptr = k;
            }
            res.resize(pos + 2);
            res[++pos] = ptr;
            break;
          }
        }
      }
      if (tp == "show")
        cout << s[res[pos]] << endl;
      if (tp == "forward" && pos + 1 != res.size())
        pos++;
      if (tp == "back" && pos != 0)
        pos--;
    }
  }
  return 0;
}
Problem p00642 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  cout << setprecision(7) << fixed;
  vector<vector<ld>> dp(100005, vector<ld>(50));
  dp[0][0] = 1;
  for (int n = 0; n < 100004; ++n) {
    for (int i = 0; i < 48; ++i) {
      dp[n + 1][i + 1] += dp[n][i] / pow(2, i);
      dp[n + 1][0] += dp[n][i] * (1 - 1 / pow(2, i));
    }
  }
  vector<ld> anss(100001);
  for (int i = 0; i < 100001; ++i) {
    anss[i + 1] = anss[i];
    for (int j = 1; j < 50; ++j) {
      anss[i + 1] += dp[i + 1][j];
    }
  }
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    cout << anss[N] << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  cout << setprecision(7) << fixed;
  vector<vector<ld>> dp(100005, vector<ld>(50));
  dp[0][0] = 1;
  for (int n = 0; n < 100004; ++n) {
    for (int i = 0; i < 48; ++i) {
      dp[n + 1][i + 1] += dp[n][i] / pow(2, i);
      dp[n + 1][0] += dp[n][i] * (1 - 1 / pow(2, i));
    }
  }
  vector<ld> anss(100001);
  for (int i = 0; i < 100001; ++i) {
    anss[i + 1] = anss[i];
    for (int j = 1; j < 50; ++j) {
      anss[i + 1] += dp[i + 1][j];
    }
  }
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    cout << anss[N] << endl;
  }

  return 0;
}
Problem p00642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    double memo1[40] = {};
    double memo2[40] = {};
    double ans = 0;

    memo1[0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 39; j++) {
        if (memo1[j] == 0)
          continue;
        double getexp = memo1[j] * pow(2, -j);
        memo2[j + 1] += getexp;
        memo2[0] += memo1[j] - getexp;
        ans += getexp;
        memo1[j] = 0;
      }
      swap(memo1, memo2);
    }

    printf("%f\n", ans);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    double memo1[40] = {};
    double memo2[40] = {};
    double ans = 0;

    memo1[0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 35; j++) {
        if (memo1[j] == 0)
          continue;
        double getexp = memo1[j] * pow(2, -j);
        memo2[j + 1] += getexp;
        memo2[0] += memo1[j] - getexp;
        ans += getexp;
        memo1[j] = 0;
      }
      swap(memo1, memo2);
    }

    printf("%f\n", ans);
  }

  return 0;
}
Problem p00642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    long long int foo = 1;
    int bar = 1;
    for (int j = i + 1; j < n + 1 and j < n + 30; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\n", ans);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    long long int foo = 1;
    long long int bar = 1;
    for (int j = i + 1; j < n + 1 and foo < 1e08; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\n", ans);
  }
  return 0;
}
Problem p00642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    int foo = 1;
    int bar = 1;
    for (int j = i + 1; j < n + 1 and foo < 1000000000; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\n", ans);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    long long int foo = 1;
    int bar = 1;
    for (int j = i + 1; j < n + 1 and foo < 1000000000; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\n", ans);
  }
  return 0;
}
Problem p00642 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
const int N = 10, ONE_KUURU = 12;

vector<double> E;

void init() {
  E.assign(N + 1, 0.0);
  vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1));
  dp[1][0] = 1.0;
  for (int i = 1; i <= N; ++i) {
    E[i] += E[i - 1];
    rep(j, ONE_KUURU) {
      dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j));
      dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j));
      E[i] += dp[i][j] * (1.0 / (1 << j));
    }
  }
}

int main() {
  init();
  for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\n')
    ;
  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
const int N = 100000, ONE_KUURU = 12;

vector<double> E;

void init() {
  E.assign(N + 1, 0.0);
  vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1));
  dp[1][0] = 1.0;
  for (int i = 1; i <= N; ++i) {
    E[i] += E[i - 1];
    rep(j, ONE_KUURU) {
      dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j));
      dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j));
      E[i] += dp[i][j] * (1.0 / (1 << j));
    }
  }
}

int main() {
  init();
  for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\n')
    ;
  return 0;
}
Problem p00642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;

typedef unsigned long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<string, map<string, int>> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;

double getb[200000];
double dp[100001][30];

int main() {
  int n;
  fill(&dp[0][0], &dp[199999][49] + 1, 0.0);
  dp[1][1] = 1.0;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < 29; j++) {
      dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j);
      dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j));
    }
  }
  getb[0] = 0;
  for (int i = 1; i <= 100000; i++) {
    getb[i] = 0.0;
    for (int j = 1; j < 30; j++) {
      getb[i] += dp[i][j];
    }
    getb[i] += getb[i - 1];
  }
  while (cin >> n && n) {
    cout << fixed << setprecision(9) << getb[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;

typedef unsigned long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<string, map<string, int>> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;

double getb[200000];
double dp[100001][30];

int main() {
  int n;
  fill(&dp[0][0], &dp[100000][29] + 1, 0.0);
  dp[1][1] = 1.0;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < 29; j++) {
      dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j);
      dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j));
    }
  }
  getb[0] = 0;
  for (int i = 1; i <= 100000; i++) {
    getb[i] = 0.0;
    for (int j = 1; j < 30; j++) {
      getb[i] += dp[i][j];
    }
    getb[i] += getb[i - 1];
  }
  while (cin >> n && n) {
    cout << fixed << setprecision(9) << getb[n] << endl;
  }
  return 0;
}
Problem p00646 (C++)

Buggy submission (status: Time Limit Exceeded):
// 00
#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool cmp[1000001] = {};
  vector<int> prm;
  for (int i = 2; i <= 1000000; i++) {
    if (!cmp[i]) {
      prm.push_back(i);
      for (int j = 2; j * i <= 1000000; j++) {
        cmp[i * j] = true;
      }
    }
  }
  for (long long l; cin >> l, l;) {
    int a = 1;
    for (int i = 0; i < prm.size(); i++) {
      int c = 0;
      while (l % prm[i] == 0) {
        c++;
        l /= prm[i];
      }
      a *= c * 2 + 1;
    }
    if (l != 1) {
      a *= 3;
    }
    cout << (a + 1) / 2 << endl;
  }
  return 0;
}

Fixed submission:
// 00
#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool cmp[1000001] = {};
  vector<int> prm;
  for (int i = 2; i <= 1000000; i++) {
    if (!cmp[i]) {
      prm.push_back(i);
      for (int j = 2; j * i <= 1000000; j++) {
        cmp[i * j] = true;
      }
    }
  }
  for (long long l; cin >> l, l;) {
    int a = 1;
    for (int i = 0; l != 1 && i < prm.size(); i++) {
      int c = 0;
      while (l % prm[i] == 0) {
        c++;
        l /= prm[i];
      }
      a *= c * 2 + 1;
    }
    if (l != 1) {
      a *= 3;
    }
    cout << (a + 1) / 2 << endl;
  }
  return 0;
}
Problem p00646 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  vector<ll> divisors;
  divisors.reserve(4096);
  while (true) {
    ll L;
    cin >> L;
    if (L == 0) {
      break;
    }
    divisors.clear();
    for (ll i = 1; i * i <= L; ++i) {
      if (L % i != 0) {
        continue;
      }
      divisors.push_back(i);
      if (i * i != L) {
        divisors.push_back(L / i);
      }
    }
    sort(divisors.begin(), divisors.end());
    int answer = 0;
    for (int i = 0; i < divisors.size(); ++i) {
      ll a = divisors[i];
      for (int j = i; j < divisors.size(); ++j) {
        ll b = divisors[j];
        ll lcm = a * (b / __gcd(a, b));
        if (lcm == L) {
          ++answer;
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  vector<ll> divisors;
  divisors.reserve(4096);
  while (true) {
    ll L;
    cin >> L;
    if (L == 0) {
      break;
    }
    divisors.clear();
    for (ll i = 1; i * i <= L; ++i) {
      if (L % i != 0) {
        continue;
      }
      divisors.push_back(i);
      if (i * i != L) {
        divisors.push_back(L / i);
      }
    }
    sort(divisors.begin(), divisors.end());
    int answer = 0;
    for (int i = 0; i < divisors.size(); ++i) {
      ll b = divisors[i];
      vector<ll>::iterator it =
          lower_bound(divisors.begin(), divisors.end(), L / b);
      for (int j = it - divisors.begin(); j <= i; ++j) {
        ll a = divisors[j];
        if (a * (b / __gcd(a, b)) == L) {
          ++answer;
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}
Problem p00646 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> fact(ll L) {
  ll N = L;
  vector<int> res;
  for (ll i = 2; i * i <= N; i++)
    if (L % i == 0) {
      int cnt = 0;
      while (L % i == 0) {
        L /= i;
        cnt++;
      }
      res.push_back(cnt);
    }
  if (L != 1)
    res.push_back(1);
  return res;
}

int main() {
  ll L;
  while (cin >> L && L) {
    vector<int> v = fact(L);
    int dp[2] = {};
    dp[1] = v[0];
    for (int i = 1; i < v.size(); i++) {
      dp[(i + 1) & 1] = dp[i & 1] * 2 * v[i] + v[i] + dp[i & 1];
    }
    cout << dp[v.size() & 1] + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> fact(ll L) {
  ll N = L;
  vector<int> res;
  for (ll i = 2; i * i <= N; i++)
    if (L % i == 0) {
      int cnt = 0;
      while (L % i == 0) {
        L /= i;
        cnt++;
      }
      res.push_back(cnt);
    }
  if (L != 1)
    res.push_back(1);
  return res;
}

int main() {
  ll L;
  while (cin >> L && L) {
    if (L == 1) {
      cout << 1 << endl;
      continue;
    }
    vector<int> v = fact(L);
    int dp[2] = {};
    dp[1] = v[0];
    for (int i = 1; i < v.size(); i++) {
      dp[(i + 1) & 1] = dp[i & 1] * 2 * v[i] + v[i] + dp[i & 1];
    }
    cout << dp[v.size() & 1] + 1 << endl;
  }
  return 0;
}
Problem p00646 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> so, v;
ll ans;
void dfs(int d, int t) {
  if (d == v.size()) {
    ans += t;
    return;
  }
  for (int i = 0; i <= v[d]; i++) {
    int nt = t;
    if (v[d] == i)
      nt *= i + 1;
    dfs(d + 1, nt);
  }
}

int main() {
  ll n;
  while (cin >> n && n) {
    ans = 1;
    v.clear();
    ll tmp = n;
    for (int i = 2; i * i <= n; i++)
      if (tmp % i == 0) {
        v.push_back(0);
        while (tmp % i == 0)
          tmp /= i, v[v.size() - 1]++;
      }
    if (tmp > 1)
      v.push_back(1);
    dfs(0, 1);
    cout << ans / 2 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> so, v;
ll ans;
void dfs(int d, int t) {
  if (d == v.size()) {
    ans += t;
    return;
  }
  for (int i = 0; i <= v[d]; i++) {
    int nt = t;
    if (v[d] == i)
      nt *= i + 1;
    dfs(d + 1, nt);
  }
}

int main() {
  ll n;
  while (cin >> n && n) {
    ans = 1;
    v.clear();
    ll tmp = n;
    for (ll i = 2; i * i <= n; i++)
      if (tmp % i == 0) {
        v.push_back(0);
        while (tmp % i == 0)
          tmp /= i, v[v.size() - 1]++;
      }
    if (tmp > 1)
      v.push_back(1);
    dfs(0, 1);
    cout << ans / 2 << endl;
  }
  return 0;
}
Problem p00654 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define all(a) (a).begin(), (a).end()
#define EPS 1e-8
using namespace std;

typedef long long ll;

int main() {
  ll n, b;
  vector<ll> odd, even;
  while (cin >> n, n) {
    odd.clear();
    even.clear();
    for (int i = 0; i < n * (n + 1) / 2; i++) {
      cin >> b;
      if (b & 1)
        odd.push_back(b);
      else
        even.push_back(b);
    }

    sort(all(odd));
    sort(all(even));

    ll tmp = __gcd(even[0], even[1]);
    for (ll i = 2; i < n; i++)
      tmp = __gcd(tmp, even[i]);

    ll t0 = even[0] / tmp, t1 = even[1] / tmp;

    ll hoge = odd[0] / (t0 * t1);

    ll sq;
    for (sq = 0; sq * sq != hoge; sq++)
      ;
    tmp /= sq;

    cout << tmp << endl;
    for (int i = 0; i < n - 1; i++)
      cout << even[i] / tmp << " ";
    cout << even[n - 1] / tmp << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define all(a) (a).begin(), (a).end()
#define EPS 1e-8
using namespace std;

typedef long long ll;

int main() {
  ll n, b;
  vector<ll> odd, even;
  while (cin >> n, n) {
    odd.clear();
    even.clear();
    for (int i = 0; i < n * (n + 1) / 2; i++) {
      cin >> b;
      if (b & 1)
        odd.push_back(b);
      else
        even.push_back(b);
    }

    sort(all(odd));
    sort(all(even));

    ll tmp = __gcd(even[0], even[1]);
    for (ll i = 2; i < n; i++)
      tmp = __gcd(tmp, even[i]);

    ll t0 = even[0] / tmp, t1 = even[1] / tmp;

    ll hoge = odd[0] / (t0 * t1);

    ll sq = (ll)(sqrt(hoge) + EPS);
    // for(sq=0;sq*sq!=hoge;sq++);
    tmp /= sq;

    cout << tmp << endl;
    for (int i = 0; i < n - 1; i++)
      cout << even[i] / tmp << " ";
    cout << even[n - 1] / tmp << endl;
  }
}