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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>

char grid[55][55];
int dist[55][55];
const int INF = INT_MAX;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int black = 0;
  rep(i, h) rep(j, w) {
    cin >> grid[i][j];
    dist[i][j] = INF;
    if (grid[i][j] == '#')
      black++;
  }
  dist[0][0] = 0;
  queue<P> q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    int pi, pj;
    tie(pi, pj) = q.front();
    q.pop();
    int past = dist[pi][pj];
    rep(tmp, 4) {
      int ni = pi + di[tmp], nj = pj + dj[tmp];
      int now = past + 1;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (grid[ni][nj] == '#')
        continue;
      if (dist[ni][nj] < now)
        continue;
      dist[ni][nj] = now;
      q.push(make_pair(ni, nj));
    }
  }
  if (dist[h - 1][w - 1] != INF)
    cout << h * w - black - dist[h - 1][w - 1] - 1 << endl;
  else
    cout << -1 << endl;
}


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

char grid[55][55];
int dist[55][55];
const int INF = INT_MAX;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int black = 0;
  rep(i, h) rep(j, w) {
    cin >> grid[i][j];
    dist[i][j] = INF;
    if (grid[i][j] == '#')
      black++;
  }
  dist[0][0] = 0;
  queue<P> q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    int pi, pj;
    tie(pi, pj) = q.front();
    q.pop();
    int past = dist[pi][pj];
    rep(tmp, 4) {
      int ni = pi + di[tmp], nj = pj + dj[tmp];
      int now = past + 1;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (grid[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= now)
        continue;
      dist[ni][nj] = now;
      q.push(make_pair(ni, nj));
    }
  }
  if (dist[h - 1][w - 1] != INF)
    cout << h * w - black - dist[h - 1][w - 1] - 1 << endl;
  else
    cout << -1 << endl;
}
Problem p03436 (C++)

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

vector<vector<char>> s(52, vector<char>(50, '#'));
vector<vector<int>> s_count(52, vector<int>(52, 0));
queue<pair<int, int>> q;
int H, W, tmp_count;

bool push_q(int m, int n) {
  if (s[m][n] != '#' && !s_count[m][n]) {
    s_count[m][n] = tmp_count + 1;
    if (m == H && n == W)
      return (true);
    q.push(make_pair(m, n));
  }
  return (false);
}

int main() {
  int w_count = 0, m, n;
  pair<int, int> tmp;

  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> s[i][j];
      if (s[i][j] == '.')
        w_count++;
    }
  }
  q.push(make_pair(1, 1));
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    m = tmp.first;
    n = tmp.second;
    tmp_count = s_count[m][n];
    if (push_q(m + 1, n) || push_q(m - 1, n) || push_q(m, n + 1) ||
        push_q(m, n - 1))
      break;
  }
  if (s_count[H][W] == 0)
    cout << -1 << endl;
  else
    cout << w_count - s_count[H][W] - 1 << endl;
}

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

vector<vector<char>> s(52, vector<char>(52, '#'));
vector<vector<int>> s_count(52, vector<int>(52, 0));
queue<pair<int, int>> q;
int H, W, tmp_count;

bool push_q(int m, int n) {
  if (s[m][n] != '#' && !s_count[m][n]) {
    s_count[m][n] = tmp_count + 1;
    if (m == H && n == W)
      return (true);
    q.push(make_pair(m, n));
  }
  return (false);
}

