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

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

int main() {
  vector<int> vi;
  int N, X, dummy, ans;
  dummy = scanf("%d %d", &N, &X);
  vi.push_back(X);

  int tmp;
  for (int i = 0; i < N; i++) {
    dummy = scanf("%d", &tmp);
    vi.push_back(tmp);
  }

  sort(vi.begin(), vi.end());

  int size = vi.size();
  for (int i = 0; i < size - 1; i++) {
    vi[i] = vi[i + 1] - vi[i];
  }
  vi.pop_back();

  vi.erase(unique(vi.begin(), vi.end()), vi.end());

  for (int i = vi[0]; i > 0; i--) {
    bool NG = false;
    for (auto itr = vi.begin(); itr != vi.end(); itr++) {
      if (*itr % i != 0) {
        NG = true;
        break;
      }
    }
    if (!NG) {
      printf("%d\n", i);
      break;
    }
  }

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

int main() {
  vector<int> vi;
  int N, X, dummy, ans;
  dummy = scanf("%d %d", &N, &X);
  vi.push_back(X);

  int tmp;
  for (int i = 0; i < N; i++) {
    dummy = scanf("%d", &tmp);
    vi.push_back(tmp);
  }

  sort(vi.begin(), vi.end());

  int size = vi.size();
  for (int i = 0; i < size - 1; i++) {
    vi[i] = vi[i + 1] - vi[i];
  }
  vi.pop_back();

  sort(vi.begin(), vi.end());
  vi.erase(unique(vi.begin(), vi.end()), vi.end());

  for (int i = vi[0]; i > 0; i--) {
    bool NG = false;
    for (auto itr = vi.begin(); itr != vi.end(); itr++) {
      if (*itr % i != 0) {
        NG = true;
        break;
      }
    }
    if (!NG) {
      printf("%d\n", i);
      break;
    }
  }

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int r;

  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }

  return a;
}
int main() {
  long long n, x, i, ans, dif[10009];
  vector<long long> a;
  cin >> n >> x;
  a.push_back(x);
  for (i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
  sort(a.begin(), a.end());
  for (i = 0; i < n; i++) {
    dif[i] = a[i + 1] - a[i];
  }
  ans = gcd(dif[0], dif[1]);
  for (i = 2; i < n; i++) {
    ans = gcd(ans, dif[i]);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int r;

  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }

  return a;
}
int main() {
  long long n, x, i, ans, dif[100090];
  vector<long long> a;
  cin >> n >> x;
  a.push_back(x);
  for (i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
  sort(a.begin(), a.end());
  for (i = 0; i < n; i++) {
    dif[i] = a[i + 1] - a[i];
  }
  ans = gcd(dif[0], dif[1]);
  for (i = 2; i < n; i++) {
    ans = gcd(ans, dif[i]);
  }
  cout << ans << endl;

  return 0;
}
Problem p03262 (C++)

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

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

int main() {
  int n, xxxx;
  cin >> n >> xxxx;
  vector<int> x(n + 1);
  x.at(0) = xxxx;
  for (int i = 1; i < n + 1; i++)
    cin >> x.at(i);
  sort(x.begin(), x.end());
  int ans = x.at(1) - x.at(0);
  if (n != 1) {
    for (int i = 0; i < n; i++) {
      ans = gcd(ans, x.at(i + 2) - x.at(i + 1));
    }
  }

  cout << ans;
  cout << endl;
}


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

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

int main() {
  int n, xxxx;
  cin >> n >> xxxx;
  vector<int> x(n + 1);
  x.at(0) = xxxx;
  for (int i = 1; i < n + 1; i++)
    cin >> x.at(i);
  sort(x.begin(), x.end());
  int ans = x.at(1) - x.at(0);
  for (int i = 0; i < n - 1; i++) {
    ans = gcd(ans, x.at(i + 2) - x.at(i + 1));
  }

  cout << ans;
  cout << endl;
}
Problem p03262 (C++)

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

using namespace std;

int main() {
  long long int z, n, x, a[10000], b[10000];
  scanf("%I64d%I64d", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    b[i] = abs(a[i] - x);
  }
  sort(b, b + n);
  z = b[0];
  for (int i = 1; i < n; i++) {
    z = __gcd(z, b[i]);
  }
  printf("%I64d", z);

  return 0;
}

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

using namespace std;

int main() {
  long long int z, n, x, a[100009], b[100009];
  scanf("%I64d%I64d", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    b[i] = abs(a[i] - x);
  }
  sort(b, b + n);
  z = b[0];
  for (int i = 1; i < n; i++) {
    z = __gcd(z, b[i]);
  }
  printf("%I64d", z);

  return 0;
}
Problem p03262 (C++)

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

using namespace std;

int gcd(int a, int b) {
  if (a <= 0 || b <= 0)
    return -1;
  if (a < b)
    gcd(b, a);

  int r = a % b;
  while (r) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int N, X;
  cin >> N >> X;
  int D;
  vector<int> diff;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    diff.push_back(abs(temp - X));
  }
  D = diff[0];
  for (auto it = diff.begin(); it != diff.end(); it++) {
    D = gcd(D, *it);
  }

  cout << D << endl;
  return 0;
}


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

using namespace std;

int gcd(int a, int b) {
  if (a <= 0 || b <= 0)
    return -1;
  if (a < b)
    gcd(b, a);

  int r = a % b;
  while (r) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int main() {
  int N, X;
  cin >> N >> X;
  int D;
  vector<int> diff;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    diff.push_back(abs(temp - X));
  }
  D = diff[0];
  for (auto it = diff.begin(); it != diff.end(); it++) {
    D = gcd(D, *it);
  }

  cout << D << endl;
  return 0;
}
Problem p03262 (C++)

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

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n >> x;
  int g = 0;
  while (n--) {
    int y;
    cin >> y;
    g = gcd(abs(x - y), g);
  }
  cout << g << '\n';
}

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

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n >> x;
  int g = 0;
  while (n--) {
    int y;
    cin >> y;
    g = gcd(abs(x - y), g);
  }
  cout << g << '\n';
}
Problem p03262 (C++)

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

