alexjercan/bugnet huggingface dataset:
Problem p00825 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v) {
    is >> x;
  }
  return is;
}

struct D {
  int l, r, d;
  bool operator<(const D &right) const { return r < right.r; }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<D> d(n);
    rep(i, n) {
      auto &p = d[i];
      cin >> p.l >> p.r >> p.d;
      p.l--;
      p.r--;
    }
    sort(all(d));

    vector<vector<int>> cur(400, vector<int>(400, 0));
    rep(i, n) {
      const auto &p = d[i];
      for (int j = 365; j >= 0; j--) {
        for (int k = 365; k >= 0; k--) {
          if (j <= p.l) {
            cur[p.r + 1][k] = max(cur[p.r + 1][k], cur[j][k] + p.d);
          }
          if (k <= p.l) {
            cur[j][p.r + 1] = max(cur[j][p.r + 1], cur[j][k] + p.d);
          }
        }
      }
    }

    int ans = 0;
    rep(i, 400) {
      rep(j, 400) { ans = max(ans, cur[i][j]); }
    }
    cout << ans << endl;
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v) {
    is >> x;
  }
  return is;
}

struct D {
  int l, r, d;
  bool operator<(const D &right) const { return r < right.r; }
};

int n;
int memo[1001][367][367];
D d[1000];

void dfs(int idx, int i, int j, int sum) {
  if (memo[idx][i][j] >= sum)
    return;
  memo[idx][i][j] = sum;
  if (idx == n)
    return;

  auto p = d[idx];

  // cout << i << ' ' << j << ' ' << sum << endl;
  // cout << p.l << ' ' << p.r << ' ' << p.d << endl;
  idx++;
  if (p.l >= i) {
    dfs(idx, p.r + 1, j, sum + p.d);
  }
  if (p.l >= j) {
    dfs(idx, i, p.r + 1, sum + p.d);
  }
  dfs(idx, i, j, sum);
}

int main() {
  while (cin >> n, n) {
    memset(memo, -1, sizeof(memo));
    rep(i, n) {
      auto &p = d[i];
      cin >> p.l >> p.r >> p.d;
      p.l--;
      p.r--;
    }
    sort(d, d + n);
    dfs(0, 0, 0, 0);

    int ans = 0;
    rep(i, 367) {
      rep(j, 367) { ans = max(ans, memo[n][i][j]); }
    }
    cout << ans << endl;
  }
}
Problem p00827 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <fstream>
#include <iostream>

using namespace std;

void solve(int a, int b, int d, int &x, int &y) {
  for (int i = 1;; i++) {
    for (y = 0; y <= i; y++) {
      x = i - y;
      if (a * x + b * y == d)
        return;
      if (a * x == b * y + d)
        return;
      if (b * y == a * x + d)
        return;
    }
  }
}