int main() {
  int w_count = 0, m, n;
  pair<int, int> tmp;

  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> s[i][j];
      if (s[i][j] == '.')
        w_count++;
    }
  }
  q.push(make_pair(1, 1));
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    m = tmp.first;
    n = tmp.second;
    tmp_count = s_count[m][n];
    if (push_q(m + 1, n) || push_q(m - 1, n) || push_q(m, n + 1) ||
        push_q(m, n - 1))
      break;
  }
  if (s_count[H][W] == 0)
    cout << -1 << endl;
  else
    cout << w_count - s_count[H][W] - 1 << endl;
}
Problem p03436 (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 int long long
typedef pair<int, int> P;
int n, m;
char a[55][55];
int f[111][111];
int black;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
signed main() {
  cin >> n >> m;
  rep(i, n) rep(j, m) {
    cin >> a[i][j];
    black += (a[i][j] == '#');
    f[i][j] = 1145141919;
  }
  queue<P> q;
  q.push({0, 0});
  f[0][0] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    rep(i, 4) {
      int x = p.first + dx[i];
      int y = p.second + dy[i];
      if (x < 0 || x >= n || y < 0 || y >= m || a[x][y] == '#')
        continue;
      if (f[x][y] > f[p.first][p.second]) {
        f[x][y] = f[p.first][p.second] + 1;
        q.push({x, y});
      }
    }
  }
  if (f[n - 1][m - 1] == 1145141919)
    puts("-1");
  else
    cout << n * m - f[n - 1][m - 1] - black - 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
typedef pair<int, int> P;
int n, m;
char a[55][55];
int f[111][111];
int black;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
signed main() {
  cin >> n >> m;
  rep(i, n) rep(j, m) {
    cin >> a[i][j];
    black += (a[i][j] == '#');
    f[i][j] = 1145141919;
  }
  queue<P> q;
  q.push({0, 0});
  f[0][0] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    rep(i, 4) {
      int x = p.first + dx[i];
      int y = p.second + dy[i];
      if (x < 0 || x >= n || y < 0 || y >= m || a[x][y] == '#')
        continue;
      if (f[x][y] > f[p.first][p.second] + 1) {
        f[x][y] = f[p.first][p.second] + 1;
        q.push({x, y});
      }
    }
  }
  if (f[n - 1][m - 1] == 1145141919)
    puts("-1");
  else
    cout << n * m - f[n - 1][m - 1] - black - 1 << endl;
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  int H, W, black = 0;
  cin >> H >> W;
  char tab[H + 2][W + 2];
  int num[H + 2][W + 2];
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      tab[i][j] = '@';
      num[i][j] = 0;
    }
  }
  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> tab[i][j];
      if (tab[i][j] == '#')
        black++;
    }
  }
  queue<int> tate, yoko;
  tate.push(1);
  yoko.push(1);
  num[1][1] = 1;
  while (1) {
    int t = tate.front(), y = yoko.front();
    tate.pop();
    yoko.pop();
    for (int k = 0; k < 4; k++) {
      int i, j;
      if (k == 0) {
        i = 1;
        j = 0;
      } else if (k == 1) {
        i = -1;
        j = 0;
      } else if (k == 2) {
        i = 0;
        j = 1;
      } else {
        i = 0;
        j = -1;
      }
      if (tab[t + i][y + j] == '.') {
        tate.push(t + i);
        yoko.push(y + j);
        num[t + i][y + j] = num[t][y] + 1;
        tab[t][y] = '@';
      } else {
        continue;
      }
      if (t + i == H && y + j == W) {
        cout << H * W - black - num[H][W] << endl;
        return 0;
      }
    }
    if (tate.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  int H, W, black = 0;
  cin >> H >> W;
  char tab[H + 2][W + 2];
  int num[H + 2][W + 2];
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      tab[i][j] = '@';
      num[i][j] = 0;
    }
  }
  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> tab[i][j];
      if (tab[i][j] == '#')
        black++;
    }
  }
  queue<int> tate, yoko;
  tate.push(1);
  yoko.push(1);
  num[1][1] = 1;
  while (1) {
    int t = tate.front(), y = yoko.front();
    tate.pop();
    yoko.pop();
    for (int k = 0; k < 4; k++) {
      int i, j;
      if (k == 0) {
        i = 1;
        j = 0;
      } else if (k == 1) {
        i = -1;
        j = 0;
      } else if (k == 2) {
        i = 0;
        j = 1;
      } else {
        i = 0;
        j = -1;
      }
      if (tab[t + i][y + j] == '.') {
        tate.push(t + i);
        yoko.push(y + j);
        num[t + i][y + j] = num[t][y] + 1;
        tab[t][y] = '@';
        tab[t + i][y + j] = '@';
      } else {
        continue;
      }
      if (t + i == H && y + j == W) {
        cout << H * W - black - num[H][W] << endl;
        return 0;
      }
    }
    if (tate.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long int lld;
typedef unsigned long long int llu;
#define INF (lld)(9223372036854775807)
#define IINF (int)(2147483647)

using namespace std;
int main(void) {
  lld h, w, b = 0, d[50][50];
  char s[50][50];
  queue<tuple<lld, lld, lld>> q;
  cin >> h >> w;
  for (lld i = 0; i < h; i++) {
    cin.ignore(1);
    for (lld j = 0; j < w; j++) {
      cin >> s[j][i];
      b += s[j][i] == '#';
      d[j][i] = -1;
    }
  }

  q.push(make_tuple(0, 0, 1));
  while (!q.empty()) {
    auto next = q.front();
    lld x, y, p;
    tie(x, y, p) = next;
    q.pop();
    d[x][y] = p;
    if (x > 0 && d[x - 1][y] == -1 && s[x - 1][y] != '#')
      q.push(make_tuple(x - 1, y, p + 1));
    if (y > 0 && d[x][y - 1] == -1 && s[x][y - 1] != '#')
      q.push(make_tuple(x, y - 1, p + 1));
    if (x < w - 1 && d[x + 1][y] == -1 && s[x + 1][y] != '#')
      q.push(make_tuple(x + 1, y, p + 1));
    if (y < h - 1 && d[x][y + 1] == -1 && s[x][y + 1] != '#')
      q.push(make_tuple(x, y + 1, p + 1));
  }

  cout << (d[w - 1][h - 1] == -1 ? -1 : h * w - b - d[w - 1][h - 1]) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int lld;
typedef unsigned long long int llu;
#define INF (lld)(9223372036854775807)
#define IINF (int)(2147483647)

using namespace std;
int main(void) {
  lld h, w, b = 0, d[50][50];
  char s[50][50];
  queue<tuple<lld, lld, lld>> q;
  cin >> h >> w;
  for (lld i = 0; i < h; i++) {
    cin.ignore(1);
    for (lld j = 0; j < w; j++) {
      cin >> s[j][i];
      b += s[j][i] == '#';
      d[j][i] = -1;
    }
  }

  q.push(make_tuple(0, 0, 1));
  while (!q.empty()) {
    auto next = q.front();
    lld x, y, p;
    tie(x, y, p) = next;
    q.pop();
    if (d[x][y] != -1) {
      continue;
    }
    d[x][y] = p;
    if (x > 0 && d[x - 1][y] == -1 && s[x - 1][y] != '#')
      q.push(make_tuple(x - 1, y, p + 1));
    if (y > 0 && d[x][y - 1] == -1 && s[x][y - 1] != '#')
      q.push(make_tuple(x, y - 1, p + 1));
    if (x < w - 1 && d[x + 1][y] == -1 && s[x + 1][y] != '#')
      q.push(make_tuple(x + 1, y, p + 1));
    if (y < h - 1 && d[x][y + 1] == -1 && s[x][y + 1] != '#')
      q.push(make_tuple(x, y + 1, p + 1));
  }

  cout << (d[w - 1][h - 1] == -1 ? -1 : h * w - b - d[w - 1][h - 1]) << "\n";
}
Problem p03436 (C++)

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

int main() {
  int r, c, i, j, x, y, land = 0;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  pair<int, int> tem, g;
  queue<pair<int, int>> q;
  cin >> r >> c;
  g = make_pair(r - 1, c - 1);
  string mp[r];
  for (i = 0; i < r; i++) {
    cin >> mp[i];
  }
  int d[r][c];
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      d[i][j] = -1;
      if (mp[i][j] == '#')
        land++;
    }
  }
  d[0][0] = 0;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    tem = q.front();
    q.pop();
    if (tem == g) {
      break;
    }
    for (i = 0; i < 4; i++) {
      y = tem.first + dy[i];
      x = tem.second + dx[i];
      if (mp[y][x] == '.' && d[y][x] == -1) {
        q.push(make_pair(y, x));
        d[y][x] = d[tem.first][tem.second] + 1;
      }
    }
  }
  if (d[r - 1][c - 1] == -1) {
    cout << "-1";
  } else {
    cout << r * c - land - d[r - 1][c - 1] - 1;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;

int main() {
  int r, c, i, j, x, y, land = 0;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  pair<int, int> tem, g;
  queue<pair<int, int>> q;
  cin >> r >> c;
  g = make_pair(r - 1, c - 1);
  string mp[r];
  for (i = 0; i < r; i++) {
    cin >> mp[i];
  }
  int d[r][c];
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      d[i][j] = -1;
      if (mp[i][j] == '#')
        land++;
    }
  }
  d[0][0] = 0;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    tem = q.front();
    q.pop();
    if (tem == g) {
      break;
    }
    for (i = 0; i < 4; i++) {
      y = tem.first + dy[i];
      x = tem.second + dx[i];
      if (0 <= y && y < r && 0 <= x && x < c && mp[y][x] == '.' &&
          d[y][x] == -1) {
        q.push(make_pair(y, x));
        d[y][x] = d[tem.first][tem.second] + 1;
      }
    }
  }
  if (d[r - 1][c - 1] == -1) {
    cout << "-1";
  } else {
    cout << r * c - land - d[r - 1][c - 1] - 1;
  }
  return 0;
}
Problem p03436 (C++)

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