typedef long long ll;
#define mod 1000000007
#define ALL(a) a.begin(), a.end()

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n, x;
  cin >> n >> x;
  ll c[x];
  for (int i = 0; i < n; i++)
    cin >> c[i];

  ll dis[n];
  for (int i = 0; i < n; i++) {
    dis[i] = abs(c[i] - x);
  }

  ll ans = dis[0];
  for (int i = 1; i < n; i++) {
    ans = gcd(ans, dis[i]);
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
#define mod 1000000007
#define ALL(a) a.begin(), a.end()

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n, x;
  cin >> n >> x;
  ll c[n];
  for (int i = 0; i < n; i++)
    cin >> c[i];

  ll dis[n];
  for (int i = 0; i < n; i++) {
    dis[i] = abs(c[i] - x);
  }

  ll ans = dis[0];
  for (int i = 1; i < n; i++) {
    ans = gcd(ans, dis[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03262 (C++)

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

using namespace std;

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

int main() {
  int N;
  long long int X;

  cin >> N >> X;
  vector<long long int> c(N);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  vector<long long int> d(N);
  for (int i = 0; i < N; i++)
    d[i] = abs(c[i] - X);

  long long int ans = 0LL;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      ans = gcd(d[0], d[1]);
      continue;
    }
    ans = gcd(d[i], ans);
  }
  cout << ans;
}


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

using namespace std;

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

int main() {
  int N;
  long long int X;

  cin >> N >> X;
  vector<long long int> c(N);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  vector<long long int> d(N);
  for (int i = 0; i < N; i++)
    d[i] = abs(c[i] - X);

  long long int ans = 0LL;
  for (int i = 0; i < N; i++) {
    if (N == 1) {
      ans = d[0];
      break;
    }
    if (i == 0) {
      ans = gcd(d[0], d[1]);
      continue;
    }
    ans = gcd(d[i], ans);
  }
  cout << ans;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int euclid_gcd(int a, int b) {
  int tmp_a, tmp_b; // tmp_a>tmp_b

  if (a < b) {
    tmp_a = b;
    tmp_b = a;
  } else {
    tmp_a = a;
    tmp_b = b;
  }

  if (tmp_b == 0) {
    return tmp_a;
  } else {
    return euclid_gcd(tmp_b, tmp_a % tmp_b);
  }
}

int gcd_multi(vector<int> x) {

  vector<int> gcds(x.size() / 2 + (x.size() % 2));

  if (x.size() == 2) {
    return euclid_gcd(x[0], x[1]);
  }

  for (int i = 0; i < x.size() / 2; ++i) {

    gcds[i] = euclid_gcd(x[i * 2], x[i * 2 + 1]);
  }

  if (x.size() % 2 == 1) {

    gcds.back() = x.back();
  }

  return gcd_multi(gcds);
}

int main(void) {

  int n, start;

  cin >> n >> start;

  vector<int> x(n);

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

    std::cin >> x[i];
  }

  x.push_back(start);

  sort(x.begin(), x.end());

  vector<int> w(n);

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

    w[i] = abs(x[i] - x[i + 1]);
  }

  cout << gcd_multi(w);

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int euclid_gcd(int a, int b) {
  int tmp_a, tmp_b; // tmp_a>tmp_b

  if (a < b) {
    tmp_a = b;
    tmp_b = a;
  } else {
    tmp_a = a;
    tmp_b = b;
  }

  if (tmp_b == 0) {
    return tmp_a;
  } else {
    return euclid_gcd(tmp_b, tmp_a % tmp_b);
  }
}

int gcd_multi(vector<int> x) {

  vector<int> gcds(x.size() / 2 + (x.size() % 2));

  if (x.size() == 1) {
    return x.front();
  }

  if (x.size() == 2) {
    return euclid_gcd(x[0], x[1]);
  }

  for (int i = 0; i < x.size() / 2; ++i) {

    gcds[i] = euclid_gcd(x[i * 2], x[i * 2 + 1]);
  }

  if (x.size() % 2 == 1) {

    gcds.back() = x.back();
  }

  return gcd_multi(gcds);
}

int main(void) {

  int n, start;

  cin >> n >> start;

  vector<int> x(n);

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

    std::cin >> x[i];
  }

  x.push_back(start);

  sort(x.begin(), x.end());

  vector<int> w(n);

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

    w[i] = abs(x[i] - x[i + 1]);
  }

  cout << gcd_multi(w);

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int gcd(int a, int b) {
  int c = a / b;
  int d = a % b;
  if (d > 0)
    return gcd(b, d);
  else
    return b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, x;
  cin >> n >> x;

  vector<int> A(n + 1);
  REP(i, n) cin >> A[i];
  A[n] = x;

  sort(A.begin(), A.end());

  vector<int> B(n);
  REP(i, n) { B[i] = A[i + 1] - A[i]; }

  int g = gcd(B[0], B[1]);
  for (int i = 1; i < n; i++) {
    g = gcd(g, B[i]);
  }

  cout << g << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)

int gcd(int a, int b) {
  int c = a / b;
  int d = a % b;
  if (d > 0)
    return gcd(b, d);
  else
    return b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, x;
  cin >> n >> x;

  vector<int> A(n + 1);
  REP(i, n) cin >> A[i];
  A[n] = x;

  sort(A.begin(), A.end());

  vector<int> B(n);
  REP(i, n) { B[i] = A[i + 1] - A[i]; }

  int g;
  if (n == 1)
    g = B[0];
  else
    g = gcd(B[0], B[1]);
  for (int i = 1; i < n; i++) {
    g = gcd(g, B[i]);
  }

  cout << g << endl;

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(X, N) for (int X = 0; X < N; X++)
#define repvin(V, N)                                                           \
  for (int i = 0; i < N; i++) {                                                \
    int tmp;                                                                   \
    cin >> tmp;                                                                \
    V.push_back(tmp);                                                          \
  }

using namespace std;

int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);

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

int main() {

  int n, x;
  cin >> n >> x;

  vector<int> v;

  repvin(v, n);

  v.push_back(x);

  sort(v.begin(), v.end());

  vector<int> diff;

  rep(i, n) { diff.push_back(v[i + 1] - v[i]); }

  int ans = gcd(diff[0], diff[1]);

  rep(i, n - 2) { ans = gcd(ans, diff[i + 2]); }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(X, N) for (int X = 0; X < N; X++)
#define repvin(V, N)                                                           \
  for (int i = 0; i < N; i++) {                                                \
    int tmp;                                                                   \
    cin >> tmp;                                                                \
    V.push_back(tmp);                                                          \
  }

using namespace std;

int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);

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

int main() {

  int n, x;
  cin >> n >> x;

  vector<int> v;

  repvin(v, n);

  v.push_back(x);

  sort(v.begin(), v.end());

  vector<int> diff;

  rep(i, n) { diff.push_back(v[i + 1] - v[i]); }

  int ans;

  if (diff.size() > 1) {
    ans = gcd(diff[0], diff[1]);
  } else {
    ans = diff[0];
  }

  rep(i, n - 2) { ans = gcd(ans, diff[i + 2]); }

  cout << ans << endl;

  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());

typedef long long ll;

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

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> v(n);
  rep(i, x) cin >> v[i];
  vector<int> d(n);
  rep(i, n) d[i] = abs(x - v[i]);
  int g;
  rep(i, n) {
    if (i == 0)
      g = d[0];
    else
      g = gcd(g, d[i]);
  }
  cout << g << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());

