alexjercan/bugnet huggingface dataset:
Problem p01346 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef long long ll;
bool dp[40001];
ll imos[40002];
int main() {
  ll N, L, M, R;
  cin >> N >> L >> M >> R;
  L *= 2, M *= 2, R *= 2;
  dp[M] = true;
  rep1(j, 40001) imos[j + 1] = imos[j] + (dp[j] ? 1 : 0);
  rep1(i, N) {
    ll m;
    cin >> m;
    ll mn = m * R, mx = m * L / 2;
    //		show(i);
    rep(j, 2 * M + 1) {
      ll l = max(0LL, j - mx), r = j - mn;
      dp[j] = false;
      if (r >= 0)
        if (imos[r + 1] - imos[l] > 0)
          dp[j] = true;
      l = j + mn, r = min(j + mx, M * 2);
      if (l <= M * 2)
        if (imos[r + 1] - imos[l] > 0)
          dp[j] = true;
      //			cout<<"j:"<<j<<"  ";
      //			show(dp[j]);
    }
    //		cout<<endl;
    rep(j, 40001) imos[j + 1] = imos[j] + (dp[j] ? 1 : 0);
  }
  cout << (imos[2 * M] > 0 ? "Yes\\n" : "No\\n");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
bool dp[40001];
int imos[40002];
int main() {
  int N, L, M, R;
  cin >> N >> L >> M >> R;
  L *= 2, M *= 2, R *= 2;
  dp[M] = true;
  rep1(j, 40001) imos[j + 1] = imos[j] + (dp[j] ? 1 : 0);
  rep1(i, N) {
    int m;
    cin >> m;
    int mn = m * R, mx = m * L / 2;
    //		show(i);
    rep(j, 2 * M + 1) {
      int l = max(0, j - mx), r = j - mn;
      dp[j] = false;
      if (r >= 0)
        if (imos[r + 1] - imos[l] > 0)
          dp[j] = true;
      l = j + mn, r = min(j + mx, M * 2);
      if (l <= M * 2)
        if (imos[r + 1] - imos[l] > 0)
          dp[j] = true;
      //			cout<<"j:"<<j<<"  ";
      //			show(dp[j]);
    }
    //		cout<<endl;
    rep(j, 40001) imos[j + 1] = imos[j] + (dp[j] ? 1 : 0);
  }
  cout << (imos[2 * M] > 0 ? "Yes\\n" : "No\\n");
}
Problem p01349 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string t[31], s[31];
int h, w, n, used[40][40];
bool del() {
  r(i, h) r(j, w) used[i][j] = 0;
  r(i, h) {
    r(j, w - n + 1) {
      int p = 0;
      r(k, n) if (k) if (t[i][j] != t[i][j + k] || t[i][j + k] == '.') p++;
      if (!p)
        r(k, n) used[i][j + k] = 1;
    }
  }
  r(i, w) {
    r(j, h - n + 1) {
      int p = 0;
      r(k, n) if (k) if (t[j][i] != t[j + k][i] || t[j + k][i] == '.') p++;
      if (!p)
        r(k, n) used[j + k][i] = 1;
    }
  }
  int c = 0;
  r(i, h) r(j, w) if (used[i][j]) t[i][j] = '.', c++;
  if (c)
    return 0;
  return 1;
}
void fall() {
  r(k, h) r(i, h - 1) r(j, w) if (t[i + 1][j] == '.' && t[i][j] != '.')
      swap(t[i][j], t[i + 1][j]);
}
bool check() {
  r(i, h) r(j, w) if (t[i][j] != '.') return 0;
  return 1;
}
int main() {

  cin >> h >> w >> n;

  r(i, h) cin >> s[i];

  r(i, h) r(j, w - 1) {

    r(k, h) t[k] = s[k];

    swap(t[i][j], t[i][j + 1]);

    r(k, w + h) {
      fall();

      if (del())
        break;
      ;

      fall();

      if (check())
        goto L;
    }
  }
  cout << "NO" << endl;
  if (0)
  L:
    cout << "YES" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string t[31], s[31];
int h, w, n, used[40][40];
void del() {
  r(i, h) r(j, w) used[i][j] = 0;
  r(i, h) {
    r(j, w - n + 1) {
      int p = 0;
      r(k, n) if (k) if (t[i][j] != t[i][j + k] || t[i][j + k] == '.') p++;
      if (!p)
        r(k, n) used[i][j + k] = 1;
    }
  }
  r(i, w) {
    r(j, h - n + 1) {
      int p = 0;
      r(k, n) if (k) if (t[j][i] != t[j + k][i] || t[j + k][i] == '.') p++;
      if (!p)
        r(k, n) used[j + k][i] = 1;
    }
  }
  r(i, h) r(j, w) if (used[i][j]) t[i][j] = '.';
}
void fall() {
  r(k, h) r(i, h - 1) r(j, w) if (t[i + 1][j] == '.' && t[i][j] != '.')
      swap(t[i][j], t[i + 1][j]);
}
bool check() {
  r(i, h) r(j, w) if (t[i][j] != '.') return 0;
  return 1;
}
int main() {

  cin >> h >> w >> n;

  r(i, h) cin >> s[i];

  r(i, h) r(j, w - 1) {

    r(k, h) t[k] = s[k];

    swap(t[i][j], t[i][j + 1]);

    r(k, w + h) {
      fall();

      del();

      fall();

      if (check())
        goto L;
    }
  }
  cout << "NO" << endl;
  if (0)
  L:
    cout << "YES" << endl;
}
Problem p01350 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef vector<P> G;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
#define rep(i, a) for (int i = 0; i < a; i++)
double dp[20][20][10010];
int main() {
  rep(i, 20) rep(j, 20) rep(k, 10010) dp[i][j][k] = inf;
  int n;
  double x, y;
  cin >> n >> x >> y;
  y *= PI / 180;
  G g(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    g[i] = P(a, b);
  }
  double D[20][20] = {0};
  double A[20][20] = {0};
  rep(i, n) rep(j, n) A[i][j] = arg(g[i] - g[j]);
  rep(i, n) rep(j, n) D[i][j] = abs(g[i] - g[j]);
  dp[0][0][0] = 0;
  rep(i, 10010) rep(j, n) rep(k, n) if (i == 0 || j != k) {
    rep(l, n) if (l != k) {
      double dis = D[l][k]; // abs(g[k]-g[l]);
      // if(i==0)printf("%.9f %.9f\\n",x,dis+dp[j][k][l]);
      if (dis + dp[j][k][i] > x)
        continue;
      double q = A[k][j] - A[l][k]; // arg(g[k]-g[j])-arg(g[l]-g[k]);
      if (q < 0)
        q *= -1;
      while (q > PI)
        q = 2 * PI - q; // q-=PI;
      if (i == 0) {
        q = 0; // abs(arg(g[l]-g[k]));
      }
      if (q > y)
        continue;
      dp[k][l][i + 1] = min(dp[k][l][i + 1], dp[j][k][i] + dis);
    }
  }
  int out = 0;
  rep(i, n) rep(j, n) rep(k, 10010) if (abs(dp[i][j][k] - inf) > EPS) out =
      max(out, k);
  cout << out << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef vector<P> G;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
#define rep(i, a) for (int i = 0; i < a; i++)
double dp[20][20][10010];
int main() {
  rep(i, 20) rep(j, 20) rep(k, 10010) dp[i][j][k] = inf;
  int n;
  double x, y;
  cin >> n >> x >> y;
  y *= PI / 180;
  G g(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    g[i] = P(a, b);
  }
  dp[0][0][0] = 0;
  rep(i, 10010) rep(j, n) rep(k, n) if (i == 0 || j != k) {
    rep(l, n) if (l != k) {
      double dis = abs(g[k] - g[l]);
      // if(i==0)printf("%.9f %.9f\\n",x,dis+dp[j][k][l]);
      if (dis + dp[j][k][i] > x)
        continue;
      double q = arg(g[k] - g[j]) - arg(g[l] - g[k]);
      if (q < ~0)
        q *= -1;
      while (q > PI)
        q = 2 * PI - q; // q-=PI;
      if (i == 0) {
        q = 0; // abs(arg(g[l]-g[k]));
      }
      if (q > y)
        continue;
      dp[k][l][i + 1] = min(dp[k][l][i + 1], dp[j][k][i] + dis);
    }
  }
  int out = 0;
  rep(i, n) rep(j, n) rep(k, 10010) if (abs(dp[i][j][k] - inf) > EPS) out =
      max(out, k);
  cout << out << endl;
}
Problem p01350 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define EPS (1e-10)

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta *= acos(-1) / 180;
  vector<int> X(n), Y(n);
  for (int i = 0; i < n; ++i)
    cin >> X[i] >> Y[i];
  vector<vector<pair<double, int>>> G(n * n);

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (j == i)
        continue;
      for (int k = 0; k < n; ++k) {
        if (k == j)
          continue;
        int x1 = X[j] - X[i], y1 = Y[j] - Y[i], x2 = X[k] - X[j],
            y2 = Y[k] - Y[j];
        if (acos((x1 * x2 + y1 * y2) / hypot(x1, y1) / hypot(x2, y2)) <=
            theta + EPS) {
          G[i * n + j].push_back(make_pair(hypot(x2, y2), j * n + k));
        }
      }
    }
  }

  bool f = false;
  int ans = 0;
  vector<int> V;
  vector<double> dp(n * n, 0);
  for (int i = 1; i < n; ++i) {
    int x1 = X[i] - X[0], y1 = Y[i] - Y[0];
    if (hypot(x1, y1) <= r + EPS) {
      f = true;
      dp[i] = r - hypot(x1, y1);
      V.push_back(i);
    }
  }
  while (f) {
    f = false;
    ++ans;
    vector<double> dp_(n * n, 0);
    vector<int> V_;
    for (int i = 0; i < V.size(); ++i) {
      int v = V[i];
      for (int j = 0; j < G[v].size(); ++j) {
        double c = G[v][j].first;
        int v_ = G[v][j].second;
        if (c <= dp[v] + EPS) {
          f = true;
          if (dp_[v_] <= EPS)
            V_.push_back(v_);
          dp_[v_] = max(dp_[v_], dp[v] - c);
        }
      }
    }
    dp = dp_;
    V = V_;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define EPS (1e-10)

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta *= acos(-1) / 180;
  vector<int> X(n), Y(n);
  for (int i = 0; i < n; ++i)
    cin >> X[i] >> Y[i];
  vector<vector<double>> dp(n, vector<double>(n, 0));
  bool f = false;
  int ans = 0;
  vector<int> V;
  for (int i = 1; i < n; ++i) {
    int x1 = X[i] - X[0], y1 = Y[i] - Y[0];
    if (hypot(x1, y1) <= r + EPS) {
      f = true;
      dp[0][i] = r - hypot(x1, y1);
      V.push_back(i);
    }
  }
  while (f) {
    f = false;
    ++ans;
    vector<vector<double>> dp_(n, vector<double>(n, 0));
    vector<int> V_;
    for (int l = 0; l < V.size(); ++l) {
      int i = V[l] / n, j = V[l] % n;
      for (int k = 0; k < n; ++k) {
        if (j == k)
          continue;
        int x1 = X[j] - X[i], y1 = Y[j] - Y[i], x2 = X[k] - X[j],
            y2 = Y[k] - Y[j];
        if (acos((x1 * x2 + y1 * y2) / hypot(x1, y1) / hypot(x2, y2)) <=
                theta + EPS &&
            hypot(x2, y2) <= dp[i][j] + EPS) {
          f = true;
          if (dp_[j][k] <= EPS)
            V_.push_back(j * n + k);
          dp_[j][k] = max(dp_[j][k], dp[i][j] - hypot(x2, y2));
        }
      }
    }
    dp = dp_;
    V = V_;
  }
  cout << ans << endl;
  return 0;
}
Problem p01350 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <queue>

using namespace std;

typedef complex<double> P;

struct S {
  int w, p, n;
  double d;
  bool operator<(S s) const { return d > s.d; }
};

double dot(P a, P b) { return (conj(a) * b).real(); }

int main() {
  int n;
  double r, th;
  cin >> n >> r >> th;
  P p[21];
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  double t[21][21][21];
  fill(t[0][0], t[21][0], -1);
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        auto f = p[j] - p[i];
        auto to = p[k] - p[j];
        if (j != k &&
            (i == 0 ||
             acos(min(1., max(-1., dot(f, to) / abs(f) / abs(to)))) * 90 <=
                 th * acos(0))) {
          t[i][j][k] = abs(to);
        }
      }
    }
  }
  priority_queue<S> que;
  que.push({1, 0, 0, 0});
  static bool mem[21][21][22222];
  int m = 0;
  while (!que.empty()) {
    auto cs = que.top();
    if (cs.d > r)
      break;
    que.pop();
    if (mem[cs.w][cs.p][cs.n]++)
      continue;
    m = max(m, cs.n);
    for (int i = 1; i <= n; i++) {
      auto v = t[cs.p][cs.w][i];
      if (v < 0)
        continue;
      que.push({i, cs.w, cs.n + 1, cs.d + v});
    }
  }
  cout << m << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <queue>