//  四方向へのベクトル
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int h, w;
// 探索用
// bool seen[1000][1000];
int MAX = 0;
long long sum = 0;
int seen[2000][2000];

int bfs(const vector<string> &G, int x, int y) {
  queue<int> q; // x,yの順に格納
  q.push(x);
  q.push(y);
  seen[y][x] = 1;

  while (true) {
    int cc = q.front();
    q.pop();
    int rr = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int next_x = cc + dx[i];
      int next_y = rr + dy[i];

      if (next_x <= 0 || next_x > w || next_y <= 0 || next_y > h)
        continue;
      if (seen[next_y][next_x] != -1)
        continue;
      if (G[next_y - 1][next_x - 1] == '#')
        continue;

      q.push(next_x);
      q.push(next_y);

      seen[next_y][next_x] = seen[rr][cc] + 1;
    }

    if (seen[h][w] != -1)
      break;
  }

  return 0;
}

int main() {
  cin >> h >> w;

  vector<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];
  memset(seen, -1, sizeof(seen));

  for (int i = 0; i < h; i++) {
    for (int k = 0; k < w; k++) {
      if (G[i][k] == '#') {
        MAX++;
      }
    }
  }

  bfs(G, 1, 1);

  if (seen[h][w] == -1) {
    cout << -1 << endl;
    return 0;
  }
  int no_trans = MAX + seen[h][w];

  cout << h * w - no_trans << endl;
  return 0;
}


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

//  四方向へのベクトル
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int h, w;
// 探索用
// bool seen[1000][1000];
int MAX = 0;
long long sum = 0;
int seen[2000][2000];

int bfs(const vector<string> &G, int x, int y) {
  queue<int> q; // x,yの順に格納
  q.push(x);
  q.push(y);
  seen[y][x] = 1;

  while (true) {
    int cc = q.front();
    q.pop();
    int rr = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int next_x = cc + dx[i];
      int next_y = rr + dy[i];

      if (next_x <= 0 || next_x > w || next_y <= 0 || next_y > h)
        continue;
      if (seen[next_y][next_x] != -1)
        continue;
      if (G[next_y - 1][next_x - 1] == '#')
        continue;

      q.push(next_x);
      q.push(next_y);

      seen[next_y][next_x] = seen[rr][cc] + 1;
    }

    if (seen[h][w] != -1 || q.size() == 0)
      break;
  }

  return 0;
}