typedef long long ll;

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

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  vector<int> d(n);
  rep(i, n) d[i] = abs(x - v[i]);
  int g;
  rep(i, n) {
    if (i == 0)
      g = d[0];
    else
      g = gcd(g, d[i]);
  }
  cout << g << endl;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[10005];
int main() {
  int n, s, l;
  scanf("%d", &n);
  for (register int i = 1; i <= n + 1; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 2);
  for (register int i = 1; i <= n; i++)
    a[i] = a[i + 1] - a[i];
  a[n + 1] = 0;
  sort(a + 1, a + n + 1);
  s = a[1];
  l = 0;
  while (s > 0) {
    l = 0;
    for (register int i = 1; i <= n; i++)
      if (a[i] % s != 0) {
        l = 1;
        s--;
        break;
      }
    if (l == 0) {
      printf("%d\n", s);
      exit(0);
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  int n, s, l;
  scanf("%d", &n);
  for (register int i = 1; i <= n + 1; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 2);
  for (register int i = 1; i <= n; i++)
    a[i] = a[i + 1] - a[i];
  a[n + 1] = 0;
  sort(a + 1, a + n + 1);
  s = a[1];
  l = 0;
  while (s > 0) {
    l = 0;
    for (register int i = 1; i <= n; i++)
      if (a[i] % s != 0) {
        l = 1;
        s--;
        break;
      }
    if (l == 0) {
      printf("%d\n", s);
      exit(0);
    }
  }
  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long int
using namespace std;
int n, x, f[100005], top, pd, d[100005];
ll ans = 0x3f3f3f3f;
ll gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return (long long)gcd(b, a % b);
}
int main() {
  ans = ans * 100000;
  scanf("%d%d", &n, &f[1]);
  for (int i = 2; i <= n + 1; i++)
    scanf("%d", &f[i]);
  sort(f + 1, f + n + 2);
  for (int i = 2; i <= n + 1; i++) {
    d[i - 1] = f[i] - f[i - 1];
    if (n == 1)
      ans = d[i - 1];
  }
  if (n != 1)
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++)
        ans = min(ans, gcd(d[i], d[j]));
    }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long int
using namespace std;
int n, x, f[100005], top, pd, d[100005];
ll ans = 0x3f3f3f3f;
ll gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return (long long)gcd(b, a % b);
}
int main() {
  ans = ans * 100000;
  scanf("%d%d", &n, &f[1]);
  for (int i = 2; i <= n + 1; i++)
    scanf("%d", &f[i]);
  sort(f + 1, f + n + 2);
  for (int i = 2; i <= n + 1; i++) {
    d[i - 1] = f[i] - f[i - 1];
    if (n == 1)
      ans = d[i - 1];
  }
  if (n != 1)
    for (int i = 2; i <= n; i++) {
      ans = min(ans, gcd(d[i], d[i - 1]));
    }
  printf("%lld", ans);
  return 0;
}
Problem p03262 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int a[10005];
int n, x, ans = 1e9;
int main() {
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = abs(a[i] - x);
    ans = min(a[i], ans);
  }
  for (int i = 1; i <= n; i++)
    ans = __gcd(ans, a[i]);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int a[100005];
