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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

void brute_force(string T, string P) {
  const char *Tc = T.c_str();
  const char *Pc = P.c_str();
  int i, j;
  for (i = 0; i < T.length() - P.length() + 1; i++) {
    for (j = 0; j < P.length(); j++) {
      if (Tc[i + j] != Pc[j])
        break;
      if (j == P.length() - 1)
        cout << i << endl;
    }
  }
}

int main() {
  string T, P;
  cin >> T >> P;
  brute_force(T, P);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

void brute_force(string T, string P) {
  const char *Tc = T.c_str();
  const char *Pc = P.c_str();
  int i, j;
  for (i = 0; i < (int)T.length() - (int)P.length() + 1; i++) {
    for (j = 0; j < P.length(); j++) {
      if (Tc[i + j] != Pc[j])
        break;
      if (j == P.length() - 1)
        cout << i << endl;
    }
  }
}

int main() {
  string T, P;
  cin >> T >> P;
  brute_force(T, P);
  return 0;
}
Problem p02247 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

string s, t;

int main() {
  cin >> s >> t;
  for (int i = 0; i < s.size() - t.size() + 1; i++) {
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] != t[j]) {
        break;
      }
      if (j == t.size() - 1 && s[i + j] == t[j]) {
        cout << i << endl;
      }
    }
  }
  return 0;
}

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

string s, t;

int main() {
  cin >> s >> t;
  if (s.size() < t.size()) {
    return 0;
  }
  for (int i = 0; i < s.size() - t.size() + 1; i++) {
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] != t[j]) {
        break;
      }
      if (j == t.size() - 1 && s[i + j] == t[j]) {
        cout << i << endl;
      }
    }
  }
  return 0;
}
Problem p02247 (C++)

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

using namespace std;

int main() {
  string S, T;

  cin >> S >> T;

  for (int i = 0; i <= S.size() - T.size(); i++) {
    if (T == S.substr(i, T.size())) {
      cout << i << endl;
    }
  }

  return 0;
}

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

using namespace std;

int main() {
  string S, T;

  cin >> S >> T;

  if (S.size() >= T.size()) {
    for (int i = 0; i <= S.size() - T.size(); i++) {
      if (T == S.substr(i, T.size())) {
        cout << i << endl;
      }
    }
  }

  return 0;
}
Problem p02247 (C++)

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

#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main(void) {
  string t;
  string p;
  cin >> t;
  cin >> p;
  rep(i, t.size() - p.size() + 1) {
    rep(j, p.size()) {
      if (i + j >= t.size() || t[i + j] != p[j])
        break;
      ;
      if (j == p.size() - 1) {
        cout << i << endl;
      }
    }
  }

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main(void) {
  string t;
  string p;
  cin >> t;
  cin >> p;
  rep(i, (int)t.size() - (int)p.size() + 1) {
    rep(j, p.size()) {
      if (i + j >= t.size() || t[i + j] != p[j])
        break;
      ;
      if (j == p.size() - 1) {
        cout << i << endl;
      }
    }
  }

  return 0;
}
Problem p02247 (C++)

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

using namespace std;

struct RollingHash {
  typedef long long int_type;
  typedef pair<int_type, int_type> hash_type;

  int_type base, mod[2];

  vector<int_type> hashed[2], power[2];

  RollingHash() { base = 9973LL, mod[0] = 999999937LL, mod[1] = 1000000007LL; }

  void init(const string &s) {
    int sz = s.size();

    for (int i = 0; i < 2; i++) {
      hashed[i].assign(sz + 1, 0);
      power[i].assign(sz + 1, 0);

      hashed[i][0] = 0;
      power[i][0] = 1;
      for (int j = 0; j < sz; j++) {
        power[i][j + 1] = power[i][j] * base % mod[i];
        hashed[i][j + 1] = (hashed[i][j] + s[j]) * base % mod[i];
      }
    }
  }

  hash_type get(int l, int r) {
    int_type hash1 =
        ((hashed[0][r] - hashed[0][l] * power[0][r - l]) % mod[0] + mod[0]) %
        mod[0];
    int_type hash2 =
        ((hashed[1][r] - hashed[1][l] * power[1][r - l]) % mod[1] + mod[1]) %
        mod[1];
    return hash_type(hash1, hash2);
  }

  hash_type concat(hash_type hash1, hash_type hash2, int hash2_len) {
    return hash_type((hash1.first * power[0][hash2_len] + hash2.first) % mod[0],
                     (hash1.second * power[1][hash2_len] + hash2.second) %
                         mod[1]);
  }
};