int main() {
  cin >> h >> w;

  vector<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];
  memset(seen, -1, sizeof(seen));

  for (int i = 0; i < h; i++) {
    for (int k = 0; k < w; k++) {
      if (G[i][k] == '#') {
        MAX++;
      }
    }
  }

  bfs(G, 1, 1);

  if (seen[h][w] == -1) {
    cout << -1 << endl;
    return 0;
  }
  int no_trans = MAX + seen[h][w];

  cout << h * w - no_trans << endl;
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
// AtCoder template
// 見た奴全員saba!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  int visited[h][w];
  rep(i, h) rep(j, w) visited[i][j] = -1;
  int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  queue<tuple<int, int, int>> que;
  que.emplace(0, 0, 1);
  int goal = 0;
  while (!que.empty()) {
    int y = get<0>(que.front()), x = get<1>(que.front()),
        ncnt = get<2>(que.front());
    que.pop();
    visited[y][x] = ncnt;
    if (y == h - 1 && x == w - 1) {
      goal = ncnt;
      break;
    }
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (visited[ny][nx] >= 0 || s[ny][nx] != '.')
        continue;
      que.emplace(ny, nx, ncnt + 1);
    }
  }
  if (goal == 0) {
    cout << -1 << endl;
    return 0;
  }

  // 全体の移動可能マス集計
  int all_white = 0;
  rep(i, h) rep(j, w) if (s[i][j] == '.') all_white++;

  cout << all_white - goal << endl;
}

Fixed submission:
// AtCoder template
// 見た奴全員saba!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  int visited[h][w];
  rep(i, h) rep(j, w) visited[i][j] = -1;
  int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  queue<tuple<int, int, int>> que;
  que.emplace(0, 0, 1);
  int goal = 0;
  while (!que.empty()) {
    int y = get<0>(que.front()), x = get<1>(que.front()),
        ncnt = get<2>(que.front());
    que.pop();
    visited[y][x] = ncnt;
    if (y == h - 1 && x == w - 1) {
      goal = ncnt;
      break;
    }
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (visited[ny][nx] >= 0 || s[ny][nx] != '.')
        continue;
      visited[ny][nx] = ncnt + 1;
      que.emplace(ny, nx, ncnt + 1);
    }
  }
  if (goal == 0) {
    cout << -1 << endl;
    return 0;
  }

  // 全体の移動可能マス集計
  int all_white = 0;
  rep(i, h) rep(j, w) if (s[i][j] == '.') all_white++;

  cout << all_white - goal << endl;
}
Problem p03436 (C++)

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

using namespace std;

#define FOR(i, s, e) for (int i = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)
#define each(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp make_pair
#define mt make_tuple
#define t0(t) get<0>((t))
#define t1(t) get<1>((t))
#define t2(t) get<2>((t))

typedef long long ll;
const int INF = 1e4;
const int H = 50;
const int W = 50;
int h, w;
int b[H][W], d[H][W];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  fscanf(stdin, "%d%d ", &h, &w);
  int res = h * w;
  REP(i, h) {
    string s;
    cin >> s;
    REP(j, w) {
      if (s[j] == '.')
        b[i][j] = 1;
      else
        res--;
    }
  }
  REP(i, h) REP(j, w) d[i][j] = INF;
  d[0][0] = 0;
  queue<tuple<int, int>> q;
  q.push(mt(0, 0));
  while (q.size()) {
    tuple<int, int> t = q.front();
    q.pop();
    int y = t0(t), x = t1(t);
    if (y == h - 1 && x == w - 1)
      break;
    REP(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h)
        continue;
      if (nx < 0 || nx >= w)
        continue;
      if (!b[ny][nx])
        continue;
      if (d[ny][nx] < d[y][x] + 1)
        continue;
      d[ny][nx] = d[y][x] + 1;
      q.push(mt(ny, nx));
    }
  }
  if (d[h - 1][w - 1] == INF)
    res = -1;
  else
    res -= d[h - 1][w - 1] + 1;
  fprintf(stdout, "%d\n", res);
  return 0;
}


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

using namespace std;

#define FOR(i, s, e) for (int i = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)
#define each(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp make_pair
#define mt make_tuple
#define t0(t) get<0>((t))
#define t1(t) get<1>((t))
#define t2(t) get<2>((t))

typedef long long ll;
const int INF = 1e4;
const int H = 50;
const int W = 50;
int h, w;
int b[H][W], d[H][W];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  fscanf(stdin, "%d%d ", &h, &w);
  int res = h * w;
  REP(i, h) {
    string s;
    cin >> s;
    REP(j, w) {
      if (s[j] == '.')
        b[i][j] = 1;
      else
        res--;
    }
  }
  REP(i, h) REP(j, w) d[i][j] = INF;
  d[0][0] = 0;
  queue<tuple<int, int>> q;
  q.push(mt(0, 0));
  while (q.size()) {
    tuple<int, int> t = q.front();
    q.pop();
    int y = t0(t), x = t1(t);
    if (y == h - 1 && x == w - 1)
      break;
    REP(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h)
        continue;
      if (nx < 0 || nx >= w)
        continue;
      if (!b[ny][nx])
        continue;
      if (d[ny][nx] <= d[y][x] + 1)
        continue;
      d[ny][nx] = d[y][x] + 1;
      q.push(mt(ny, nx));
    }
  }
  if (d[h - 1][w - 1] == INF)
    res = -1;
  else
    res -= d[h - 1][w - 1] + 1;
  fprintf(stdout, "%d\n", res);
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < (n); i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REPEACH(itr, k) for (auto &&itr : k)
#define MOD 1000000007

using namespace std;
typedef long long ll;