int n, x, ans = 1e9;
int main() {
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = abs(a[i] - x);
    ans = min(a[i], ans);
  }
  for (int i = 1; i <= n; i++)
    ans = __gcd(ans, a[i]);
  cout << ans;
  return 0;
}
Problem p03263 (Python)

Buggy submission (status: Runtime Error):
h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
r, c, d = 0, 0, 3
ret = []
for y in range(h * w - 1):
    if y % w == 0 or y % w == w - 1:
        d = (d + 1) % 4
    dy, dx = [(0, 1), (1, 0), (0, -1), (1, 0)][d]
    if a[r][c] % 2 != 0:
        ret.append((r, c, r + dy, c + dx))
        a[r + dy][c + dx] += 1
    r, c = r + dy, c + dx
print(len(ret))
for r1, c1, r2, c2 in ret:
    print(r1 + 1, c1 + 1, r2 + 1, c2 + 1)


Fixed submission:
h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
r, c, d = 0, 0, 3
ret = []
for y in range(h * w - 1):
    if y % w == 0:
        d = (d + 1) % 4
    if y % w == w - 1:
        d = (d + 1) % 4
    dy, dx = [(0, 1), (1, 0), (0, -1), (1, 0)][d]
    if a[r][c] % 2 != 0:
        ret.append((r, c, r + dy, c + dx))
        a[r + dy][c + dx] += 1
    r, c = r + dy, c + dx
print(len(ret))
for r1, c1, r2, c2 in ret:
    print(r1 + 1, c1 + 1, r2 + 1, c2 + 1)
Problem p03263 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-


def main():
    h, w = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(h)]
    operations = list()

    # See:
    # https://atcoder.jp/contests/abc109/submissions/3154532
    for row in range(h):
        for col in range(w - 1):
            if a[row][col] % 2 == 1:
                operations.append((row + 1, col + 1, row + 1, col + 2))
                a[row][col] -= 1
                a[row][col + 1] += 1

    for row in range(h - 1):
        if a[row][w - 1] % 2 == 1:
            operations.append((row + 1, col + 1, row + 2, col + 1))
            a[row][w - 1] -= 1
            a[row + 1][w - 1] += 1

    n = len(operations)
    print(n)

    for i in range(n):
        y, x, y_dash, x_dash = operations[i]
        print(y, x, y_dash, x_dash)


if __name__ == "__main__":
    main()


Fixed submission:
# -*- coding: utf-8 -*-


def main():
    h, w = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(h)]
    operations = list()

    # See:
    # https://atcoder.jp/contests/abc109/submissions/3154532
    for row in range(h):
        for col in range(w - 1):
            if a[row][col] % 2 == 1:
                operations.append((row + 1, col + 1, row + 1, col + 2))
                a[row][col] -= 1
                a[row][col + 1] += 1

    for row in range(h - 1):
        if a[row][w - 1] % 2 == 1:
            operations.append((row + 1, w, row + 2, w))
            a[row][w - 1] -= 1
            a[row + 1][w - 1] += 1

    n = len(operations)
    print(n)

    for i in range(n):
        y, x, y_dash, x_dash = operations[i]
        print(y, x, y_dash, x_dash)


if __name__ == "__main__":
    main()
Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct node {
  int a, b, c, d;
} re[10000];
int a[550][550];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &a[i][j]);
  int cnt = 0;
  for (int i = 1; i < h; i++)
    for (int j = 1; j <= w; j++)
      if (a[i][j] % 2 == 1)
        a[i][j]--, a[i + 1][j]++, re[cnt].a = i, re[cnt].b = j,
                                  re[cnt].c = i + 1, re[cnt].d = j, cnt++;
  for (int j = 1; j < w; j++)
    if (a[h][j] % 2 == 1)
      a[h][j]--, a[h][j + 1]++, re[cnt].a = h, re[cnt].b = j, re[cnt].c = h,
                                re[cnt].d = j + 1, cnt++;
  printf("%d\n", cnt);
  for (int i = 0; i < cnt; i++)
    printf("%d %d %d %d\n", re[i].a, re[i].b, re[i].c, re[i].d);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct node {
  int a, b, c, d;
} re[250050];
int a[550][550];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &a[i][j]);
  int cnt = 0;
  for (int i = 1; i < h; i++)
    for (int j = 1; j <= w; j++)
      if (a[i][j] % 2 == 1)
        a[i][j]--, a[i + 1][j]++, re[cnt].a = i, re[cnt].b = j,
                                  re[cnt].c = i + 1, re[cnt].d = j, cnt++;
  for (int j = 1; j < w; j++)
    if (a[h][j] % 2 == 1)
      a[h][j]--, a[h][j + 1]++, re[cnt].a = h, re[cnt].b = j, re[cnt].c = h,
                                re[cnt].d = j + 1, cnt++;
  printf("%d\n", cnt);
  for (int i = 0; i < cnt; i++)
    printf("%d %d %d %d\n", re[i].a, re[i].b, re[i].c, re[i].d);
}
Problem p03263 (Python)