using namespace std;

typedef complex<double> P;

struct S {
  int w, p, n;
  double d;
  bool operator<(S s) const { return d > s.d; }
};

double dot(P a, P b) { return (conj(a) * b).real(); }

int main() {
  int n;
  double r, th;
  cin >> n >> r >> th;
  P p[21];
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  priority_queue<S> que;
  que.push({1, 0, 0, 0});
  static bool mem[21][21][22222];
  int m = 0;
  while (!que.empty()) {
    auto cs = que.top();
    if (cs.d > r)
      break;
    que.pop();
    m = max(m, cs.n);
    for (int i = 1; i <= n; i++) {
      if (i == cs.w)
        continue;
      auto f = p[cs.w] - p[cs.p];
      auto t = p[i] - p[cs.w];
      if (cs.p && acos(min(1., max(-1., dot(f, t) / abs(f) / abs(t)))) * 90 >
                      th * acos(0))
        continue;
      if (mem[i][cs.w][cs.n + 1]++)
        continue;
      que.push({i, cs.w, cs.n + 1, cs.d + abs(t)});
    }
  }
  cout << m << endl;
}
Problem p01350 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;

typedef complex<double> P;
const double EPS = 1e-10;
const double INF = 1e9;
const double PI = acos((double)(-1));

int n;
double r, s;
P p[20];
bool canmove[20][20][20];
double dp[10101][20][20] = {0};

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

  cin >> n >> r >> s;
  rep(i, 0, n) {
    double x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }

  rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) {
    canmove[i][j][k] = false;
    if (i == j)
      continue;
    if (j == k)
      continue;
    if (i == k)
      continue;
    double rr = abs(arg((p[j] - p[i]) / (p[k] - p[j]))) / PI * 180.0;
    if (rr <= s - EPS)
      canmove[i][j][k] = true;
  }

  rep(i, 0, 10101) rep(j, 0, n) rep(k, 0, n) dp[i][j][k] = INF;

  rep(i, 1, n) dp[1][0][i] = abs(p[0] - p[i]);

  int ans = 0;
  rep(i, 1, 10100) rep(j, 0, n) rep(k, 0, n) {
    if (r < dp[i][j][k] + EPS)
      continue;

    // printf("dp[%d][%d][%d]=%f\\n", i, j, k, dp[i][j][k]);

    ans = max(ans, i);

    rep(l, 0, n) if (canmove[j][k][l]) {
      dp[i + 1][k][l] = min(dp[i + 1][k][l], dp[i][j][k] + abs(p[k] - p[l]));
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
typedef complex<int> P;
#define PI acos(-1)
#define EPS 1e-10

int n;
double r, s;
P xy[20];
double dp[10101][20][20];

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

  while (cin >> n) {
    cin >> r >> s;
    rep(i, 0, n) {
      double x, y;
      cin >> x >> y;
      xy[i] = P(x, y);
    }

    s = PI * s / 180.0;

    rep(i1, 0, 10101) rep(i2, 0, 20) rep(i3, 0, 20) dp[i1][i2][i3] = r;

    rep(i, 1, n) dp[1][0][i] = abs(xy[0] - xy[i]);

    int ans = 0;
    rep(i1, 1, 10100) rep(i2, 0, 20)
        rep(i3, 0, 20) if (dp[i1][i2][i3] <= r + EPS) {
      ans = max(ans, i1);

      rep(i4, 0, 20) {
        if (i4 == i3)
          continue;
        if (i4 == i2)
          continue;

        P vec32 = xy[i3] - xy[i2];
        P vec34 = xy[i4] - xy[i3];

        P vec = vec32 - vec34;
        double a = arg(vec);
        if (a < 0)
          a = arg(-vec); // 0???-?????????????¨???
        if (a < EPS)
          a = PI;

        if (a <= s)
          dp[i1 + 1][i3][i4] =
              min(dp[i1 + 1][i3][i4], dp[i1][i2][i3] + abs(xy[i3] - xy[i4]));
      }
    }
    cout << ans << endl;
  }
}
Problem p01350 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define x real()
#define y imag()
using namespace std;
typedef complex<double> P;
typedef pair<int, int> Pi;
typedef pair<double, int> Pd;
typedef pair<Pd, Pi> S;

int n;
double r, d;
P p[20];
double dp[20][20][10001], ab[20][20];
int ck[20][20][20];
bool check(int a, int b, int c) {
  if (b == c)
    return 0;
  if (a == b)
    return 1;
  P o = p[b] - p[a], s = p[c] - p[b];
  s *= conj(o);
  return real(s / abs(s)) >= cos(M_PI * d / 180);
}

int cal() {
  priority_queue<S, vector<S>, greater<S>> Q;
  int ans = 0;
  for (int i = 0; i <= r; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        dp[j][k][i] = 1e9;
  dp[0][0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        ck[i][j][k] = check(i, j, k);
      ab[i][j] = abs(p[i] - p[j]);
    }
  Q.push(S(Pd(0, 0), Pi(0, 0)));
  while (!Q.empty()) {
    double cs = Q.top().first.first;
    int i = Q.top().first.second;
    int j = Q.top().second.first;
    int k = Q.top().second.second;
    Q.pop();
    if (dp[j][k][i] < cs)
      continue;
    if (cs > r)
      return ans;
    for (int l = 0; l < n; l++) {
      double ncs = cs + ab[k][l];
      if (ck[j][k][l] && ncs <= r) {
        if (dp[k][l][i + 1] > ncs) {
          Q.push(S(Pd(cs + ab[k][l], i + 1), Pi(k, l)));
          dp[k][l][i + 1] = ncs;
        }
      }
    }
    ans = max(ans, i);
  }
  return ans;
}

int main() {
  cin >> n >> r >> d;
  double a, b;
  for (int i = 0; i < n; i++)
    cin >> a >> b, p[i] = P(a, b);
  cout << cal() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define x real()
#define y imag()
using namespace std;
typedef complex<double> P;
typedef pair<int, int> Pi;
typedef pair<double, int> Pd;
typedef pair<Pd, Pi> S;

int n;
double r, d;
P p[20];
double dp[20][20][10001];

bool check(int a, int b, int c) {
  if (b == c)
    return 0;
  if (a == b)
    return 1;
  P o = p[b] - p[a], s = p[c] - p[b];
  s *= conj(o);
  return real(s / abs(s)) >= cos(M_PI * d / 180);
}

int cal() {
  priority_queue<S, vector<S>, greater<S>> Q;
  int ans = 0;
  for (int i = 0; i <= r; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        dp[j][k][i] = 1e9;
  dp[0][0][0] = 0;
  Q.push(S(Pd(0, 0), Pi(0, 0)));
  while (!Q.empty()) {
    double cs = Q.top().first.first;
    int i = Q.top().first.second;
    int j = Q.top().second.first;
    int k = Q.top().second.second;
    Q.pop();
    if (dp[j][k][i] < cs)
      continue;
    if (cs > r)
      return ans;
    for (int l = 0; l < n; l++) {
      double ncs = cs + abs(p[k] - p[l]);
      if (check(j, k, l) && ncs <= r) {
        if (dp[k][l][i + 1] > ncs) {
          Q.push(S(Pd(cs + abs(p[k] - p[l]), i + 1), Pi(k, l)));
          dp[k][l][i + 1] = ncs;
        }
      }
    }
    ans = max(ans, i);
  }
  return ans;
}

int main() {
  cin >> n >> r >> d;
  double a, b;
  for (int i = 0; i < n; i++)
    cin >> a >> b, p[i] = P(a, b);
  cout << cal() << endl;
  return 0;
}
Problem p01351 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <set>
#include <stdio.h>
using namespace std;
int gcd(int a, int b) {
  while (a) {
    b %= a;
    int c = a;
    a = b;
    b = c;
  }
  return b;
}
set<pair<int, int>> S;
set<pair<int, int>> T;
int main() {
  int a, b, c, d, e, f;
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  int row = 0;
  int col = 0;
  long long cnt = 1;
  S.insert(make_pair(row, col));
  while (1) {
    row = (row + c) % a;
    col = (col + d) % b;
    if (col == 0)
      break;
    cnt++;
    S.insert(make_pair(row, col));
  }
  int g = gcd(row, a);
  long long ret = a / g * cnt;
  if (ret < 110000) {
    S.clear();
    swap(c, e);
    swap(d, f);
    row = 0;
    col = 0;
    cnt = 1;
    S.insert(make_pair(row, col));
    while (1) {
      row = (row + c) % a;
      col = (col + d) % b;
      if (col == 0)
        break;
      cnt++;
      S.insert(make_pair(row, col));
    }
    g = gcd(row, a);
    ret = a / g * cnt;
  }
  for (set<pair<int, int>>::iterator it = S.begin(); it != S.end(); it++) {
    pair<int, int> tmp = (*it);
    T.insert(make_pair(tmp.first % g, tmp.second));
  }
  // printf("%lld\\n",ret);
  row = 0;
  col = 0;
  cnt = 1;
  while (1) {
    row = (row + e) % a;
    col = (col + f) % b;
    if (T.count(make_pair(row % g, col)))
      break;
    cnt++;
  }
  // printf("%lld %lld\\n",ret,cnt);
  printf("%lld\\n", ret * cnt - 1);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <set>
#include <stdio.h>
using namespace std;
int gcd(int a, int b) {
  while (a) {
    b %= a;
    int c = a;
    a = b;
    b = c;
  }
  return b;
}
set<pair<int, int>> S;
set<pair<int, int>> T;
int main() {
  int a, b, c, d, e, f;
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  int row = 0;
  int col = 0;
  int cnt = 1;
  S.insert(make_pair(row, col));
  while (1) {
    row = (row + c) % a;
    col = (col + d) % b;
    if (col == 0)
      break;
    cnt++;
    S.insert(make_pair(row, col));
  }
  int g = gcd(row, a);
  long long ret = a / g * cnt;
  for (set<pair<int, int>>::iterator it = S.begin(); it != S.end(); it++) {
    pair<int, int> tmp = (*it);
    T.insert(make_pair(tmp.first % g, tmp.second));
  }
  row = 0;
  col = 0;
  cnt = 1;
  while (1) {
    row = (row + e) % a;
    col = (col + f) % b;
    if (T.count(make_pair(row % g, col)))
      break;
    cnt++;
  }
  // printf("%lld %lld\\n",ret,cnt);
  printf("%lld\\n", ret * cnt - 1);
}
Problem p01351 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <numeric>
#include <queue>
#include <set>

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

typedef long long ll;

using namespace std;

int check(int a, int m) {
  vector<int> visit(m);
  int now = 0;

  while (!visit[now]) {
    visit[now] = 1;
    now = (now + a) % m;
  }

  return accumulate(visit.begin(), visit.end(), 0);
}

int main() {
  int m = getInt();
  int n = getInt();
  int a = getInt();
  int b = getInt();
  int c = getInt();
  int d = getInt();

  a %= m;
  b %= n;
  c %= m;
  d %= n;

  int acnt = check(a, m);
  int bcnt = check(b, n);

  ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
  ll ans2 = 0;

  vector<int> h(m, -1);
  int hh = 0;
  int xx = 0;
  int yy = 0;

  while (h[xx] == -1) {
    h[xx] = yy;
    xx = (xx + a) % m;
    yy = (yy + b) % n;
    hh++;
  }

  int ym = ans1 / hh;
  int yd = n / ym;

  xx = c;
  yy = d;

  while (true) {
    ans2++;

    if (ans2 == 1000 * n) {
      int ccnt = check(c, m);
      int dcnt = check(d, n);
      ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);
      break;
    }

    if (h[xx] != -1) {
      if ((yy - h[xx] + n) % yd == 0)
        break;
    }

    xx = (xx + c) % m;
    yy = (yy + d) % n;
  }

  printf("%lld\\n", ans1 * ans2 - 1);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <numeric>
#include <queue>
#include <set>

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

typedef long long ll;

using namespace std;

int check(int a, int m) {
  vector<int> visit(m);
  int now = 0;

  while (!visit[now]) {
    visit[now] = 1;
    now = (now + a) % m;
  }

  return accumulate(visit.begin(), visit.end(), 0);
}

int main() {
  int m = getInt();
  int n = getInt();
  int a = getInt();
  int b = getInt();
  int c = getInt();
  int d = getInt();

  a %= m;
  b %= n;
  c %= m;
  d %= n;

  int acnt = check(a, m);
  int bcnt = check(b, n);

  ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
  ll ans2 = 0;

  vector<int> h(m, -1);
  int hh = 0;
  int xx = 0;
  int yy = 0;

  while (h[xx] == -1) {
    h[xx] = yy;
    xx = (xx + a) % m;
    yy = (yy + b) % n;
    hh++;
  }

  int ym = ans1 / hh;
  int yd = n / ym;

  xx = c;
  yy = d;

  while (true) {
    ans2++;

    if (h[xx] != -1) {
      if ((yy - h[xx] + n) % yd == 0)
        break;
    }

    xx = (xx + c) % m;
    yy = (yy + d) % n;
  }

  printf("%lld\\n", ans1 * ans2 - 1);

  return 0;
}