const ll INF = 1ll << 60;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> v(h + 2, vector<char>(w + 2, '#'));
  int ans = 0;
  REP(i, h) REP(j, w) {
    cin >> v[i + 1][j + 1];
    if (v[i + 1][j + 1] == '.') {
      ans++;
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(1, 1));
  queue<int> count;
  count.push(0);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    v[x][y] = '#';
    int c = count.front();
    count.pop();
    q.pop();
    if (x == h && y == w) {
      cout << ans - c - 1 << endl;
      return 0;
    }
    if (v[x - 1][y] == '.') {
      q.push(make_pair(x - 1, y));
      count.push(c + 1);
    }
    if (v[x + 1][y] == '.') {
      q.push(make_pair(x + 1, y));
      count.push(c + 1);
    }
    if (v[x][y - 1] == '.') {
      q.push(make_pair(x, y - 1));
      count.push(c + 1);
    }
    if (v[x][y + 1] == '.') {
      q.push(make_pair(x, y + 1));
      count.push(c + 1);
    }
  }
  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < (n); i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REPEACH(itr, k) for (auto &&itr : k)
#define MOD 1000000007

using namespace std;
typedef long long ll;

const ll INF = 1ll << 60;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> v(h + 2, vector<char>(w + 2, '#'));
  int ans = 0;
  REP(i, h) REP(j, w) {
    cin >> v[i + 1][j + 1];
    if (v[i + 1][j + 1] == '.') {
      ans++;
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(1, 1));
  queue<int> count;
  count.push(0);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    if (v[x][y] == '#') {
      q.pop();
      count.pop();
      continue;
    }
    v[x][y] = '#';
    int c = count.front();
    count.pop();
    q.pop();
    if (x == h && y == w) {
      cout << ans - c - 1 << endl;
      return 0;
    }
    if (v[x - 1][y] == '.') {
      q.push(make_pair(x - 1, y));
      count.push(c + 1);
    }
    if (v[x + 1][y] == '.') {
      q.push(make_pair(x + 1, y));
      count.push(c + 1);
    }
    if (v[x][y - 1] == '.') {
      q.push(make_pair(x, y - 1));
      count.push(c + 1);
    }
    if (v[x][y + 1] == '.') {
      q.push(make_pair(x, y + 1));
      count.push(c + 1);
    }
  }
  cout << -1 << endl;
}
Problem p03436 (C++)

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

// repr(i,10,13) -> 10,11,12
#define repr(i, a, b) for (int i = a; i < b; i++)
// rep(i,3) -> 0,1,2
#define rep(i, n) for (int i = 0; i < n; i++)
// reprrev(i,10,13) -> 12,11,10
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--)
// reprev(i,3) -> 2,1,0
#define reprev(i, n) reprrev(i, 0, n)

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
const int inf = 1 << 29;
typedef tuple<int, int> T;

int main() {
  ios::sync_with_stdio(false);
  int y, x;
  cin >> y >> x;
  vector<vector<char>> c = vector<vector<char>>(y + 1, vector<char>(x + 1));

  int blank = x * y;
  repr(i, 1, y + 1) {
    repr(j, 1, x + 1) {
      cin >> c[i][j];
      if (c[i][j] == '#') {
        blank--;
      }
    }
  }
  queue<T> q;
  vector<vector<int>> s = vector<vector<int>>(y + 1, vector<int>(x + 1, inf));
  s[1][1] = 1;

  int min;
  q.push(T(1, 1));
  while (!q.empty()) {
    T data = q.front();
    q.pop();
    int cy = get<0>(data);
    int cx = get<1>(data);
    if (cy == y && cx == x) {
      min = s[cy][cx];
      break;
    }
    rep(i, 4) {
      int cyd = cy + dy[i];
      int cxd = cx + dx[i];
      if (1 <= cyd && cyd <= y && 1 <= cxd && cxd <= x && c[cyd][cxd] != '#' &&
          s[cy][cx] + 1 <= s[cyd][cxd]) {
        q.push(T(cyd, cxd));
        s[cyd][cxd] = s[cy][cx] + 1;
      }
    }
  }
  if (s[y][x] != inf) {
    cout << blank - min << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

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

// repr(i,10,13) -> 10,11,12
#define repr(i, a, b) for (int i = a; i < b; i++)
// rep(i,3) -> 0,1,2
#define rep(i, n) for (int i = 0; i < n; i++)
// reprrev(i,10,13) -> 12,11,10
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--)
// reprev(i,3) -> 2,1,0
#define reprev(i, n) reprrev(i, 0, n)

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
const int inf = 1 << 29;
typedef tuple<int, int> T;

int main() {
  ios::sync_with_stdio(false);
  int y, x;
  cin >> y >> x;
  vector<vector<char>> c = vector<vector<char>>(y + 1, vector<char>(x + 1));

  int blank = x * y;
  repr(i, 1, y + 1) {
    repr(j, 1, x + 1) {
      cin >> c[i][j];
      if (c[i][j] == '#') {
        blank--;
      }
    }
  }
  queue<T> q;
  vector<vector<int>> s = vector<vector<int>>(y + 1, vector<int>(x + 1, inf));
  s[1][1] = 1;

  int min;
  q.push(T(1, 1));
  while (!q.empty()) {
    T data = q.front();
    q.pop();
    int cy = get<0>(data);
    int cx = get<1>(data);
    if (cy == y && cx == x) {
      min = s[cy][cx];
      break;
    }
    rep(i, 4) {
      int cyd = cy + dy[i];
      int cxd = cx + dx[i];
      if (1 <= cyd && cyd <= y && 1 <= cxd && cxd <= x && c[cyd][cxd] != '#' &&
          s[cy][cx] + 1 < s[cyd][cxd]) {
        q.push(T(cyd, cxd));
        s[cyd][cxd] = s[cy][cx] + 1;
      }
    }
  }
  if (s[y][x] != inf) {
    cout << blank - min << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
Problem p03436 (C++)

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

char tab[55][55];
bool vis[55][55];
queue<pair<pair<int, int>, int>> q;
int dist[55][55];
int n, m;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

void bfs() {
  memset(dist, -1, sizeof(dist));
  q.push({{1, 1}, 1});
  while (!q.empty()) {
    pair<pair<int, int>, int> depan = q.front();
    q.pop();
    int x = depan.first.first;
    int y = depan.first.second;
    vis[x][y] = 1;
    int step = depan.second;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (1 <= xx && xx <= n && 1 <= yy && yy <= m && !vis[xx][yy] &&
          tab[xx][yy] == '.') {
        q.push({{xx, yy}, step + 1});
        dist[xx][yy] = step + 1;
      }
    }
  }
}

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n >> m;
  int hitam = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> tab[i][j];
      hitam += (tab[i][j] == '#');
    }
  bfs();
  if (dist[n][m] == -1)
    cout << -1 << "\n";
  else {
    int ans = n * m - dist[n][m] - hitam;
    cout << ans << "\n";
  }
}

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