Buggy submission (status: Runtime Error):
H, W = map(int, input().split())
G = [list(map(int, input().split())) for i in range(H)]

N = 0
ans = []
for h in range(H):
    for w in range(W - 1):
        if G[h][w] % 2 == 1:
            N += 1
            ans.append((h, w, h, w + 1))
            G[h][w + 1] += 1
            G[h][w] -= 1

for h in range(H - 1):
    if G[h][W - 1] % 2 == 1:
        N += 1
        ans.append((h, w, h + 1, w))
        G[h + 1][W - 1] += 1
        G[h][W - 1] -= 1

print(N)
for y1, x1, y2, x2 in ans:
    print(y1 + 1, x1 + 1, y2 + 1, x2 + 1)


Fixed submission:
H, W = map(int, input().split())
G = [list(map(int, input().split())) for i in range(H)]

N = 0
ans = []
for h in range(H):
    for w in range(W - 1):
        if G[h][w] % 2 == 1:
            N += 1
            ans.append((h, w, h, w + 1))
            G[h][w + 1] += 1
            G[h][w] -= 1

for h in range(H - 1):
    if G[h][W - 1] % 2 == 1:
        N += 1
        ans.append((h, W - 1, h + 1, W - 1))
        G[h + 1][W - 1] += 1
        G[h][W - 1] -= 1

print(N)
for y1, x1, y2, x2 in ans:
    print(y1 + 1, x1 + 1, y2 + 1, x2 + 1)
Problem p03263 (C++)

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

using namespace std;
const int N = 500 + 10;
int a[N][N], h, w, cnt;
int ans[N][N];

int main() {
  cin >> h >> w;
  cnt = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= h - 1; i++) {
    for (int j = 1; j <= w; j++) {
      if (a[i][j] % 2 != 0) {
        a[i][j]--;
        a[i + 1][j]++;
        cnt++;
        ans[cnt][0] = i;
        ans[cnt][1] = j;
        ans[cnt][2] = i + 1;
        ans[cnt][3] = j;
      }
    }
  }
  for (int j = 1; j <= w - 1; j++) {
    if (a[h][j] % 2 != 0) {
      a[h][j]--;
      a[h][j + 1]++;
      cnt++;
      ans[cnt][0] = h;
      ans[cnt][1] = j;
      ans[cnt][2] = h;
      ans[cnt][3] = j + 1;
    }
  }
  cout << cnt << endl;
  for (int i = 1; i <= cnt; i++) {
    cout << ans[i][0] << ' ' << ans[i][1] << ' ' << ans[i][2] << ' '
         << ans[i][3] << endl;
  }
  return 0;
}


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

using namespace std;
const int N = 500 + 10;
int a[N][N], h, w, cnt;
int ans[N * N][4];

int main() {
  cin >> h >> w;
  cnt = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= h - 1; i++) {
    for (int j = 1; j <= w; j++) {
      if (a[i][j] % 2 != 0) {
        a[i][j]--;
        a[i + 1][j]++;
        cnt++;
        ans[cnt][0] = i;
        ans[cnt][1] = j;
        ans[cnt][2] = i + 1;
        ans[cnt][3] = j;
      }
    }
  }
  for (int j = 1; j <= w - 1; j++) {
    if (a[h][j] % 2 != 0) {
      a[h][j]--;
      a[h][j + 1]++;
      cnt++;
      ans[cnt][0] = h;
      ans[cnt][1] = j;
      ans[cnt][2] = h;
      ans[cnt][3] = j + 1;
    }
  }
  cout << cnt << endl;
  for (int i = 1; i <= cnt; i++) {
    cout << ans[i][0] << ' ' << ans[i][1] << ' ' << ans[i][2] << ' '
         << ans[i][3] << endl;
  }
  return 0;
}
Problem p03263 (C++)

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

using map_t = std::vector<std::vector<size_t>>;
using ans_t = std::vector<std::tuple<size_t, size_t, size_t, size_t>>;

void move(size_t y1, size_t x1, size_t y2, size_t x2, map_t &A, ans_t &ans) {
  A[y1 - 1][x1 - 1]--;
  A[y2 - 1][x2 - 1]++;
  ans.push_back(std::make_tuple(y1, x1, y2, x2));
}

int main() {
  size_t H, W;
  std::cin >> H >> W;

  map_t A(H, std::vector<size_t>(W));

  for (size_t y = 0; y < H; y++) {
    for (size_t x = 0; x < W; x++) {
      std::cin >> A[y][x];
    }
  }

  ans_t ans;

  for (size_t y = 1; y < H + 1; y++) {
    if (y % 2 == 1) {
      for (size_t x = 1; x < W + 1; x++) {
        if (A[y - 1][x - 1] % 2 == 1) {
          if (x < W)
            move(y, x, y, x + 1, A, ans);
          else if (y < H)
            move(y, x, y + 1, x, A, ans);
        }
      }
    } else {
      for (size_t x = W; x > 0; x--) {
        if (A[y - 1][x - 1] % 2 == 1) {
          if (x > 0)
            move(y, x, y, x - 1, A, ans);
          else if (y < H)
            move(y, x, y + 1, x, A, ans);
        }
      }
    }
  }

  std::cout << ans.size() << std::endl;
  for (auto const &a : ans) {
    std::cout << std::get<0>(a) << ' ' << std::get<1>(a) << ' '
              << std::get<2>(a) << ' ' << std::get<3>(a) << std::endl;
  }

  return 0;
}


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