int main(void) {
  int a, b, d;
  while (cin >> a >> b >> d) {
    if (a == 0 && b == 0 && d == 0)
      break;

    int x, y;

    if (a < b) {
      solve(a, b, d, x, y);
    } else {
      solve(b, a, d, y, x);
    }

    cout << x << " " << y << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

pair<int, int> solve2(int a, int b, int d) {
  bool swapped = false;
  if (a < b) {
    swap(a, b);
    swapped = true;
  }

  int acnt = d / a;
  while (((d - (acnt * a)) % b) != 0) {
    acnt--;
  }
  int bcnt = (d - (acnt * a)) / b;

  if (acnt < 0)
    return make_pair(999999999, 99999999);
  if (swapped)
    return make_pair(bcnt, acnt);
  else
    return make_pair(acnt, bcnt);
}

pair<int, int> solve(int a, int b, int d) {
  /*
  int md=d%a;
  int fd=a-md;
  int mb=b%a;
  */
  int aa = 0;
  int bb = d;
  int acnt = 0, bcnt = 0;

  while (aa != bb) {
    if (aa < bb) {
      acnt++;
      aa += a;
    } else {
      bcnt++;
      bb += b;
    }
  }
  return make_pair(acnt, bcnt);
}

bool cmp(pair<int, int> &ka, pair<int, int> &kb, int a, int b) {
  int ca = ka.first + ka.second;
  int cb = kb.first + kb.second;

  int cca = a * ka.first + b * ka.second;
  int ccb = a * kb.first + b * kb.second;

  return (ca < cb || (ca == cb && cca < ccb));
}

int main() {
  ifstream cin("A.txt");

  int a, b, d;
  while (cin >> a >> b >> d, (a | b | d) != 0) {
    pair<int, int> ka = solve(a, b, d);
    pair<int, int> kb = solve(b, a, d);
    pair<int, int> kc = solve2(a, b, d);
    swap(kb.first, kb.second);

    if (cmp(ka, kb, a, b) && cmp(ka, kc, a, b))
      cout << ka.first << " " << ka.second << endl;
    else if (cmp(kb, kc, a, b))
      cout << kb.first << " " << kb.second << endl;
    else
      cout << kc.first << " " << kc.second << endl;
  }
  return 0;
}
Problem p00827 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const int inf = 1012345678;
int a, b, c;
int main() {
  while (cin >> a >> b >> c, a | b | c) {
    int rx = inf, ry = inf;
    for (int i = -100000; i <= 100000; i++) {
      int res = abs(c - a * i);
      if (res % b == 0) {
        int y = abs(i), z = res / b;
        if (y + z < rx + ry)
          rx = y, ry = z;
        else if (y + z == rx + ry &&
                 1LL * a * y + 1LL * b * z <= 1LL * a * rx + 1LL * b * ry)
          rx = y, ry = z;
      }
    }
    cout << rx << ' ' << ry << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>

using namespace std;

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

  if (a > b) {
    return gcd(b, a % b);
  }

  return gcd(a, b % a);
}

int extgcd(int a, int b, int &x, int &y) {
  int d = a;

  if (b != 0) {
    d = extgcd(b, a % b, y, x);

    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }

  return d;
}

pair<long long, long long> solve(int a, int b, int c) // ax + by = c
{
  int x, y, d;
  d = extgcd(a, b, x, y);

  x *= (c / d);
  y *= (c / d);

  pair<long long, long long> ret;
  long long e = (1LL << 60);

  for (long long i = -5000000; i <= 5000000; i++) {
    int x2 = x + i * b / d;
    int y2 = y - i * a / d;

    if (abs(x2) + abs(y2) < e) {
      e = abs(x2) + abs(y2);

      ret = make_pair(x2, y2);
    }
  }

  return ret;
}

int main() {
  long long a, b, c;

  while (true) {
    cin >> a >> b >> c;

    if (a == 0 && b == 0 && c == 0) {
      break;
    }

    pair<long long, long long> ret = solve(a, b, c);

    cout << abs(ret.first) << ' ' << abs(ret.second) << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;

ll a, b, d;

bool ok(ll ax, ll ay, ll x, ll y) {
  return ax + ay > x + y || ax + ay == x + y && a * ax + b * ay > a * x + b * y;
}

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

  while (cin >> a >> b >> d, a | b | d) {
    ll ax = INF, ay = INF;
    for (ll x = 0; x <= 50000; x++) {
      if ((d + a * x) % b == 0) {
        ll y = (d + a * x) / b;
        if (ok(ax, ay, x, y)) {
          ax = x;
          ay = y;
        }
      }
      if (a * x - d >= 0 && (a * x - d) % b == 0) {
        ll y = (a * x - d) / b;
        if (ok(ax, ay, x, y)) {
          ax = x;
          ay = y;
        }
      }
      if (d - a * x >= 0 && (d - a * x) % b == 0) {
        ll y = (d - a * x) / b;
        if (ok(ax, ay, x, y)) {
          ax = x;
          ay = y;
        }
      }
    }
    cout << ax << " " << ay << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;

ll a, b, d;

bool ok(ll ax, ll ay, ll x, ll y) {
  return ax + ay > x + y ||
         ax == x && ay == y && a * ax + b * ay > a * x + b * y;
}

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

  while (cin >> a >> b >> d, a | b | d) {
    ll ax = INF, ay = INF;
    for (ll x = 0; x <= 1e7; x++) {
      if ((d + a * x) % b == 0) {
        ll y = (d + a * x) / b;
        if (ok(ax, ay, x, y)) {
          ax = x;
          ay = y;
        }
      }
      if (a * x - d >= 0 && (a * x - d) % b == 0) {
        ll y = (a * x - d) / b;
        if (ok(ax, ay, x, y)) {
          ax = x;
          ay = y;
        }
      }
      if (d - a * x >= 0 && (d - a * x) % b == 0) {
        ll y = (d - a * x) / b;
        if (ok(ax, ay, x, y)) {
          ax = x;
          ay = y;
        }
      }
    }
    cout << ax << " " << ay << endl;
  }
}
Problem p00827 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mp make_pair
#define eps 1e-8
using namespace std;
typedef pair<int, int> pii;
int main() {
  int a, b, d;
  while (cin >> a >> b >> d, a) {
    int x = 0, y = 0;
    pii ans = mp(1e9, 1e9);
    for (int i = -(d / b) - 2 - b; i <= 100000; i++) {
      x = i;
      if ((a * x + d) % b != 0)
        continue;
      y = (a * x + d) / b;
      if (ans.first + ans.second > abs(x) + abs(y)) {
        ans = mp(abs(x), abs(y));
      } else if (ans.first + ans.second == abs(x) + abs(y)) {
        if (a * ans.first + b * ans.second > a * abs(x) + b * abs(y))
          ans = mp(abs(x), abs(y));
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
#define eps 1e-8
using namespace std;
typedef pair<int, int> pii;
long long int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int main() {
  int a, b, d;
  while (cin >> a >> b >> d, a) {
    int x = 0, y = 0;
    pii ans = mp(1e9, 1e9);
    for (long long int i = -(d / b) - 1 - lcm(b, d); i <= 1; i++) {
      x = i;
      if ((a * x + d) % b != 0)
        continue;
      y = (a * x + d) / b;
      if (ans.first + ans.second > abs(x) + abs(y)) {
        ans = mp(abs(x), abs(y));
      } else if (ans.first + ans.second == abs(x) + abs(y)) {
        if (a * ans.first + b * ans.second > a * abs(x) + b * abs(y))
          ans = mp(abs(x), abs(y));
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}
Problem p00827 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int main() {
  while (1) {
    long long int A, B, D;
    cin >> A >> B >> D;
    if (!A)
      break;
    pair<long long int, long long int> ans = make_pair(1e9, 1e9);
    for (int i = -100000; i < 100000; ++i) {
      const int anum = abs(i);
      long long int rest = D - A * i;
      if (!(rest % B)) {
        long long int bnum = abs(rest / B);
        if (anum + bnum < ans.first + ans.second)
          ans = make_pair(anum, bnum);
        if (anum * A + bnum * B < ans.first * A + ans.second * B)
          ans = make_pair(anum, bnum);
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#pragma warning(disable : 4996)
using namespace std;

bool operator<(const pair<int, int> &l, const pair<int, int> &r) {
  return l.first + l.second < r.first + r.second;
}
bool operator>(const pair<int, int> &l, const pair<int, int> &r) {
  return l.first + l.second > r.first + r.second;
}

struct aa {
  int a;
  int num;
};
class Compare {
public:
  // keiroが昇順に並ぶ
  bool operator()(const aa &l, const aa &r) { return l.num > r.num; }
};
int main() {
  while (1) {
    int a, b, c;
    cin >> a >> b >> c;
    if (!a)
      break;
    vector<pair<int, int>> nums(70000, make_pair(999999, 999999));
    nums[a] = make_pair(1, 0);
    nums[b] = make_pair(0, 1);
    priority_queue<aa, vector<aa>, Compare> stack;
    stack.push({a, 0});
    stack.push({b, 0});
    while (!stack.empty()) {
      aa aaa(stack.top());
      int f = aaa.a;
      int num = aaa.num + 1;
      stack.pop();
      pair<int, int> n(nums[f]);
      if (f - a >= 0) {
        if (nums[f - a] > make_pair(n.first + 1, n.second)) {

          nums[f - a] = min(nums[f - a], {n.first + 1, n.second});
          stack.push({f - a, aaa.num});
        }
      }
      if (f - b >= 0) {
        if (nums[f - b] > make_pair(n.first + 1, n.second)) {
          nums[f - b] = min(nums[f - b], {n.first, n.second + 1});
          stack.push({f - b, aaa.num});
        }
      }
      if (f + a <= 60000) {
        if (nums[f + a] > make_pair(n.first + 1, n.second)) {
          nums[f + a] = min(nums[f + a], {n.first + 1, n.second});
          stack.push({f + a, aaa.num});
        }
      }
      if (f + b <= 60000) {
        if (nums[f + b] > make_pair(n.first + 1, n.second)) {
          nums[f + b] = min(nums[f + b], {n.first, n.second + 1});
          stack.push({f + b, aaa.num});
        }
      }
    }
    if (nums[c].first == 999999) {
      cout << -1 << endl;
    } else {
      cout << nums[c].first << " " << nums[c].second << endl;
    }
  }
  return 0;
}
Problem p00827 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
void solve(int a, int b, int d) {
  auto comp = [&](pair<ll, ll> lhs, pair<ll, ll> rhs) {
    return lhs.first + lhs.second == rhs.first + rhs.second
               ? a * lhs.first + b * lhs.second < a * rhs.first + b * rhs.second
               : lhs.first + lhs.second < rhs.first + rhs.second;
  };
  ll xex, yex;
  for (int i = 0; i <= b; i++) {
    if ((d - i * a) % b == 0) {
      xex = i, yex = (d - a * i) / b;
      break;
    }
  }
  pair<ll, ll> res = {30000LL, 30000LL};
  for (int k = -30000; k <= 30000; k++) {
    pair<ll, ll> tmp = {abs(xex + b * k), abs(yex - a * k)};
    if (comp(tmp, res))
      res = tmp;
  }
  cout << res.first << " " << res.second << endl;
}
int main() {
  int a, b, d;
  while (cin >> a >> b >> d, a) {
    int g = __gcd(a, b);
    g = __gcd(g, d);
    a /= g, b /= g, d /= g;
    solve(a, b, d);
  }
  return 0;
}


Error: -8

Fixed submission:

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

void solve(int a, int b, int d) {
  auto comp = [&](pair<int, int> lhs, pair<int, int> rhs) {
    return lhs.first + lhs.second == rhs.first + rhs.second
               ? a * lhs.first + b * lhs.second < a * rhs.first + b * rhs.second
               : lhs.first + lhs.second < rhs.first + rhs.second;
  };
  pair<int, int> res = {10000, 10000};
  for (int x = 0; x <= b; x++) {
    for (int y = 0; y <= a; y++) {
      if (abs(a * x - y * b) == d && comp({x, y}, res)) {
        res = {x, y};
      }
      if (abs(a * x + y * b) == d && comp({x, y}, res)) {
        res = {x, y};
      }
    }
  }
  cout << res.first << " " << res.second << endl;
}
int main() {
  int a, b, d;
  while (cin >> a >> b >> d) {
    int g = __gcd(a, b);
    g = __gcd(g, d);
    a /= g, b /= g, d /= g;
    solve(a, b, d);
  }
  return 0;
}
Problem p00827 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int a, b, d;
struct P {
  int x, y;
  P(int x = -1, int y = -1) : x(x), y(y) {}
  bool operator<(const P &p) const {
    if (x + y != p.x + p.y)
      return x + y < p.x + p.y;
    if (a * x + b * y != a * p.x + b * p.y)
      return a * x + b * y < a * p.x + b * p.y;
    if (x != p.x)
      return x < p.x;
    return y < p.y;
  }
};

int main() {

  while (cin >> a >> b >> d, a | b | d) {
    assert(0 <= a && a <= 10000);
    assert(0 <= b && b <= 10000);
    assert(0 <= d && d <= 50000);
    int ans_x = (1 << 28), ans_y = (1 << 28);
    P p = P(ans_x, ans_y);
    for (int x = 0; x <= 50000; x++) {
      int y;
      if ((a * x + d) % b == 0) {
        y = (a * x + d) / b;
        P pp = P(x, y);
        p = min(p, pp);
      }

      if (x * a - d >= 0 && (x * a - d) % b == 0) {
        y = (x * a - d) / b;
        P pp = P(x, y);
        p = min(p, pp);
      }

      if (d - x * a >= 0 && (d - x * a) % b == 0) {
        y = (d - x * a) / b;
        P pp = P(x, y);
        p = min(p, pp);
      }
    }

    ans_x = p.x, ans_y = p.y;
    cout << ans_x << " " << ans_y << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

struct P {
  int x, y, a, b;
  P(int x = -1, int y = -1, int a = -1, int b = -1) : x(x), y(y), a(a), b(b) {}
  bool operator<(const P &p) const {
    if (x + y != p.x + p.y)
      return x + y < p.x + p.y;
    if (a * x + b * y != a * p.x + b * p.y)
      return a * x + b * y < a * p.x + b * p.y;
    if (x != p.x)
      return x < p.x;
    return y < p.x;
  }
};

int main() {
  int a, b, d;
  while (cin >> a >> b >> d, a | b | d) {
    assert(0 <= a && a <= 10000);
    assert(0 <= b && b <= 10000);
    assert(0 <= d && d <= 50000);
    int ans_x = (1 << 28), ans_y = (1 << 28);
    vector<P> vec;
    for (int x = 0; x <= d; x++) {
      int y;
      if ((a * x + d) % b == 0) {
        y = (a * x + d) / b;
        vec.push_back(P(x, y, a, b));
      }

      if (x * a - d >= 0 && (x * a - d) % b == 0) {
        y = (x * a - d) / b;
        vec.push_back(P(x, y, a, b));
      }

      if (d - x * a >= 0 && (d - x * a) % b == 0) {
        y = (d - x * a) / b;
        vec.push_back(P(x, y, a, b));
      }
    }
    assert(!vec.empty());
    sort(vec.begin(), vec.end());
    ans_x = vec[0].x, ans_y = vec[0].y;
    cout << ans_x << " " << ans_y << endl;
  }
  return 0;
}
Problem p00827 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#define INF (1e9)
using namespace std;

int main() {
  int a, b, c, x, y;
  while (1) {
    x = y = INF;
    cin >> a >> b >> c;
    if (!a && !b && !c)
      break;
    for (int i = 0; i <= 100000000; i += a) {
      int x1, y1, k = abs(i - c);
      if (k % b == 0)
        x1 = i / a, y1 = k / b;
      if (x1 + y1 < x + y)
        x = x1, y = y1;
      if (x1 + y1 == x + y && x1 * a + y1 * a < x * a + y * a)
        x = x1, y = y1;
    }
    for (int i = 0; i <= 100000000; i += b) {
      int x1, y1, k = abs(i - c);
      if (k % a == 0)
        x1 = k / a, y1 = i / b;
      if (x1 + y1 < x + y)
        x = x1, y = y1;
      if (x1 + y1 == x + y && x1 * a + y1 * a < x * a + y * a)
        x = x1, y = y1;
    }
    cout << x << ' ' << y << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#define INF (1e9)
using namespace std;

int main() {
  int a, b, c, x, y;
  while (1) {
    x = y = INF;
    cin >> a >> b >> c;
    if (!a && !b && !c)
      break;
    for (int i = 0; i <= 100000000; i++) {
      int x1, y1, k = abs(i - c);
      if ((i % a) == 0 && (k % b) == 0)
        x1 = i / a, y1 = k / b;
      if ((k % a) == 0 && (i % b) == 0)
        x1 = k / a, y1 = i / b;
      if (x1 + y1 < x + y)
        x = x1, y = y1;
      if (x1 + y1 == x + y && x1 * a + y1 * a < x * a + y * a)
        x = x1, y = y1;
    }
    cout << x << ' ' << y << endl;
  }
  return 0;
}
Problem p00828 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int N, M, P;
int x[1 << 10], y[1 << 10];

const int dx[] = {0, 1, 1, 1, 0, 0, -1, -1, -1, 1, 1, 1, 0};
const int dy[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
const int dz[] = {0, -1, 0, 1, -1, 1, -1, 0, 1, -1, 0, 1, 1};
int fld[8][8][8];
int Stack[8][8];
inline bool check(int x, int y, int z) {
  if (x < 0 || x >= N)
    return false;
  if (y < 0 || y >= N)
    return false;
  if (z < 0 || z >= N)
    return false;
  return true;
}
int counter(int x, int y, int z, int d, int t, int r) {
  if (fld[x][y][z] != t)
    return 0;
  int tx = x + dx[d] * r, ty = y + dy[d] * r, tz = z + dz[d] * r;
  int ret = 0;
  if (check(tx, ty, tz))
    ret += counter(tx, ty, tz, d, t, r);
  return ret + 1;
}
void solve() {
  for (int i = 0; i < P; i++) {
    int pos = Stack[x[i]][y[i]]++;
    fld[x[i]][y[i]][pos] = i & 1;

    for (int k = 0; k < 13; k++) {
      int cnt = 0;
      cnt += counter(x[i], y[i], pos, k, i & 1, 1);
      cnt += counter(x[i], y[i], pos, k, i & 1, -1);
      if (cnt - 1 >= M) {
        if (i & 1)
          printf("White %d\\n", i + 1);
        else
          printf("Black %d\\n", i + 1);
        return;
      }
    }
  }
  puts("Draw");
}
int main() {
  while (scanf("%d%d%d", &N, &M, &P), N || M || P) {
    for (int i = 0; i < P; i++) {
      scanf("%d%d", &x[i], &y[i]);
      x[i]--;
      y[i]--;
    }
    fill_n(**fld, 8 * 8 * 8, -1);
    fill_n(*Stack, 8 * 8, 0);
    solve();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int N, M, P;
int x[32], y[32];

const int dx[] = {0, 1, 1, 1, 0, 0, -1, -1, -1, 1, 1, 1, 0};
const int dy[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
const int dz[] = {0, -1, 0, 1, -1, 1, -1, 0, 1, -1, 0, 1, 1};
int fld[8][8][8];
int Stack[8][8];
inline bool check(int x, int y, int z) {
  if (x < 0 || x >= N)
    return false;
  if (y < 0 || y >= N)
    return false;
  if (z < 0 || z >= N)
    return false;
  return true;
}
int counter(int x, int y, int z, int d, int t, int r) {
  if (fld[x][y][z] != t)
    return 0;
  int tx = x + dx[d] * r, ty = y + dy[d] * r, tz = z + dz[d] * r;
  int ret = 0;
  if (check(tx, ty, tz))
    ret += counter(tx, ty, tz, d, t, r);
  return ret + 1;
}
void solve() {
  for (int i = 0; i < P; i++) {
    int pos = Stack[x[i]][y[i]]++;
    if (pos >= N) {
      continue;
    }
    fld[x[i]][y[i]][pos] = i & 1;

    for (int k = 0; k < 13; k++) {
      int cnt = 0;
      cnt += counter(x[i], y[i], pos, k, i & 1, 1);
      cnt += counter(x[i], y[i], pos, k, i & 1, -1);
      if (cnt - 1 >= M) {
        if (i & 1)
          printf("White %d\\n", i + 1);
        else
          printf("Black %d\\n", i + 1);
        return;
      }
    }
  }
  puts("Draw");
}
int main() {
  while (scanf("%d%d%d", &N, &M, &P), N || M || P) {
    for (int i = 0; i < P; i++) {
      scanf("%d%d", &x[i], &y[i]);
      x[i]--;
      y[i]--;
    }
    fill_n(**fld, 8 * 8 * 8, -1);
    fill_n(*Stack, 8 * 8, 0);
    solve();
  }
}