char tab[55][55];
bool vis[55][55];
queue<pair<pair<int, int>, int>> q;
int dist[55][55];
int n, m;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

void bfs() {
  memset(dist, -1, sizeof(dist));
  q.push({{1, 1}, 1});
  while (!q.empty()) {
    pair<pair<int, int>, int> depan = q.front();
    q.pop();
    int x = depan.first.first;
    int y = depan.first.second;
    vis[x][y] = 1;
    int step = depan.second;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (1 <= xx && xx <= n && 1 <= yy && yy <= m && !vis[xx][yy] &&
          tab[xx][yy] == '.') {
        q.push({{xx, yy}, step + 1});
        dist[xx][yy] = step + 1;
        vis[xx][yy] = 1;
      }
    }
  }
}

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n >> m;
  int hitam = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> tab[i][j];
      hitam += (tab[i][j] == '#');
    }
  bfs();
  if (dist[n][m] == -1)
    cout << -1 << "\n";
  else {
    int ans = n * m - dist[n][m] - hitam;
    cout << ans << "\n";
  }
}
Problem p03436 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.')
        cnt++;
    }
  }

  queue<pii> q;
  pii g(h - 1, w - 1);
  q.push(pii(0, 0));
  int stp = -1;
  for (int i = 1; 1; i++) {
    int j = q.size();
    for (int k = 0; k < j; k++) {
      if (q.front() == g) {
        stp = i;
        break;
      }
      int a = q.front().first;
      int b = q.front().second;
      q.pop();
      if (s[a][b] != '.')
        continue;
      s[a][b] = ':';
      if (a > 0)
        q.push(pii(a - 1, b));
      if (a < h - 1)
        q.push(pii(a + 1, b));
      if (b > 0)
        q.push(pii(a, b - 1));
      if (b < w - 1)
        q.push(pii(a, b + 1));
    }
    if (stp >= 0)
      break;
  }

  cout << cnt - stp << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.')
        cnt++;
    }
  }

  queue<pii> q;
  pii g(h - 1, w - 1);
  q.push(pii(0, 0));
  int stp = -1;
  for (int i = 1; 1; i++) {
    int j = q.size();
    for (int k = 0; k < j; k++) {
      if (q.front() == g) {
        stp = i;
        break;
      }
      int a = q.front().first;
      int b = q.front().second;
      q.pop();
      if (s[a][b] != '.')
        continue;
      s[a][b] = ':';
      if (a > 0)
        q.push(pii(a - 1, b));
      if (a < h - 1)
        q.push(pii(a + 1, b));
      if (b > 0)
        q.push(pii(a, b - 1));
      if (b < w - 1)
        q.push(pii(a, b + 1));
    }
    if (stp >= 0)
      break;
    if (q.empty()) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << cnt - stp << endl;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x)                                                                \
  for (auto &&itr : x) {                                                       \
    cerr << (itr) << " ";                                                      \
  }

template <class T> inline void chmax(T &ans, T t) {
  if (t > ans)
    ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
  if (t < ans)
    ans = t;
}

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

  unsigned long long x, y;
  cin >> x >> y;
  if (x == y || y == 1) {
    cout << -1 << endl;
    return 0;
  }

  unsigned long long maxNum = 1000000000000000000;

  for (unsigned long long i = 1; i * x <= maxNum; ++i) {
    if ((i * x) % y != 0) {
      cout << x << endl;
      return 0;
    }
  }

  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x)                                                                \
  for (auto &&itr : x) {                                                       \
    cerr << (itr) << " ";                                                      \
  }

template <class T> inline void chmax(T &ans, T t) {
  if (t > ans)
    ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
  if (t < ans)
    ans = t;
}

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

  unsigned long long x, y;
  cin >> x >> y;
  if (x % y == 0 || y == 1) {
    cout << -1 << endl;
    return 0;
  }

  unsigned long long maxNum = 1000000000000000000;

  for (unsigned long long i = 1; i * x <= maxNum; ++i) {
    if ((i * x) % y != 0) {
      cout << x << endl;
      return 0;
    }
  }

  cout << -1 << endl;
}
Problem p03437 (Python)