using map_t = std::vector<std::vector<size_t>>;
using ans_t = std::vector<std::tuple<size_t, size_t, size_t, size_t>>;

void move(size_t y1, size_t x1, size_t y2, size_t x2, map_t &A, ans_t &ans) {
  A[y1 - 1][x1 - 1]--;
  A[y2 - 1][x2 - 1]++;
  ans.push_back(std::make_tuple(y1, x1, y2, x2));
}

int main() {
  size_t H, W;
  std::cin >> H >> W;

  map_t A(H, std::vector<size_t>(W));

  for (size_t y = 0; y < H; y++) {
    for (size_t x = 0; x < W; x++) {
      std::cin >> A[y][x];
    }
  }

  ans_t ans;

  for (size_t y = 1; y < H + 1; y++) {
    if (y % 2 == 1) {
      for (size_t x = 1; x < W + 1; x++) {
        if (A[y - 1][x - 1] % 2 == 1) {
          if (x < W)
            move(y, x, y, x + 1, A, ans);
          else if (y < H)
            move(y, x, y + 1, x, A, ans);
        }
      }
    } else {
      for (size_t x = W; x > 0; x--) {
        if (A[y - 1][x - 1] % 2 == 1) {
          if (x > 1)
            move(y, x, y, x - 1, A, ans);
          else if (y < H)
            move(y, x, y + 1, x, A, ans);
        }
      }
    }
  }

  std::cout << ans.size() << std::endl;
  for (auto const &a : ans) {
    std::cout << std::get<0>(a) << ' ' << std::get<1>(a) << ' '
              << std::get<2>(a) << ' ' << std::get<3>(a) << std::endl;
  }

  return 0;
}
Problem p03263 (Python)

Buggy submission (status: Runtime Error):
h, w = map(int, input().split())
a = []

for _ in range(h):
    a.append = list(map(int, input().split()))

ans = []

for i in range(h):
    for j in range(w - 1):
        if a[i][j] % 2 == 1:
            ans.append([i + 1, j + 1, i + 1, j + 2])
            a[i][j + 1] += 1

for i in range(h - 1):
    if a[i][w - 1] % 2 == 1:
        ans.append([i + 1, w, i + 2, w])
        a[i + 1][w - 1] += 1

print(len(ans))
for ans in ans:
    print(*ans)


Fixed submission:
h, w = map(int, input().split())
a = []

for _ in range(h):
    a.append(list(map(int, input().split())))

ans = []

for i in range(h):
    for j in range(w - 1):
        if a[i][j] % 2 == 1:
            ans.append([i + 1, j + 1, i + 1, j + 2])
            a[i][j + 1] += 1

for i in range(h - 1):
    if a[i][w - 1] % 2 == 1:
        ans.append([i + 1, w, i + 2, w])
        a[i + 1][w - 1] += 1

print(len(ans))
for ans in ans:
    print(*ans)
Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define int ll

int h, w;
int a[250][251];
int cnt;
vector<int> ans[4];
signed main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      a[i][j] = a[i][j] % 2;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (a[i][j]) {
        a[i][j]--;
        a[i][j + 1] ^= 1;
        ans[0].push_back(i + 1);
        ans[1].push_back(j + 1);
        ans[2].push_back(i + 1);
        ans[3].push_back(j + 2);
        cnt++;
      }
    }
  }
  for (int i = 0; i < h - 1; i++) {
    if (a[i][w - 1]) {
      a[i][w - 1]--;
      a[i + 1][w - 1] ^= 1;
      ans[0].push_back(i + 1);
      ans[1].push_back(w);
      ans[2].push_back(i + 2);
      ans[3].push_back(w);
      cnt++;
    }
  }

  cout << cnt << endl;
  for (int i = 0; i < ans[0].size(); i++) {
    for (int j = 0; j < 4; j++) {
      cout << ans[j][i];
      if (j != 3)
        cout << " ";
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define int ll

int h, w;
int a[500][501];
int cnt;
vector<int> ans[4];
signed main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      a[i][j] = a[i][j] % 2;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (a[i][j]) {
        a[i][j]--;
        a[i][j + 1] ^= 1;
        ans[0].push_back(i + 1);
        ans[1].push_back(j + 1);
        ans[2].push_back(i + 1);
        ans[3].push_back(j + 2);
        cnt++;
      }
    }
  }
  for (int i = 0; i < h - 1; i++) {
    if (a[i][w - 1]) {
      a[i][w - 1]--;
      a[i + 1][w - 1] ^= 1;
      ans[0].push_back(i + 1);
      ans[1].push_back(w);
      ans[2].push_back(i + 2);
      ans[3].push_back(w);
      cnt++;
    }
  }

  cout << cnt << endl;
  for (int i = 0; i < ans[0].size(); i++) {
    for (int j = 0; j < 4; j++) {
      cout << ans[j][i];
      if (j != 3)
        cout << " ";
    }
    cout << endl;
  }
}
Problem p03263 (C++)

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