int main() {
  string T, P;
  cin >> T >> P;

  RollingHash rollhash[2];
  rollhash[0].init(T);
  rollhash[1].init(P);

  for (int i = 0; i <= T.size() - P.size(); i++) {
    if (rollhash[0].get(i, i + P.size()) == rollhash[1].get(0, P.size()))
      cout << i << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

struct RollingHash {
  typedef long long int_type;
  typedef pair<int_type, int_type> hash_type;

  int_type base, mod[2];

  vector<int_type> hashed[2], power[2];

  RollingHash() { base = 9973LL, mod[0] = 999999937LL, mod[1] = 1000000007LL; }

  void init(const string &s) {
    int sz = s.size();

    for (int i = 0; i < 2; i++) {
      hashed[i].assign(sz + 1, 0);
      power[i].assign(sz + 1, 0);

      hashed[i][0] = 0;
      power[i][0] = 1;
      for (int j = 0; j < sz; j++) {
        power[i][j + 1] = power[i][j] * base % mod[i];
        hashed[i][j + 1] = (hashed[i][j] + s[j]) * base % mod[i];
      }
    }
  }

  hash_type get(int l, int r) {
    int_type hash1 =
        ((hashed[0][r] - hashed[0][l] * power[0][r - l]) % mod[0] + mod[0]) %
        mod[0];
    int_type hash2 =
        ((hashed[1][r] - hashed[1][l] * power[1][r - l]) % mod[1] + mod[1]) %
        mod[1];
    return hash_type(hash1, hash2);
  }

  hash_type concat(hash_type hash1, hash_type hash2, int hash2_len) {
    return hash_type((hash1.first * power[0][hash2_len] + hash2.first) % mod[0],
                     (hash1.second * power[1][hash2_len] + hash2.second) %
                         mod[1]);
  }
};

int main() {
  string T, P;
  cin >> T >> P;

  RollingHash rollhash[2];
  rollhash[0].init(T);
  rollhash[1].init(P);

  for (int i = 0; i <= (int)(T.size() - P.size()); i++) {
    if (rollhash[0].get(i, i + P.size()) == rollhash[1].get(0, P.size()))
      cout << i << endl;
  }

  return 0;
}
Problem p02248 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string t, p;
  cin >> t >> p;
  int tl = t.length();
  int pl = p.length();
  int skip[256];
  for (int i = 0; i < 256; i++)
    skip[i] = pl;
  for (int i = 0; i < pl - 1; i++)
    skip[(unsigned)p[i]] = pl - 1 - i;
  for (int pos = 0; pos <= tl - pl; pos += skip[(unsigned)t[pos + pl - 1]]) {
    for (int i = pl - 1; i >= 0; i--) {
      if (t[pos + i] != p[i])
        break;
      if (i == 0)
        cout << pos << '\n';
    }
  }
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string t, p;
  cin >> t >> p;
  for (int pos = 0; pos < t.length(); pos++) {
    pos = t.find(p, pos);
    if (pos == string::npos)
      break;
    cout << pos << '\n';
  }
  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;

void Rolling_Hash(const string &a, const string &b) {
  const ull B = 100000007;
  int al = a.length(), bl = b.length();
  if (al > bl)
    return;

  ull t = 1;
  for (int i = 0; i < al; i++)
    t *= B;

  ull ah = 0, bh = 0;
  for (int i = 0; i < al; i++)
    ah = ah * B + a[i];
  for (int i = 0; i < al; i++)
    bh = bh * B + b[i];

  for (int i = 0; i + al <= bl; i++) {
    if (ah == bh && a == b.substr(i, al))
      cout << i << endl;
    if (i + al < bl)
      bh = bh * B + b[i + al] - b[i] * t;
  }
}

int main() {
  string T, P;
  cin >> T >> P;
  Rolling_Hash(P, T);
  return 0;
}

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

void Rolling_Hash(const string &a, const string &b) {
  const ull B = 100000007;
  int al = a.length(), bl = b.length();
  if (al > bl)
    return;

  ull t = 1;
  for (int i = 0; i < al; i++)
    t *= B;

  ull ah = 0, bh = 0;
  for (int i = 0; i < al; i++)
    ah = ah * B + a[i];
  for (int i = 0; i < al; i++)
    bh = bh * B + b[i];

  for (int i = 0; i + al <= bl; i++) {
    if (ah == bh)
      cout << i << endl;
    if (i + al < bl)
      bh = bh * B + b[i + al] - b[i] * t;
  }
}

int main() {
  string T, P;
  cin >> T >> P;
  Rolling_Hash(P, T);
  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define base 950527
using namespace std;
typedef unsigned long long ll;
map<ll, ll> map;

int main() {
  string a, b;
  cin >> a >> b;

  ll hs = 0, n = 0, t = 1;
  for (int i = 0; i < b.size(); i++) {
    hs = hs * base + b[i];
    n = n * base + a[i];
    t *= base;
  }

  if (hs == n)
    cout << 0 << endl;
  for (int i = b.size(); i < a.size(); i++) {
    n = n * base - t * a[i - b.size()];
    n += a[i];
    if (hs == n)
      cout << i - b.size() + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define base 950527
using namespace std;
typedef unsigned long long ll;
map<ll, ll> map;

int main() {
  string a, b;
  cin >> a >> b;

  ll hs = 0, n = 0, t = 1;
  for (int i = 0; i < b.size(); i++) {
    hs = hs * base + b[i];
    n = n * base + a[i];
    t *= base;
  }

  if (hs == n)
    cout << 0 << endl;
  for (int i = b.size(); i < a.size(); i++) {
    n = n * base - t * a[i - b.size()];
    n += a[i];
    if (hs == n)
      printf("%d\n", i - (int)b.size() + 1);
  }
  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long lli;
typedef unsigned long long ull;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

string t;
string p;

const ull b = 1000000007;
ull hsh = 0;
ull cmp = 0;
vll vb;

int main() {
  cin >> t >> p;
  vb = vll(p.size());
  vb[0] = 1;
  for (lli i = 1; i < p.size(); i++) {
    vb[i] = vb[i - 1] * b;
  }
  for (lli i = 0; i < p.size(); i++) {
    hsh += ((lli)p[i]) * vb[p.size() - i - 1];
    cmp += ((lli)t[i]) * vb[p.size() - i - 1];
  }
  for (lli i = 0; i < t.size() - p.size(); i++) {
    if (hsh == cmp)
      cout << i << endl;
    cmp -= vb.back() * t[i];
    cmp *= b;
    cmp += t[i + p.size()];
  }
  if (cmp == hsh)
    cout << t.size() - p.size() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long lli;
typedef unsigned long long ull;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

string t;
string p;

const ull b = 1000000007;
ull hsh = 0;
ull cmp = 0;
vll vb;

int main() {
  cin >> t >> p;
  if (t.size() < p.size())
    return 0;
  vb = vll(p.size());
  vb[0] = 1;
  for (lli i = 1; i < p.size(); i++) {
    vb[i] = vb[i - 1] * b;
  }
  for (lli i = 0; i < p.size(); i++) {
    hsh += ((lli)p[i]) * vb[p.size() - i - 1];
    cmp += ((lli)t[i]) * vb[p.size() - i - 1];
  }
  for (lli i = 0; i < t.size() - p.size(); i++) {
    if (hsh == cmp)
      cout << i << endl;
    cmp -= vb.back() * t[i];
    cmp *= b;
    cmp += t[i + p.size()];
  }
  if (cmp == hsh)
    cout << t.size() - p.size() << endl;
  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef unsigned long long ll;
typedef pair<int, int> pint;

const ll x = 1000000007;
string t, p;
int main() {
  cin >> t >> p;
  ll st = t.size(), sp = p.size();
  ll q = 1;
  rep(i, sp) q = q * x;
  ll ht = 0, hp = 0;
  rep(i, sp) {
    ht = (ht * x + t[i]);
    hp = (hp * x + p[i]);
  }
  rep(i, st - sp + 1) {
    if (ht == hp)
      cout << i << endl;
    if (i < st - sp)
      ht = ht * x + t[i + sp] - q * t[i];
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef unsigned long long ll;
typedef pair<int, int> pint;

const ll x = 1000000007;
string t, p;
int main() {
  cin >> t >> p;
  ll st = t.size(), sp = p.size();
  if (st < sp)
    return 0;
  ll q = 1;
  rep(i, sp) q = q * x;
  ll ht = 0, hp = 0;
  rep(i, sp) {
    ht = (ht * x + t[i]);
    hp = (hp * x + p[i]);
  }
  rep(i, st - sp + 1) {
    if (ht == hp)
      cout << i << endl;
    if (i < st - sp)
      ht = ht * x + t[i + sp] - q * t[i];
  }
  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 950527;
int n, m;
char str[1000005];
ull a[1000005];

int main() {
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < n; i++) {
    a[i] = str[i];
    if (i)
      a[i] += a[i - 1] * B;
  }
  scanf("%s", str);
  m = strlen(str);
  ull key = 0, num = 0, C = 1;
  for (int i = 0; i < m; i++) {
    key = key * B + str[i];
    C *= B;
  }
  for (int i = 0; i < n; i++) {
    num = a[i];
    if (i - m >= 0)
      num -= C * a[i - m];
    if (key == num) {
      cout << i - m + 1 << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 950527;
int n, m;
char str[1000005];
ull a[1000005];

int main() {
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < n; i++) {
    a[i] = str[i];
    if (i)
      a[i] += a[i - 1] * B;
  }
  scanf("%s", str);
  m = strlen(str);
  ull key = 0, num = 0, C = 1;
  for (int i = 0; i < m; i++) {
    key = key * B + str[i];
    C *= B;
  }
  for (int i = 0; i < n; i++) {
    num = a[i];
    if (i - m >= 0)
      num -= C * a[i - m];
    if (key == num) {
      printf("%d\n", i - m + 1);
    }
  }

  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  string T[2];
  cin >> T[0] >> T[1];

  typedef unsigned int int_type;
  const int_type base = 9973;

  vector<int_type> hashed[2], power[2];

  for (int i = 0; i < 2; i++) {
    int sz = T[i].size();
    hashed[i].assign(sz + 1, 0);
    power[i].assign(sz + 1, 0);

    hashed[i][0] = 0;
    power[i][0] = 1;
    for (int j = 0; j < sz; j++) {
      power[i][j + 1] = power[i][j] * base;
      hashed[i][j + 1] = (hashed[i][j] + T[i][j]) * base;
    }
  }

  for (int i = 0; i <= (int)(T[0].size() - T[1].size()); i++) {
    if (hashed[0][i + T[1].size()] - hashed[0][i] * power[0][T[1].size()] ==
        hashed[1][T[1].size()] - hashed[1][0] * power[0][T[1].size()]) {
      cout << i << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  string T[2];
  cin >> T[0] >> T[1];

  typedef unsigned int int_type;
  const int_type base = 9973;

  vector<int_type> hashed[2], power[2];

  for (int i = 0; i < 2; i++) {
    int sz = T[i].size();
    hashed[i].assign(sz + 1, 0);
    power[i].assign(sz + 1, 0);

    hashed[i][0] = 0;
    power[i][0] = 1;
    for (int j = 0; j < sz; j++) {
      power[i][j + 1] = power[i][j] * base;
      hashed[i][j + 1] = (hashed[i][j] + T[i][j]) * base;
    }
  }

  for (int i = 0; i <= (int)(T[0].size() - T[1].size()); i++) {
    if (hashed[0][i + T[1].size()] - hashed[0][i] * power[0][T[1].size()] ==
        hashed[1][T[1].size()] - hashed[1][0] * power[0][T[1].size()]) {
      printf("%d\n", i);
    }
  }

  return 0;
}
Problem p02248 (C++)

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

using namespace std;

const long long MOD = 1e9 + 7;
const long long BASE = 0x234567;

struct RollingHash {
  string s;
  long long mod, base;
  vector<long long> hash, pow;
  RollingHash() {}
  RollingHash(string s, long long m, long long b) : s(s), mod(m), base(b) {
    pow.resize(s.length() + 1);
    pow[0] = 1;
    for (size_t i = 0; i < s.length(); ++i)
      (pow[i + 1] = pow[i] * base) %= mod;

    hash.assign(s.length() + 1, 0);
    for (size_t i = 0; i < s.length(); ++i)
      (hash[i + 1] = hash[i] + pow[i] * s[i]) %= mod;
  }
  long long get_hash(size_t first, size_t last) {
    return (hash[last] - hash[first] + mod) % mod;
  }
  long long make_hash(string t) {
    long long h = 0;
    for (size_t i = 0; i < t.length(); ++i)
      (h += pow[i] * t[i]) %= mod;

    return h;
  }
  long long diff(long long h, size_t pos, size_t len) {
    return (h * pow[pos] - get_hash(pos, pos + len)) % mod;
  }
  bool is_equiv(long long h, size_t pos, size_t len) {
    return !diff(h, pos, len);
  }
};

int main() {
  char buf[1 << 10];
  scanf("%s", buf);
  string T(buf);

  RollingHash rhash(T, MOD, BASE);

  scanf("%s", buf);
  string P(buf);

  long long h = rhash.make_hash(P);
  for (size_t i = 0; i + P.length() <= T.length(); ++i)
    if (rhash.is_equiv(h, i, P.length()))
      printf("%zd\n", i);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

const long long MOD = 1e9 + 7;
const long long BASE = 0x234567;

struct RollingHash {
  string s;
  long long mod, base;
  vector<long long> hash, pow;
  RollingHash() {}
  RollingHash(string s, long long m, long long b) : s(s), mod(m), base(b) {
    pow.resize(s.length() + 1);
    pow[0] = 1;
    for (size_t i = 0; i < s.length(); ++i)
      (pow[i + 1] = pow[i] * base) %= mod;

    hash.assign(s.length() + 1, 0);
    for (size_t i = 0; i < s.length(); ++i)
      (hash[i + 1] = hash[i] + pow[i] * s[i]) %= mod;
  }
  long long get_hash(size_t first, size_t last) {
    return (hash[last] - hash[first] + mod) % mod;
  }
  long long make_hash(string t) {
    long long h = 0;
    for (size_t i = 0; i < t.length(); ++i)
      (h += pow[i] * t[i]) %= mod;

    return h;
  }
  long long diff(long long h, size_t pos, size_t len) {
    return (h * pow[pos] - get_hash(pos, pos + len)) % mod;
  }
  bool is_equiv(long long h, size_t pos, size_t len) {
    return !diff(h, pos, len);
  }
};

int main() {
  char buf[1 << 20];
  scanf("%s", buf);
  string T(buf);

  RollingHash rhash(T, MOD, BASE);

  scanf("%s", buf);
  string P(buf);

  long long h = rhash.make_hash(P);
  for (size_t i = 0; i + P.length() <= T.length(); ++i)
    if (rhash.is_equiv(h, i, P.length()))
      printf("%zd\n", i);

  return 0;
}
Problem p02248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  string T, P;
  cin >> T >> P;
  for (int i = 0; i < T.size(); i++) {
    for (int j = 0; j < P.size(); j++) {
      if (T[i + j] != P[j])
        break;
      else if (j == P.size() - 1) {
        cout << i << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  string T, P;
  cin >> T >> P;
  int t = T.size(), p = P.size();
  for (int i = 0; i <= t - p; i++) {
    if (T[i] == P[0]) {
      if (T.substr(i, p) == P) {
        printf("%d\n", i);
      }
    }
  }
  return 0;
}
Problem p02255 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

static const int N = 1000;

void print(int a[], int n) {
  int k;
  for (k = 0; k < n; k++) {
    if (k > 0)
      printf(" ");
    printf("%d", a[k]);
  }
  printf("\n");
}

int main(void) {
  int n, i, j, key;
  int a[N + 1];
  scanf("%d", &n);
  // input
  for (i = 0; i < n; i++) {
    scanf("%d", a[i]);
  }
  print(a, n);

  // insertion
  for (i = 1; i < n; i++) {
    key = a[i];
    j = i - 1;
    while (j >= 0 && a[j] > key) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = key;
    print(a, n);
  }

  return 0;
}


Fixed submission:
#include <stdio.h>

static const int N = 1000;

void print(int a[], int n) {
  int k;
  for (k = 0; k < n; k++) {
    if (k > 0)
      printf(" ");
    printf("%d", a[k]);
  }
  printf("\n");
}

int main(void) {
  int n, i, j, key;
  int a[N + 1];
  scanf("%d", &n);
  // input
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  print(a, n);

  // insertion
  for (i = 1; i < n; i++) {
    key = a[i];
    j = i - 1;
    while (j >= 0 && a[j] > key) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = key;
    print(a, n);
  }

  return 0;
}
Problem p02255 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

void show(vector<int> a) {
  for (int i = 0; i < a.size() - 1; i++) {
    cout << a[i] << " ";
  }
  cout << a[a.size() - 1] << endl;
}

vector<int> isort(vector<int> a, int n) {
  int v = 0;
  int j;
  show(a);
  for (int i = 1; i < n; i++) {
    v = a[i];
    j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    show(a);
  }
}

int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  isort(a, n);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

void show(vector<int> a) {
  for (int i = 0; i < a.size() - 1; i++) {
    cout << a[i] << " ";
  }
  cout << a[a.size() - 1] << endl;
}

vector<int> isort(vector<int> a, int n) {
  int v = 0;
  int j;
  show(a);
  for (int i = 1; i < n; i++) {
    v = a[i];
    j = i - 1;
    while (j >= 0 && a[j] > v) {
      a[j + 1] = a[j];
      j--;
    }
    a[j + 1] = v;
    show(a);
  }
  return a;
}

int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  isort(a, n);

  return 0;
}
Problem p02255 (C++)

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

int main() {
  int n = 30;
  int k[n];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k[i];
  }

  for (int i = 0; i < n; i++) {
    int v = k[i];
    int j = i - 1;
    while (j >= 0 && k[j] > v) {
      k[j + 1] = k[j];
      j--;
    }
    k[j + 1] = v;
    for (int m = 0; m < n; m++) {
      cout << k[m] << (m + 1 < n ? " " : "\n");
      // printf("%d",k[m]);
      // printf(" ");
    }
  }
  return 0;
}

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

int main() {
  int n = 100;
  int k[n];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k[i];
  }

  for (int i = 0; i < n; i++) {
    int v = k[i];
    int j = i - 1;
    while (j >= 0 && k[j] > v) {
      k[j + 1] = k[j];
      j--;
    }
    k[j + 1] = v;
    for (int m = 0; m < n; m++) {
      cout << k[m] << (m + 1 < n ? " " : "\n");
      // printf("%d",k[m]);
      // printf(" ");
    }
  }
  return 0;
}
Problem p02255 (C++)

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

int main() {
  int numOfElement;
  int elem[99] = {};

  cin >> numOfElement;

  for (int i = 0; i < numOfElement; i++) {
    cin >> elem[i];
  }

  for (int i = 0; i < numOfElement; i++) {
    for (int j = i; j > 0 && elem[j] < elem[j - 1]; j--) {
      int buf = elem[j - 1];
      elem[j - 1] = elem[j];
      elem[j] = buf;
    }
    for (int j = 0; j < numOfElement; j++) {
      if (j + 1 >= numOfElement) {
        cout << elem[j];
        break;
      }
      cout << elem[j] << " ";
    }
    cout << endl;
  }

  return 0;
}

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

int main() {
  int numOfElement;
  int elem[100] = {};

  cin >> numOfElement;

  for (int i = 0; i < numOfElement; i++) {
    cin >> elem[i];
  }

  for (int i = 0; i < numOfElement; i++) {
    for (int j = i; j > 0 && elem[j] < elem[j - 1]; j--) {
      int buf = elem[j - 1];
      elem[j - 1] = elem[j];
      elem[j] = buf;
    }
    for (int j = 0; j < numOfElement; j++) {
      if (j + 1 >= numOfElement) {
        cout << elem[j];
        break;
      }
      cout << elem[j] << " ";
    }
    cout << endl;
  }

  return 0;
}
Problem p02255 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <iostream>
#include <vector>

using namespace std;

void insortionSort(vector<int> A) {
  for (int i = 0; i < A.size(); i++) {
    int v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = v;
    for (int i : A) {
      cout << i;
    }
    cout << endl;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  insortionSort(A);
}


Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

void insortionSort(vector<int> A) {
  for (int i = 0; i < A.size(); i++) {
    int v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = v;
    for (int i = 0; i < A.size(); i++) {
      cout << A[i];
      if (i != A.size() - 1) {
        cout << ' ';
      }
    }
    cout << endl;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  insortionSort(A);
}
Problem p02255 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef signed long long ll;

int main(void) {
  int N = 0;

  cin >> N;

  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i > 0) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;

  for (int i = 1; i < N; i++) {
    int value = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > value) {
      A[j + 1] = A[j];
      j--;
    }
    A[j] = value;
    for (int k = 0; k < N; k++) {
      if (k > 0) {
        cout << " ";
      }
      cout << A[k];
    }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef signed long long ll;

int main(void) {
  int N = 0;

  cin >> N;

  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i > 0) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;

  for (int i = 1; i < N; i++) {
    int value = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > value) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = value;
    for (int k = 0; k < N; k++) {
      if (k > 0) {
        cout << " ";
      }
      cout << A[k];
    }
    cout << endl;
  }

  return 0;
}
Problem p02256 (C++)

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

int gcd(int x, int y) {
  cerr << x << " " << y << endl;
  if (x < y)
    swap(x, y);
  if (x % y == 0)
    return y;
  return gcd(x % y, y);
}

int main(void) {
  int x, y;
  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}

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

int gcd(int x, int y) {
  // cerr << x << " " << y << endl;
  if (x < y)
    swap(x, y);
  if (x % y == 0)
    return y;
  return gcd(x % y, y);
}

int main(void) {
  int x, y;
  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
using namespace std;

// a > b
int gcd(int a, int b) {
  if (a < b) {
    cout << "Error : In using gcd function, must a > b.\n";
    exit(1);
  }
  if (a % b == 0) {
    return b;
  } else {
    gcd(b, a % b);
  }
}

int main() {
  int a, b;
  cin >> a >> b;
  cout << gcd(a, b) << endl;
  return 0;
}

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

// a > b
int gcd(int a, int b) {
  int r;
  while (1) {
    r = a % b;
    if (!r) {
      return b;
    } else {
      a = b;
      b = r;
    }
  }
}

int main() {
  int a, b;
  cin >> a >> b;
  cout << gcd(a, b) << endl;
  return 0;
}
Problem p02256 (C++)

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

int main() {
  int x, y, a;
  cin >> x >> y;
  while (x % y != 0) {
    cout << a << endl;
    x = y;
    y = a;
  }
  cout << y << "\n";
}

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

int main() {
  int x, y, a;
  cin >> x >> y;
  while (x % y != 0) {
    a = x % y;
    x = y;
    y = a;
  }
  cout << y << "\n";
}
Problem p02256 (C++)

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

int main() {
  int a, b;
  cin >> a >> b;
  if (max(a, b) % min(a, b) == 0) {
    cout << min(a, b) << endl;
    return 0;
  }
  for (int i = min(a, b) / 2; i >= 1; --i) {
    if (a % i == 0 && b % i == 0) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

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

int main() {
  int a, b;
  cin >> a >> b;
  if (max(a, b) % min(a, b) == 0) {
    cout << min(a, b) << endl;
    return 0;
  }
  for (int j = 2; j <= min(a, b); ++j) {
    int i = min(a, b) / j;
    if (a % i == 0 && b % i == 0) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02256 (C++)

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

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

int main() {
  int x, y;
  cin >> x;
  cin >> y;
  if (x >= y)
    cout << gcd(x, y) << endl;
  if (y > x)
    cout << gcd(y, x) << endl;

  return 0;
}

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

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

int main() {
  int x, y;
  cin >> x;
  cin >> y;
  if (x >= y)
    cout << gcd(x, y) << endl;
  if (y > x)
    cout << gcd(y, x) << endl;

  return 0;
}
Problem p02256 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  int a, b;
  cin >> a;
  cin >> b;
  for (;;) {
    if (a > b)
      a = a % b;
    else if (a < b)
      b = b % a;
    if (a * b == 0)
      break;
  }
  cout << a + b << endl;
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  int a, b;
  cin >> a;
  cin >> b;
  if (a - b == 0) {
    cout << a << endl;
    return 0;
  }
  for (;;) {
    if (a > b)
      a = a % b;
    else if (a < b)
      b = b % a;
    if (a * b == 0)
      break;
  }
  cout << a + b << endl;
  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
///
// File:  alds1_1_b.cpp
// Author: ymiyamoto
//
// Created on Mon Nov 13 03:00:08 2017
//

#include <cstdint>
#include <iostream>

using namespace std;

uint64_t gcd(uint64_t a, uint64_t b) {
  if (b > a)
    gcd(a, b);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int32_t main() {
  uint64_t a, b;
  cin >> a >> b;

  cout << gcd(a, b) << endl;

  return 0;
}

Fixed submission:
///
// File:  alds1_1_b.cpp
// Author: ymiyamoto
//
// Created on Mon Nov 13 03:00:08 2017
//

#include <cstdint>
#include <iostream>

using namespace std;

uint64_t gcd(uint64_t a, uint64_t b) {
  if (b > a)
    gcd(b, a);
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int32_t main() {
  uint64_t a, b;
  cin >> a >> b;

  cout << gcd(a, b) << endl;

  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  long long x, y;
  cin >> x >> y;
  if (x < y) {
    long long t = y;
    y = x;
    x = t;
  }
  long long z = x % y;
  long long a = 0;
  for (long long i = 1; i < y + 1; i++) {
    if (y % i == 0 && z % i == 0) {
      a = i;
    }
  }
  cout << a << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long x, y;
  cin >> x >> y;
  if (x < y) {
    long long t = y;
    y = x;
    x = t;
  }
  while (1) {
    long long z = x % y;
    if (z == 0) {
      break;
    } else {
      x = y;
      y = z;
    }
  }
  long long a;
  for (int i = 1; i < y + 1; i++) {
    if (x % i == 0 && y % i == 0) {
      a = i;
    }
  }
  cout << a << endl;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define INPUT_FROM_FILE (1)

#if INPUT_FROM_FILE
#include <fstream>
#endif

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

int main(int argc, char **argv) {
#if INPUT_FROM_FILE
  std::ifstream ifs("test.txt");
#endif
  int a = 0;
  int b = 0;

#if INPUT_FROM_FILE
  ifs >> a >> b;
#else
  std::cin >> a >> b;
#endif

  if (a < b) {
    std::swap(a, b);
  }
  std::cout << gcd(a, b) << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define INPUT_FROM_FILE (0)

#if INPUT_FROM_FILE
#include <fstream>
#endif

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

int main(int argc, char **argv) {
#if INPUT_FROM_FILE
  std::ifstream ifs("test.txt");
#endif
  int a = 0;
  int b = 0;

#if INPUT_FROM_FILE
  ifs >> a >> b;
#else
  std::cin >> a >> b;
#endif

  if (a < b) {
    std::swap(a, b);
  }
  std::cout << gcd(a, b) << std::endl;
  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

int main() {

  int a, b, c, r, x, y, flag;

  scanf("%d%d", &a, &b);

  if (a == b) {
    printf("%d\n", a);
    exit(1);
  } else if (a > b) {
    flag = 0;
    c = b;
  } else {
    c = a;
    flag = 1;
  }

  if (flag == 0)
    r = a % b;
  if (flag == 1)
    r = b % a;

  while (1) {
    x = c % r;
    if (x == 0)
      break;
    c = r;
    r = x;
    // printf("%d %d %d\n",x,c,r);
  }

  printf("%d\n", r);

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>

int main() {

  int a, b, c, r, x, y, flag;

  scanf("%d%d", &a, &b);

  if (a == b) {
    printf("%d\n", a);
    return 0;
  } else if (a > b) {
    flag = 0;
    c = b;
  } else {
    c = a;
    flag = 1;
  }

  if (flag == 0)
    r = a % b;
  if (flag == 1)
    r = b % a;

  while (1) {
    x = c % r;
    if (x == 0)
      break;
    c = r;
    r = x;
    // printf("%d %d %d\n",x,c,r);
  }

  printf("%d\n", r);

  return 0;
}
Problem p02256 (C++)

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

long long int gcd(long long int x, long long int y) {
  bool flag = true;
  while (flag) {
    if (x > y) {
      if (x % y != 0) {
        x = x % y;
      }
      if (x % y == 0) {
        return y;
        flag = false;
      }
    }
    if (y > x) {
      if (y % x != 0) {
        y = y % x;
      }
      if (y % x == 0) {
        return x;
        flag = false;
      }
    }
  }
}
int main() {
  long long int x, y;
  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}

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

int gcd(int x, int y) {
  if (x < y) {
    return gcd(y, x);
  } else if (x >= y && x % y != 0) {
    return gcd(x % y, y);
  } else {
    return y;
  }
}
int main() {
  long long int x, y;
  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}
Problem p02256 (C++)

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

int main() {
  long x, y;
  cin >> x >> y;
  while (x != 0 && y != 0) {
    if (x > y) {
      long tmp = 0;
      tmp = y;
      y = x % y;
      x = tmp;
    } else {
      long tmp = 0;
      tmp = x;
      x = y;
      y = tmp;
    }
  }
  if (x == 0)
    cout << y << endl;
  else
    cout << x << endl;
  return 0;
}

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

int main() {
  long x, y;
  cin >> x >> y;
  while (x != 0 && y != 0) {
    if (x >= y) {
      long tmp = 0;
      tmp = y;
      y = x % y;
      x = tmp;
    } else {
      long tmp = 0;
      tmp = x;
      x = y;
      y = tmp;
    }
  }
  if (x == 0)
    cout << y << endl;
  else
    cout << x << endl;
  return 0;
}
Problem p02256 (C++)

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

using namespace std;

int main() {
  int x, y;
  int ma, mi;

  cin >> x >> y;
  ma = max(x, y);
  mi = min(x, y);

  while (1) {
    int t = ma % mi;
    ma = max(t, mi);
    mi = min(t, mi);
    if (ma % mi == 0)
      break;
  }
  cout << mi << endl;

  return 0;
}

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

using namespace std;

int main() {
  int x, y;
  int ma, mi;

  cin >> x >> y;
  ma = max(x, y);
  mi = min(x, y);

  while (ma != mi) {
    int t = ma % mi;
    ma = max(t, mi);
    mi = min(t, mi);
    if (ma % mi == 0)
      break;
  }
  cout << mi << endl;

  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  long long x, y, c;
  cin >> x >> y;
  if (x < y) {
    c = y;
    y = x;
    x = c;
  }
  c = 0;
  while (!x == 0 || y == 0) {
    if (c % 2 == 0) {
      x = x % y;
    } else {
      y = y % x;
    }
    ++c;
  }
  cout << x + y << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long x, y, c;
  cin >> x >> y;
  if (x < y) {
    c = y;
    y = x;
    x = c;
  }
  c = 0;
  while (x != 0 && y != 0) {
    if (c % 2 == 0) {
      x = x % y;
    } else {
      y = y % x;
    }
    ++c;
  }
  cout << x + y << endl;
  return 0;
}
Problem p02256 (C++)

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

int main() {
  int a, b, c;
  bool check = true;
  cin >> a >> b;
  while (check) {
    if (a > b) {
      c = a % b;
      if (b % c == 0)
        check = false;
      a = c;
    } else {
      c = b % a;
      if (a % c == 0)
        check = false;
      b = c;
    }
  }
  cout << c << endl;
  return 0;
}

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

int main() {
  int a, b, c;
  bool check = true;
  cin >> a >> b;
  if (a == b) {
    cout << a << endl;
    return 0;
  }
  while (check) {
    if (a > b) {
      c = a % b;
      if (b % c == 0)
        check = false;
      a = c;
    } else {
      c = b % a;
      if (a % c == 0)
        check = false;
      b = c;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02256 (C++)

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

int main() {
  int x, y;
  cin >> x >> y;
  int a, b;
  if (x >= y) {
    a = x;
    b = y;
  } else {
    a = y;
    b = x;
  }

  for (int i = b; i >= 1; i--) {
    if (a % i == 0 && b % i == 0) {
      cout << i << endl;
      return 0;
    }
  }
}


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

int main() {
  int x, y;
  cin >> x >> y;
  int a, b;
  if (x >= y) {
    a = x;
    b = y;
  } else {
    a = y;
    b = x;
  }
  int c;
  while (true) {
    c = a % b;
    if (c == 0) {
      cout << b << endl;
      break;
    } else {
      a = b;
      b = c;
    }
  }
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

const double pi = acos(-1);
#define nyan cout << "(=^•ω•^=)" << endl;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)

int gcd(int i, int j) {
  if (i > j)
    swap(i, j);
  if (i == 1)
    return 1;
  if (i == j)
    return i;
  else
    return gcd(i, j % i);
}

signed main() {
  int x, y;
  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

const double pi = acos(-1);
#define nyan cout << "(=^•ω•^=)" << endl;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)

int gcd(int i, int j) {
  while (true) {
    if (i < j)
      swap(i, j);
    if (!j)
      break;
    i %= j;
  }
  return i;
}

signed main() {
  int x, y;
  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

const int N_MAX = 1000000;

int main() {
  int x, y;
  return -1;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

const int N_MAX = 1000000;

int main() {
  int x, y;
  cin >> x;
  cin >> y;

  int residue = -1;
  int ans;
  while (residue != 0) {
    residue = max(x, y) % min(x, y);
    ans = min(x, y);
    if (x > y)
      x = residue;
    else
      y = residue;
  }

  cout << ans << endl;

  return 0;
}
Problem p02256 (C++)

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

void sorts(int *xx, int *yy);

int main() {
  int x, y;
  cin >> x >> y;
  sorts(&x, &y);
  x = x % y;
  int ans = y;
  for (int i = x; i >= 0; i--) {
    if (y % i == 0 && x % i == 0) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
}

void sorts(int *xx, int *yy) {
  if (*xx < *yy) {
    int temp = *xx;
    *xx = *yy;
    *yy = temp;
  }
  return;
}

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

void sorts(int *xx, int *yy);

int main() {
  int x, y;
  cin >> x >> y;
  sorts(&x, &y);
  x = x % y;
  if (!x) {
    cout << y << endl;
    return 0;
  }
  int ans = y;
  for (int i = x; i >= 0; i--) {
    if (y % i == 0 && x % i == 0) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
}

void sorts(int *xx, int *yy) {
  if (*xx < *yy) {
    int temp = *xx;
    *xx = *yy;
    *yy = temp;
  }
  return;
}
Problem p02256 (C++)

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

int gcd(int a, int b);

void swap(int *a, int *b);

int main() {
  int a, b;
  cin >> a >> b;

  if (a < b)
    swap(a, b);

  cout << gcd(a, b) << endl;
}

void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

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

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

int gcd(int a, int b);

void swap(int *a, int *b);

int main() {
  int a, b;
  cin >> a >> b;

  if (a < b)
    swap(a, b);

  cout << gcd(a, b) << endl;
}

void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  while (1) {
    a = a % b;
    if (b % a == 0)
      return a;
    b = b % a;
    if (a % b == 0)
      return b;
  }
}
Problem p02256 (C++)

Buggy submission (status: Time Limit Exceeded):
//
// Write a program which finds the greatest common divisor of two natural
// numbers a and b
//
#include <iostream>
using namespace std;

void read(int &x, int &y);
int calcGcd(const int x, const int y);
void write(const int gcd);

int main() {
  int x = 0, y = 0;
  int ret = 0;

  read(x, y);
  ret = x > y ? calcGcd(x, y) : calcGcd(y, x);
  write(ret);
}

void read(int &x, int &y) { cin >> x >> y; }

int calcGcd(const int x, const int y) {
  int r = 0;

  r = x - y;
  if (y == r)
    return r;

  y > r ? calcGcd(y, r) : calcGcd(r, y);
}

void write(const int gcd) { cout << gcd << endl; }

Fixed submission:
//
// Write a program which finds the greatest common divisor of two natural
// numbers a and b
//
#include <iostream>
using namespace std;

void read(int &x, int &y);
int calcGcd(const int x, const int y);
void write(const int gcd);

int main() {
  int x = 0, y = 0;
  int ret = 0;

  read(x, y);
  ret = x > y ? calcGcd(x, y) : calcGcd(y, x);
  write(ret);
}

void read(int &x, int &y) { cin >> x >> y; }

int calcGcd(const int x, const int y) {
  int r = 0;

  if (x == y)
    return x;

  r = x - y;
  if (y == r)
    return r;

  y > r ? calcGcd(y, r) : calcGcd(r, y);
}

void write(const int gcd) { cout << gcd << endl; }
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>

int gcd(int, int);

int main() {
  int x, y, z;
  scanf("%d %d\n", x, y);
  z = gcd(x, y);

  printf("%d\n", z);

  return 0;
}

int gcd(int x, int y) {
  int r;

  while (y > 0) {
    r = x % y;
    x = y;
    y = r;
  }
  return x;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>

int gcd(int, int);

int main() {
  int x, y, z;
  scanf("%d %d", &x, &y);
  z = gcd(x, y);

  printf("%d\n", z);

  return 0;
}

int gcd(int x, int y) {
  int r;

  while (y > 0) {
    r = x % y;
    x = y;
    y = r;
  }
  return x;
}
Problem p02256 (C++)

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

long int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  if (b == 1)
    return a;
  return gcd(b, a % b);
}

int main() {

  long int x, y;

  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}

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

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

int main() {

  long int x, y;

  cin >> x >> y;
  cout << gcd(x, y) << endl;
  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main(void) {
  int x;
  int y;
  int r;
  int tmp;
  scanf("%d %d", &x, &y);

  if (y >= x) {
    x = tmp;
    x = y;
    y = tmp;
  }

  while (1) {
    r = x % y;
    if (r == 0) {
      printf("%d\n", y);
      break;
    }
    x = y;
    y = r;
  }

  return 0;
}


Fixed submission:
#include <stdio.h>

int main(void) {
  int x;
  int y;
  int r;
  int tmp;
  scanf("%d %d", &x, &y);

  if (y >= x) {
    tmp = x;
    x = y;
    y = tmp;
  }

  while (1) {
    r = x % y;
    if (r == 0) {
      printf("%d\n", y);
      break;
    }
    x = y;
    y = r;
  }

  return 0;
}
Problem p02256 (C++)

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

int main() {
  int a, b, c;
  scanf("%d %d", &a, &b);
  c = a > b ? b : a;
  while (c > 0 && (a % c || b % c))
    c--;
  printf("%d\n", c);
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int a, b, c;
  scanf("%d %d", &a, &b);
  if (a < b) {
    c = a;
    a = b;
    b = c;
  }
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  printf("%d\n", a);
  return 0;
}