Buggy submission (status: Time Limit Exceeded):
X, Y = map(int, input().split())
i = 1
if X % Y == 0:
    print(-1)
else:
    while True:
        if i % X == 0 and i % Y != 0:
            print(i)
            break
        else:
            i += 1


Fixed submission:
X, Y = map(int, input().split())
i = 1
if X % Y == 0:
    print(-1)
else:
    print(X)
Problem p03437 (C++)

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

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

  for (long long i = 1;; i++) {
    if (x * i % y != 0 || x < y) {
      cout << x * i << endl;
      break;
    } else if (x == y || x * i > 1000000000000000000 || x == 1 || y == 1) {
      cout << -1 << endl;
      break;
    }
  }

  return 0;
}

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

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

  for (long long i = 1;; i++) {
    if (x * i % y != 0 || x < y) {
      cout << x * i << endl;
      break;
    } else if (x * i % y == 0 || x == y || x * i > 1000000000000000000 ||
               x == 1 || y == 1) {
      cout << -1 << endl;
      break;
    }
  }

  return 0;
}
Problem p03437 (C++)

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

int main(void) {
  long long x, y;
  std::cin >> x >> y;
  for (long long i = 1; i < y; ++i) {
    auto z = x * i;
    if (x * i % y != 0) {
      printf("%lld\n", z);
      return 0;
    }
  }
  std::cout << -1 << std::endl;
  return 0;
}


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

int main(void) {
  long long x, y;
  std::cin >> x >> y;
  if (x % y) {
    printf("%lld\n", x);
    return 0;
  }
  std::cout << -1 << std::endl;
  return 0;
}
Problem p03437 (C++)

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