int main() {
  int H, W;
  vector<vector<int>> a(510, vector<int>(510));

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

  vector<vector<int>> ans;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i == H - 1 && j == W - 1)
        break;

      if (i % 2 == 0) { // 左から右に
        if (a[i][j] % 2 == 1) {
          a[i][j] -= 1;
          if (j == W - 1) {
            a[i + 1][j] += 1;
            ans.push_back({i + 1, j + 1, i + 2, j + 1});
          } else {
            a[i][j + 1] += 1;
            ans.push_back({i + 1, j + 1, i + 1, j + 2});
          }
        }
      }

      if (i % 2 == 1) { // 右から左に
        if (a[i][W - j - 1] % 2 == 1) {
          a[i][W - j - 1] -= 1;
          if (j == 0) {
            a[i + 1][W - j - 1] += 1;
            ans.push_back({i + 1, W - j, i + 2, W - j});
          } else {
            a[i][W - j - 2] += 1;
            ans.push_back({i + 1, W - j, i + 1, W - j - 1});
          }
        }
      }
    }
  }

  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    printf("%d %d %d %d\n", ans[i][0], ans[i][1], ans[i][2], ans[i][3]);
  }
}


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

int main() {
  int H, W;
  vector<vector<int>> a(510, vector<int>(510));

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

  vector<vector<int>> ans;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i == H - 1 && j == W - 1)
        break;

      if (i % 2 == 0) { // 左から右に
        if (a[i][j] % 2 == 1) {
          a[i][j] -= 1;
          if (j == W - 1) {
            a[i + 1][j] += 1;
            ans.push_back({i + 1, j + 1, i + 2, j + 1});
          } else {
            a[i][j + 1] += 1;
            ans.push_back({i + 1, j + 1, i + 1, j + 2});
          }
        }
      }

      if (i % 2 == 1) { // 右から左に
        if (a[i][W - j - 1] % 2 == 1) {
          a[i][W - j - 1] -= 1;
          if (j == W - 1) {
            a[i + 1][W - j - 1] += 1;
            ans.push_back({i + 1, W - j, i + 2, W - j});
          } else {
            a[i][W - j - 2] += 1;
            ans.push_back({i + 1, W - j, i + 1, W - j - 1});
          }
        }
      }
    }
  }

  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    printf("%d %d %d %d\n", ans[i][0], ans[i][1], ans[i][2], ans[i][3]);
  }
}
Problem p03263 (C++)

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

#define each(i, c) for (auto &i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ",";
  os << ")";
  return os;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w;
  while (cin >> h >> w) {
    int g[h][w];
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> g[i][j];
      }
    }

    vector<pair<pair<int, int>, pair<int, int>>> v;
    for (int i = 0; i < h; ++i) {
      vector<int> odd;
      for (int j = 0; j + 1 < w; ++j) {
        if (g[i][j] % 2) {
          odd.push_back(j);
          v.push_back(make_pair(make_pair(i, j), make_pair(i, j + 1)));
          --g[i][j];
          ++g[i][j + 1];
        }
      }
      if (g[i][w - 1] % 2) {
        v.push_back(make_pair(make_pair(i, w - 1), make_pair(i + 1, w - 1)));
        --g[i][w - 1];
        ++g[i + 1][w - 1];
      }
    }

    cout << v.size() << endl;
    each(i, v) {
      int a = i.first.first;
      int b = i.first.second;
      int c = i.second.first;
      int d = i.second.second;
      cout << a + 1 << ' ' << b + 1 << ' ' << c + 1 << ' ' << d + 1 << endl;
    }
  }

  return 0;
}


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

#define each(i, c) for (auto &i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ",";
  os << ")";
  return os;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w;
  while (cin >> h >> w) {
    int g[h][w];
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> g[i][j];
      }
    }

    vector<pair<pair<int, int>, pair<int, int>>> v;
    for (int i = 0; i < h; ++i) {
      vector<int> odd;
      for (int j = 0; j + 1 < w; ++j) {
        if (g[i][j] % 2) {
          odd.push_back(j);
          v.push_back(make_pair(make_pair(i, j), make_pair(i, j + 1)));
          --g[i][j];
          ++g[i][j + 1];
        }
      }
      if (i + 1 < h && g[i][w - 1] % 2) {
        v.push_back(make_pair(make_pair(i, w - 1), make_pair(i + 1, w - 1)));
        --g[i][w - 1];
        ++g[i + 1][w - 1];
      }
    }

    cout << v.size() << endl;
    each(i, v) {
      int a = i.first.first;
      int b = i.first.second;
      int c = i.second.first;
      int d = i.second.second;
      cout << a + 1 << ' ' << b + 1 << ' ' << c + 1 << ' ' << d + 1 << endl;
    }
  }

  return 0;
}
Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
int vv[505][505];
int v[50005][5];
int count = 0;
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &vv[i][j]);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (vv[i][j] % 2 == 1) {
        if (j + 1 <= w && (vv[i][j] + vv[i][j + 1]) % 2 == 0) {
          vv[i][j]--;
          vv[i][j + 1]++;
          count++;
          v[count][1] = v[count][3] = i;
          v[count][2] = j;
          v[count][4] = j + 1;
        } else if (i + 1 <= h && (vv[i][j] + vv[i + 1][j]) % 2 == 0) {
          vv[i][j]--;
          vv[i + 1][j]++;
          count++;
          v[count][1] = i;
          v[count][3] = i + 1;
          v[count][2] = v[count][4] = j;
        } else if (j + 1 <= w) {
          vv[i][j]--;
          vv[i][j + 1]++;
          count++;
          v[count][1] = v[count][3] = i;
          v[count][2] = j;
          v[count][4] = j + 1;
        } else if (i + 1 <= h) {
          vv[i][j]--;
          vv[i + 1][j]++;
          count++;
          v[count][1] = i;
          v[count][3] = i + 1;
          v[count][2] = v[count][4] = j;
        }
      }
    }
  printf("%d\n", count);
  for (int i = 1; i <= count; i++)
    printf("%d %d %d %d\n", v[i][1], v[i][2], v[i][3], v[i][4]);
  return 0;
}