int main() {
  int x, y;
  cin >> x >> y;
  const long long a = 1000000000000000000;

  if (x == y || y == 1) {
    cout << -1 << endl;
    return 0;
  }

  for (long long i = x; i <= a; i += x) {
    if (i % y != 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define v(type) vector<type>

int main() {
  int x, y;
  cin >> x >> y;
  const long long a = 1000000000000000000;

  if (x % y == 0) {
    cout << -1 << endl;
    return 0;
  }

  for (long long i = x; i <= a; i += x) {
    if (i % y != 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define lp(i, a, n) for (int i = a; i <= n; ++i)
#define lpd(i, a, n) for (int i = a; i >= n; --i)
#define mem(a, b) memset(a, b, sizeof a)
#define all(v) v.begin(), v.end()
#define println(a) cout << (a) << endl
#define sz(x) ((int)(x).size())
#define readi(x) scanf("%d", &x)
#define read2i(x, y) scanf("%d%d", &x, &y)
#define read3i(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define mod 1000000007
#define eps 1e-8
#define infi 1000000000
#define infll 1000000000000000000ll
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef set<int> si;
typedef map<int, int> mii;

const int N = 100002;
int x, y;

int main() {
  read2i(x, y);
  if (x == y or y == 1)
    return cout << -1, 0;
  lp(i, 2, infi) if (1ll * i * x % y) return cout << 1ll * i * x, 0;
}

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/


Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define lp(i, a, n) for (int i = a; i <= n; ++i)
#define lpd(i, a, n) for (int i = a; i >= n; --i)
#define mem(a, b) memset(a, b, sizeof a)
#define all(v) v.begin(), v.end()
#define println(a) cout << (a) << endl
#define sz(x) ((int)(x).size())
#define readi(x) scanf("%d", &x)
#define read2i(x, y) scanf("%d%d", &x, &y)
#define read3i(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define mod 1000000007
#define eps 1e-8
#define infi 1000000000
#define infll 1000000000000000000ll
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef set<int> si;
typedef map<int, int> mii;

const int N = 100002;
int x, y;

int main() {
  read2i(x, y);
  if (x % y == 0)
    return cout << -1, 0;
  lp(i, 2, infi) if (1ll * i * x % y) return cout << 1ll * i * x, 0;
}

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main() {
  const ll LIMIT = pow(10, 18);
  ll x, y;
  scanf("%lld %lld", &x, &y);
  if (x == y) {
    puts("-1");
    return 0;
  }
  for (ll i = 2; x * i < LIMIT; ++i) {
    if (x * i % y != 0) {
      printf("%lld\n", x * i);
      return 0;
    }
  }
  puts("-1");
  return 0;
}

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

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main() {
  const ll LIMIT = pow(10, 18);
  ll x, y;
  scanf("%lld %lld", &x, &y);
  if (x % y == 0) {
    puts("-1");
    return 0;
  }
  for (ll i = 2; x * i < LIMIT; ++i) {
    if (x * i % y != 0) {
      printf("%lld\n", x * i);
      return 0;
    }
  }
  puts("-1");
  return 0;
}
Problem p03437 (C++)

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

int main() {
  long x, y;
  std::cin >> x >> y;
  if (x == y)
    std::cout << -1 << std::endl;
  else {
    for (long long i = 0; i < 1000000000000000000; i++) {
      if (x * i % y != 0) {
        std::cout << x * i << std::endl;
        break;
      }
    }
  }
  return 0;
}


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

int main() {
  long x, y;
  std::cin >> x >> y;
  if (x == y || !(x % y))
    std::cout << -1 << std::endl;
  else {
    for (long long i = 0; i < 1000000000000000000; i++) {
      if (x * i % y != 0) {
        std::cout << x * i << std::endl;
        break;
      }
    }
  }
  return 0;
}
Problem p03437 (C++)

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

typedef long long ll;

int main() {
  ll x, y;
  std::cin >> x >> y;

  ll ans = -1;
  for (ll i = 0; i <= y; i++) {
    if ((x * i) % y != 0) {
      ans = x * i;
      break;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


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

typedef long long ll;

int main() {
  ll x, y;
  std::cin >> x >> y;

  ll ans = -1;
  for (ll i = 0; i <= y; i++) {
    if (x == y) {
      break;
    }
    if ((x * i) % y != 0) {
      ans = x * i;
      break;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
const int INF = 2147483647;
const int mod = 1e9 + 7;
char s[300005];
int vis[300005] = {0};

typedef long long ll;
ll gcd(ll a, ll b) {
  if (a < b) {
    ll temp;
    temp = a;
    a = b;
    b = temp;
  }
  while (a % b) {
    ll r = a % b;
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  ll x, y;
  scanf("%lld%lld", &x, &y);
  if (x == y || y == 1) {
    printf("-1\n");

  } else if (x < y) {
    printf("%lld\n", x);
  } else {
    long long cnt = gcd(x, y) * x - x;
    while (cnt % y == 0) {
      cnt += x;
    }
    printf("%lld\n", cnt);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
const int INF = 2147483647;
const int mod = 1e9 + 7;
char s[300005];
int vis[300005] = {0};

typedef long long ll;
ll gcd(ll a, ll b) {
  if (a < b) {
    ll temp;
    temp = a;
    a = b;
    b = temp;
  }
  while (a % b) {
    ll r = a % b;
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  ll x, y;
  scanf("%lld%lld", &x, &y);
  if (x == y || y == 1 || x % y == 0) {
    printf("-1\n");

  } else if (x < y) {
    printf("%lld\n", x);
  } else {
    long long cnt = gcd(x, y) * x - x;
    while (cnt % y == 0) {
      cnt += x;
    }
    printf("%lld\n", cnt);
  }

  return 0;
}
Problem p03437 (C++)

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

#define ll uint64_t
#define lli long long int
#define MAX 1000
#define MOD 1000000007

int main() {
  ll x, y;
  cin >> x >> y;
  if (x == y)
    cout << -1;
  else {
    for (ll i = 2; x * i < 1e18; i++) {

      if ((x * i) % y != 0) {
        cout << x * i;
        return 0;
      }
    }

    cout << -1;
  }
  return 0;
}


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

#define ll uint64_t
#define lli long long int
#define MAX 1000
#define MOD 1000000007

int main() {
  ll x, y;
  cin >> x >> y;
  if (x == y)
    cout << -1;
  else {
    for (ll i = 2; i < 100005; i++) {
      if ((x * i) % y != 0) {
        cout << x * i;
        return 0;
      }
    }

    cout << -1;
  }
  return 0;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *
 * Author: Mouhanad
 * Lang: GNU C++14
 *
 **/
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define ii pair<int, int>
#define LS pair<ll, string>
typedef long long ll;
const int inf = 1 << 30;
const int N = 100100;
int n, a[N], q;

int main() {

  ll x, y;
  cin >> x >> y;
  if (x == y) {
    printf("-1");
    return 0;
  }
  for (ll i = x; i <= 1000000000000000000; i += x) {
    if (i % y) {
      printf("%lld", i);
      return 0;
    }
  }
  printf("-1");
  return 0;
}


Fixed submission:
/**
 *
 * Author: Mouhanad
 * Lang: GNU C++14
 *
 **/
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define ii pair<int, int>
#define LS pair<ll, string>
typedef long long ll;
const int inf = 1 << 30;
const int N = 100100;
int n, a[N], q;

int main() {

  ll x, y;
  cin >> x >> y;
  if (x % y == 0) {
    printf("-1");
    return 0;
  }
  for (ll i = x; i <= 1000000000000000000; i += x) {
    if (i % y) {
      printf("%lld", i);
      return 0;
    }
  }
  printf("-1");
  return 0;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  long long int X, Y;
  cin >> X >> Y;
  if (X == Y) {
    printf("%d\n", -1);
    return 0;
  }
  if (X < Y) {
    printf("%d\n", X);
    return 0;
  }
  for (long long int i = X; i <= 10e18; i += X) {
    if (i % Y != 0) {
      printf("%lld\n", i);
      return 0;
    }
  }
  printf("%d\n", -1);
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long int X, Y;
  cin >> X >> Y;
  if (X == Y) {
    printf("%d\n", -1);
    return 0;
  }
  if (X < Y) {
    printf("%d\n", X);
    return 0;
  }
  if (X % Y == 0) {
    printf("%d\n", -1);
    return 0;
  }
  for (long long int i = X; i <= 10e18; i += X) {
    if (i % Y != 0) {
      printf("%lld\n", i);
      return 0;
    }
  }
  printf("%d\n", -1);
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll n;
  string t, T;
  cin >> n;
  ll a = 0, b = n * 3 / 2;
  cout << a << endl;
  cin >> t;
  while (t != "Vacant") {
    ll m = (a + b) / 2;
    cout << m << endl;
    cin >> T;
    if (((T != t) && (a - b) % 2 == 0) || ((T == t) && (a - b) % 2 != 0)) {
      b = a;
    }
    a = m;
    t = T;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  ll x, y;
  cin >> x >> y;
  if (x % y == 0) {
    cout << -1 << endl;
  } else
    cout << x << endl;
}