Fixed submission:
#include <cstdio>
using namespace std;
int vv[505][505];
int v[250005][5];
int count = 0;
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &vv[i][j]);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (vv[i][j] % 2 == 1) {
        if (j + 1 <= w && (vv[i][j] + vv[i][j + 1]) % 2 == 0) {
          vv[i][j]--;
          vv[i][j + 1]++;
          count++;
          v[count][1] = v[count][3] = i;
          v[count][2] = j;
          v[count][4] = j + 1;
        } else if (i + 1 <= h && (vv[i][j] + vv[i + 1][j]) % 2 == 0) {
          vv[i][j]--;
          vv[i + 1][j]++;
          count++;
          v[count][1] = i;
          v[count][3] = i + 1;
          v[count][2] = v[count][4] = j;
        } else if (j + 1 <= w) {
          vv[i][j]--;
          vv[i][j + 1]++;
          count++;
          v[count][1] = v[count][3] = i;
          v[count][2] = j;
          v[count][4] = j + 1;
        } else if (i + 1 <= h) {
          vv[i][j]--;
          vv[i + 1][j]++;
          count++;
          v[count][1] = i;
          v[count][3] = i + 1;
          v[count][2] = v[count][4] = j;
        }
      }
    }
  printf("%d\n", count);
  for (int i = 1; i <= count; i++)
    printf("%d %d %d %d\n", v[i][1], v[i][2], v[i][3], v[i][4]);
  return 0;
}
Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> a(H, vector<int>(W));
  vector<vector<int>> ans(H * W, vector<int>(4));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
    }
  }
  int X = 0;
  int Y = 0;
  int A = 0;
  int B = 0;
  int cnt = 0;
  while (X < H && Y < W) {
    bool ok = false;
    if (a[X][Y] % 2 == 1) {
      ok = true;
      A = X;
      B = Y;
    }
    if (X % 2 == 0) {
      if (Y + 1 < W) {
        Y++;
      } else {
        X++;
      }
    } else {
      if (Y - 1 >= 0) {
        Y--;
      } else {
        X++;
      }
    }
    if (ok) {
      a[X][Y] += 1;
      ans[cnt] = {A + 1, B + 1, X + 1, Y + 1};
      cnt++;
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < 4; j++) {
      cout << ans[i][j];
      if (j + 1 != 4) {
        cout << " ";
      }
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> a(H, vector<int>(W));
  vector<vector<int>> ans(H * W, vector<int>(4));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
    }
  }
  int X = 0;
  int Y = 0;
  int A = 0;
  int B = 0;
  int cnt = 0;
  while (X < H && Y < W) {
    bool ok = false;
    if (a[X][Y] % 2 == 1) {
      ok = true;
      A = X;
      B = Y;
    }
    if (X % 2 == 0) {
      if (Y + 1 < W) {
        Y++;
      } else {
        X++;
      }
    } else {
      if (Y - 1 >= 0) {
        Y--;
      } else {
        X++;
      }
    }
    if (ok && X < H) {
      a[X][Y] += 1;
      ans[cnt] = {A + 1, B + 1, X + 1, Y + 1};
      cnt++;
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < 4; j++) {
      cout << ans[i][j];
      if (j + 1 != 4) {
        cout << " ";
      }
    }
    cout << endl;
  }
}
Problem p03263 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  int a[510][510];
  int check[30000][4];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (a[i][j] % 2 == 1) {
        check[ans][0] = i;
        check[ans][1] = j;
        check[ans][2] = i;
        check[ans][3] = j + 1;
        ans++;
        a[i][j + 1]++;
      }
    }
  }

  for (int i = 0; i < h - 1; i++) {
    if (a[i][w - 1] % 2 == 1) {
      check[ans][0] = i;
      check[ans][1] = w - 1;
      check[ans][2] = i + 1;
      check[ans][3] = w - 1;
      ans++;
      a[i + 1][w - 1]++;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < ans; i++) {
    cout << check[i][0] + 1 << " " << check[i][1] + 1 << " " << check[i][2] + 1
         << " " << check[i][3] + 1 << endl;
  }
}

Fixed submission:
#include "iostream"
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int a[510][510];
  int check[250250][4];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      if (a[i][j] % 2 == 1) {
        check[ans][0] = i;
        check[ans][1] = j;
        check[ans][2] = i;
        check[ans][3] = j + 1;
        ans++;
        a[i][j + 1]++;
      }
    }
  }

  for (int i = 0; i < h - 1; i++) {
    if (a[i][w - 1] % 2 == 1) {
      check[ans][0] = i;
      check[ans][1] = w - 1;
      check[ans][2] = i + 1;
      check[ans][3] = w - 1;
      ans++;
      a[i + 1][w - 1]++;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < ans; i++) {
    cout << check[i][0] + 1 << " " << check[i][1] + 1 << " " << check[i][2] + 1
         << " " << check[i][3] + 1 << endl;
  }
}