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

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

int N;
vector<int> s[200];
int main() {
  int k;

  while (cin >> N) {
    k = 1;
    while ((k * k - k) < 2 * N)
      k++;
    if ((k * k - k) == 2 * N) {
      for (int i = 0; i < k; i++)
        s[i].clear();
      cout << "Yes" << endl;
      cout << k << endl;
      int num = 1;
      for (int i = 0; i < k; i++) {
        for (int j = i + 1; j < k; j++) {
          s[i].push_back(num);
          s[j].push_back(num);
          num++;
        }
      }
      int l = s[0].size();
      for (int i = 0; i < k; i++) {
        cout << k - 1;
        for (int j = 0; j < l; j++)
          cout << ' ' << s[i][j];
        cout << endl;
      }
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

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

int N;
vector<int> s[1000];
int main() {
  int k;

  while (cin >> N) {
    k = 1;
    while ((k * k - k) < 2 * N)
      k++;
    if ((k * k - k) == 2 * N) {
      for (int i = 0; i < k; i++)
        s[i].clear();
      cout << "Yes" << endl;
      cout << k << endl;
      int num = 1;
      for (int i = 0; i < k; i++) {
        for (int j = i + 1; j < k; j++) {
          s[i].push_back(num);
          s[j].push_back(num);
          num++;
        }
      }
      int l = s[0].size();
      for (int i = 0; i < k; i++) {
        cout << k - 1;
        for (int j = 0; j < l; j++)
          cout << ' ' << s[i][j];
        cout << endl;
      }
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p03230 (C++)

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, A, n) for (int(i) = (A); (i) < (n); (i)++)
#define pb emplace_back
using LL = long long;

const LL MOD = 1e9 + 7;

int N;
int K;
vector<int> S[114514];

int main() {
  scanf("%d", &N);
  reps(k, 1, N + 1) {
    if (k * (k - 1) > 2 * N) {
      puts("No");
      return 0;
    }

    if (k * (k - 1) == 2 * N) {
      K = k;
      break;
    }
  }

  int cnt = 0;
  rep(i, K) {
    reps(j, i + 1, K) {
      ++cnt;
      S[i].pb(cnt);
      S[j].pb(cnt);
    }
  }
  assert(cnt == N);

  printf("Yes\n%d\n", K);
  rep(i, K) {
    printf("%d", (int)S[i].size());
    for (int v : S[i]) {
      printf(" %d", v);
    }
    puts("");
  }
}


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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, A, n) for (int(i) = (A); (i) < (n); (i)++)
#define pb emplace_back
using LL = long long;

const LL MOD = 1e9 + 7;

int N;
int K;
vector<int> S[114514];

int main() {
  scanf("%d", &N);
  reps(k, 1, N + 11) {
    if (k * (k - 1) > 2 * N) {
      puts("No");
      return 0;
    }

    if (k * (k - 1) == 2 * N) {
      K = k;
      break;
    }
  }

  int cnt = 0;
  rep(i, K) {
    reps(j, i + 1, K) {
      ++cnt;
      S[i].pb(cnt);
      S[j].pb(cnt);
    }
  }
  assert(cnt == N);

  printf("Yes\n%d\n", K);
  rep(i, K) {
    printf("%d", (int)S[i].size());
    for (int v : S[i]) {
      printf(" %d", v);
    }
    puts("");
  }
}
Problem p03230 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A = 1;
  while (A * (A - 1) / 2 < N)
    ++A;
  if (A * (A - 1) / 2 > N) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    vector<vector<int>> G(N);
    int cnt = 0;
    for (int i = 0; i < A; ++i) {
      for (int j = 0; j < i; ++j) {
        ++cnt;
        G[i].push_back(cnt);
        G[j].push_back(cnt);
      }
    }
    cout << A << endl;
    for (int i = 0; i < A; ++i) {
      cout << G[i].size();
      for (int j : G[i]) {
        cout << ' ' << j;
      }
      cout << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A = 1;
  while (A * (A - 1) / 2 < N)
    ++A;
  if (A * (A - 1) / 2 > N) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    vector<vector<int>> G(A);
    int cnt = 0;
    for (int i = 0; i < A; ++i) {
      for (int j = 0; j < i; ++j) {
        ++cnt;
        G[i].push_back(cnt);
        G[j].push_back(cnt);
      }
    }
    cout << A << endl;
    for (int i = 0; i < A; ++i) {
      cout << G[i].size();
      for (int j : G[i]) {
        cout << ' ' << j;
      }
      cout << endl;
    }
  }
  return 0;
}
Problem p03230 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;

int main() {
  int N;

  cin >> N;
  if (N == 1) {
    cout << "Yes" << endl;
    cout << 2 << endl;
    cout << "1 1" << endl;
    cout << "1 1" << endl;
    return 0;
  }

  int x = 0;
  int k = 0;
  vector<int> X;
  for (int i = 1; i < N; ++i) {
    x += i;
    X.push_back(x);
    if (x == N) {
      k = i;
      break;
    }
    if (x > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  cout << k + 1 << endl;

  vector<vector<int>> R;
  for (int i = 0; i < k + 1; ++i) {
    vector<int> v;
    R.push_back(v);
  }

  R[0].push_back(1);
  R[1].push_back(1);
  for (int i = 0; i < X.size() - 1; ++i) {
    int x = X[i];
    int x2 = X[i + 1];
    for (int j = x + 1; j <= x2; ++j) {
      R[j - (x + 1)].push_back(j);
      R[2 + i].push_back(j);
    }
  }

  for (int i = 0; i < k + 1; ++i) {
    cout << k;
    for (int j = 0; j < k; ++j) {
      cout << " " << R[i][j];
    }
    cout << endl;
  }

  return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;

int main() {
  int N;

  cin >> N;
  if (N == 1) {
    cout << "Yes" << endl;
    cout << 2 << endl;
    cout << "1 1" << endl;
    cout << "1 1" << endl;
    return 0;
  }

  int x = 0;
  int k = 0;
  vector<int> X;
  for (int i = 1; i <= N; ++i) {
    x += i;
    X.push_back(x);
    if (x == N) {
      k = i;
      break;
    }
    if (x > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  cout << k + 1 << endl;

  vector<vector<int>> R;
  for (int i = 0; i < k + 1; ++i) {
    vector<int> v;
    R.push_back(v);
  }

  R[0].push_back(1);
  R[1].push_back(1);
  for (int i = 0; i < X.size() - 1; ++i) {
    int x = X[i];
    int x2 = X[i + 1];
    for (int j = x + 1; j <= x2; ++j) {
      R[j - (x + 1)].push_back(j);
      R[2 + i].push_back(j);
    }
  }

  for (int i = 0; i < k + 1; ++i) {
    cout << k;
    for (int j = 0; j < k; ++j) {
      cout << " " << R[i][j];
    }
    cout << endl;
  }

  return 0;
}
Problem p03230 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ld long double
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }              // 最小公倍数
using Graph = vector<vector<ll>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const ll MOD = 1000000007;
int main() {
  int n;
  cin >> n;
  int u = 1;
  while (u * (u - 1) / 2 < n) {
    u++;
  }
  if (u * (u - 1) / 2 != n) {
    cout << "No" << endl;
    return 0;
  }
  Graph ans(u);
  int l = 0, r = 1;
  rep(i, n) {
    ans.at(l).push_back(i + 1);
    ans.at(r).push_back(i + 1);
    if (r != n - 1)
      r++;
    else {
      l++;
      r = l + 1;
    }
  }
  cout << "Yes" << endl;
  cout << u << endl;
  rep(i, u) {
    cout << u - 1 << " ";
    rep(j, u - 2) cout << ans.at(i).at(j) << " ";
    cout << ans.at(i).at(u - 2) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ld long double
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }              // 最小公倍数
using Graph = vector<vector<ll>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const ll MOD = 1000000007;
int main() {
  int n;
  cin >> n;
  int u = 1;
  while (u * (u - 1) / 2 < n) {
    u++;
  }
  if (u * (u - 1) / 2 != n) {
    cout << "No" << endl;
    return 0;
  }
  Graph ans(u);
  int l = 0, r = 1;
  rep(i, n) {
    ans.at(l).push_back(i + 1);
    ans.at(r).push_back(i + 1);
    if (r != u - 1)
      r++;
    else {
      l++;
      r = l + 1;
    }
  }
  cout << "Yes" << endl;
  cout << u << endl;
  rep(i, u) {
    cout << u - 1 << " ";
    rep(j, u - 2) cout << ans.at(i).at(j) << " ";
    cout << ans.at(i).at(u - 2) << endl;
  }
}
Problem p03230 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;
typedef long double lb;
#define int long long
#define double long double
typedef pair<int, int> P;
const int MOD = 1000000007;
const ll INF = 1e16;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const pair<int, int> fd[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                             make_pair(0, -1)};

////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////

int n;
vector<vector<int>> ans;
signed main() {
  cin >> n;
  int k = -1;
  repd(i, 2, n + 1) {
    if (i * (i - 1) / 2 == n) {
      k = i;
      break;
    } else if (i * (i - 1) / 2 > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  ans.resize(k);
  int t = 1;
  rep(i, k) {
    repd(j, i + 1, k) {
      ans[i].push_back(t);
      ans[j].push_back(t);
      t++;
    }
  }
  cout << "Yes" << endl << k << endl;
  rep(i, k) {
    cout << ans[i].size() << " ";
    rep(j, ans[i].size()) cout << ans[i][j]
                               << (j == ans[i].size() - 1 ? "\n" : " ");
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;
typedef long double lb;
#define int long long
#define double long double
typedef pair<int, int> P;
const int MOD = 1000000007;
const ll INF = 1e16;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const pair<int, int> fd[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                             make_pair(0, -1)};

////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////

int n;
vector<vector<int>> ans;
signed main() {
  cin >> n;
  int k = -1;
  repd(i, 2, n + 3) {
    if (i * (i - 1) / 2 == n) {
      k = i;
      break;
    } else if (i * (i - 1) / 2 > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  ans.resize(k);
  int t = 1;
  rep(i, k) {
    repd(j, i + 1, k) {
      ans[i].push_back(t);
      ans[j].push_back(t);
      t++;
    }
  }
  cout << "Yes" << endl << k << endl;
  rep(i, k) {
    cout << ans[i].size() << " ";
    rep(j, ans[i].size()) cout << ans[i][j]
                               << (j == ans[i].size() - 1 ? "\n" : " ");
  }
}
Problem p03230 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define mp make_pair
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 30)
#define LLINF (1 << 60)
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

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

  int n;
  cin >> n;
  int i = 1;
  while (1) {
    if (i * (i + 1) == n * 2)
      break;
    if (i * (i + 1) > n * 2) {
      cout << "No" << endl;
      return 0;
    }
    i++;
  }
  cout << "Yes" << endl << (i + 1) << endl;
  int num = 1;
  int a[100][100] = {};
  int l = i;
  for (int j = 0; j < i; j++) {
    for (int k = 0; k < l; k++) {
      a[j][j + k] = num;
      a[j + 1 + k][j] = num;
      num++;
    }
    l--;
  }
  for (int j = 0; j <= i; j++) {
    cout << i << ' ';
    for (int k = 0; k < i; k++)
      cout << a[j][k] << ' ';
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define mp make_pair
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 30)
#define LLINF (1 << 60)
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

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

  int n;
  cin >> n;
  int i = 1;
  while (1) {
    if (i * (i + 1) == n * 2)
      break;
    if (i * (i + 1) > n * 2) {
      cout << "No" << endl;
      return 0;
    }
    i++;
  }
  cout << "Yes" << endl << (i + 1) << endl;
  int num = 1;
  int a[1000][1000] = {};
  int l = i;
  for (int j = 0; j < i; j++) {
    for (int k = 0; k < l; k++) {
      a[j][j + k] = num;
      a[j + 1 + k][j] = num;
      num++;
    }
    l--;
  }
  for (int j = 0; j <= i; j++) {
    cout << i << ' ';
    for (int k = 0; k < i; k++)
      cout << a[j][k] << ' ';
    cout << endl;
  }
  return 0;
}
Problem p03230 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int f = -1;
  for (int i = 1; i < 1000; i++) {
    if (n == i * (i + 1) / 2) {
      f = i;
    }
  }
  if (f == -1) {
    cout << "No" << endl;
    return 0;
  } else {
    cout << "Yes" << endl;
    cout << f + 1 << endl;
    int cnt = 0;
    vector<vector<int>> c(n, vector<int>(n));
    int sum = 0, s = 0;
    for (int i = 1; i <= n; i++) {
      c[cnt][i - sum - 1] = i;
      s++;
      if ((cnt + 1) * (cnt + 2) / 2 == i) {
        cnt++;
        sum = s;
      }
    }
    for (int i = 0; i < f; i++) {
      cout << f << " ";
      for (int j = 0; j <= i; j++) {
        cout << c[i][j] << " ";
      }
      for (int j = i + 1; j < f; j++) {
        cout << c[j][i] << " ";
      }
      cout << endl;
    }
    cout << f << " ";
    for (int i = 0; i < f; i++) {
      cout << c[i][i] << " ";
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int f = -1;
  for (int i = 1; i < 1000; i++) {
    if (n == i * (i + 1) / 2) {
      f = i;
    }
  }
  if (f == -1) {
    cout << "No" << endl;
    return 0;
  } else {
    cout << "Yes" << endl;
    cout << f + 1 << endl;
    int cnt = 0;
    vector<vector<int>> c(f, vector<int>(f));
    int sum = 0, s = 0;
    for (int i = 1; i <= n; i++) {
      c[cnt][i - sum - 1] = i;
      s++;
      if ((cnt + 1) * (cnt + 2) / 2 == i) {
        cnt++;
        sum = s;
      }
    }
    for (int i = 0; i < f; i++) {
      cout << f << " ";
      for (int j = 0; j <= i; j++) {
        cout << c[i][j] << " ";
      }
      for (int j = i + 1; j < f; j++) {
        cout << c[j][i] << " ";
      }
      cout << endl;
    }
    cout << f << " ";
    for (int i = 0; i < f; i++) {
      cout << c[i][i] << " ";
    }
    cout << endl;
  }
}
Problem p03230 (C++)

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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int n, now, ans[400][400];

signed main() {
  cin >> n;
  int ns = (int)sqrt(n * 2);
  if (n * 2 != ns * (ns + 1)) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  cout << ns + 1 << endl;
  rep(i, ns + 1) {
    rep(j, i) ans[i][j] = ans[j][i - 1];
    repo(j, i, ns) ans[i][j] = ++now;
  }
  rep(i, ns + 1) {
    cout << ns << " ";
    rep(j, ns) cout << ans[i][j] << " ";
    cout << endl;
  }
}


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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int n, now, ans[1000][1000];

signed main() {
  cin >> n;
  int ns = (int)sqrt(n * 2);
  if (n * 2 != ns * (ns + 1)) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  cout << ns + 1 << endl;
  rep(i, ns + 1) {
    rep(j, i) ans[i][j] = ans[j][i - 1];
    repo(j, i, ns) ans[i][j] = ++now;
  }
  rep(i, ns + 1) {
    cout << ns << " ";
    rep(j, ns) cout << ans[i][j] << " ";
    cout << endl;
  }
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  int64_t g = gcd(N, M);
  int64_t L = N * M / g;
  // cout << L << endl;
  if (g == 1) {
    if (S.at(0) == T.at(0))
      cout << L << endl;
    else
      cout << -1 << endl;
  } else {
    bool OK = true;
    for (int i = 0; i < L; i += L / g) {
      if (S.at(i / (L / N)) != T.at(i / (L / M)))
        OK = false;
    }
    if (OK) {
      cout << L << endl;
    } else
      cout << -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 REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  int64_t g = gcd(N, M);
  int64_t L = N / g * M;
  // cout << L << endl;
  if (g == 1) {
    if (S.at(0) == T.at(0))
      cout << L << endl;
    else
      cout << -1 << endl;
  } else {
    bool OK = true;
    for (int i = 0; i < L; i += L / g) {
      if (S.at(i / (L / N)) != T.at(i / (L / M)))
        OK = false;
    }
    if (OK) {
      cout << L << endl;
    } else
      cout << -1 << endl;
  }
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
char s[maxn + 10];
char t[maxn + 10];
int n, m;
int main() {
  while (cin >> n >> m) {
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    ll lcm = 1ll * n * m / __gcd(n, m);
    ll nn = lcm / n;
    ll mm = lcm / m;
    ll sum = 1;
    bool flag = true;
    for (int i = 1; i <= min(nn, mm); i++) {
      if (s[(sum - 1) / nn + 1] != t[(sum - 1) / mm + 1]) {
        flag = false;
        break;
      }
      sum += nn * mm;
    }
    if (!flag)
      puts("-1");
    else
      printf("%lld\n", lcm);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
char s[maxn + 10];
char t[maxn + 10];
int n, m;
int main() {
  while (cin >> n >> m) {
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    ll lcm = 1ll * n * m / __gcd(n, m);
    ll nn = lcm / n;
    ll mm = lcm / m;
    ll sum = 1;
    bool flag = true;
    for (int i = 1;; i++) {
      if ((sum - 1) / nn + 1 > n || (sum - 1) / mm + 1 > m)
        break;
      if (s[(sum - 1) / nn + 1] != t[(sum - 1) / mm + 1]) {
        flag = false;
        break;
      }
      sum += nn * mm;
    }
    if (!flag)
      puts("-1");
    else
      printf("%lld\n", lcm);
  }
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (long long i = 0; i < (ll)(n); i++)
using namespace std;
ll gcd(ll a, ll b) {
  if (a < b) {
    ll t = a;
    a = b;
    b = t;
  }
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return ((ll)(a * b)) / gcd(a, b); }
int main() {
  // cout<<lcm(10000,100001)<<endl;
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll ans = lcm(n, m);

  ll N = ans / n;
  ll M = ans / m;
  rep(i, n) rep(j, m) {
    if (i * N < j * M)
      break;
    if (i * N == j * M) {
      // cout<<s[i]<<","<<t[j]<<endl;
      if (s[i] != t[j]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (long long i = 0; i < (ll)(n); i++)
using namespace std;
ll gcd(ll a, ll b) {
  if (a < b) {
    ll t = a;
    a = b;
    b = t;
  }
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return ((ll)(a * b)) / gcd(a, b); }
int main() {
  // cout<<lcm(10000,100001)<<endl;
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll ans = lcm(n, m);

  ll N = ans / n;
  ll M = ans / m;
  rep(i, n) {
    if ((i * N) % M != 0)
      continue;
    ll j = (i * N) / M;
    // cout<<s[i]<<","<<t[j]<<endl;
    if (s[i] != t[j]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  bool OK = true;
  ll a = gcd(n, m);
  ll l = a * n / a * m / a;
  string c = "";
  rep(i, l + 1) { c.pb('*'); }
  rep(i, n) { c[i * l / n + 1] = s[i]; }
  rep(i, m) {
    if (c[i * l / m + 1] != '*' && t[i] != c[i * l / m + 1]) {
      OK = false;
    }
  }
  cout << (OK ? l : -1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  bool OK = true;
  ll a = gcd(n, m);
  ll l = a * n / a * m / a;
  ll x = l / n, y = l / m;
  rep(i, n) {
    if (i * x / y > sz(t)) {
      break;
    }
    if ((i * x) % y == 0 && s[i] != t[i * x / y]) {
      OK = false;
    }
  }
  cout << (OK ? l : -1) << endl;
}
Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
template <typename F, typename S>
std::ostream &operator<<(ostream &os, const pair<F, S> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (const T &a : v) {
    os << a << ", ";
  }
  os << "]";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &m) {
  os << "{";
  for (const auto &p : m) {
    os << p.first << ":" << p.second << ", ";
  }
  os << "]";
  return os;
}

LL gcd(LL n, LL m) {
  if (n < m)
    swap(n, m);
  if (m == 0)
    return n;
  return gcd(m, n % m);
}

bool check(string &s, string &t, LL c) {
  LL a = c / s.size();
  LL b = c / t.size();
  for (LL i = 0; i < c; ++i) {
    if (i % a == 0 && i % b == 0) {
      if (s[i / a] != t[i / b])
        return false;
    }
  }
  return true;
}

int main() {
  LL n, m;
  string s, t;
  cin >> n >> m;
  cin >> s >> t;
  LL c = n * m / gcd(n, m);
  cerr << "c=" << c << endl;
  if (check(s, t, c)) {
    cout << c << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
template <typename F, typename S>
std::ostream &operator<<(ostream &os, const pair<F, S> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (const T &a : v) {
    os << a << ", ";
  }
  os << "]";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &m) {
  os << "{";
  for (const auto &p : m) {
    os << p.first << ":" << p.second << ", ";
  }
  os << "]";
  return os;
}

LL gcd(LL n, LL m) {
  if (n < m)
    swap(n, m);
  if (m == 0)
    return n;
  return gcd(m, n % m);
}

bool check(string &s, string &t, LL c) {
  LL a = c / s.size();
  LL b = c / t.size();
  for (LL i = 0; i < c; i += a) {
    if (i % b != 0)
      continue;
    if (s[i / a] != t[i / b])
      return false;
  }
  return true;
}

int main() {
  LL n, m;
  string s, t;
  cin >> n >> m;
  cin >> s >> t;
  LL c = n * m / gcd(n, m);
  cerr << "c=" << c << endl;
  if (check(s, t, c)) {
    cout << c << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
Problem p03231 (C++)

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

using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < (n); i++)
#define CLR(mat, f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a <= x && x < b)
#define out(ans) cout << ans << endl
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const int INF = 1000000;
const double PI = 3.14159265359;

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

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

int main() {

  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll l = lcm(n, m);

  string k = "";
  rep(i, 0, l) k += "1";

  rep(i, 0, n) { k[i * l / n] = s[i]; }
  rep(i, 0, m) {
    if (k[i * l / m] == '1' || k[i * l / m] == t[i])
      ;
    else {
      out(-1);
      return 0;
    }
  }
  out(l);

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < (n); i++)
#define CLR(mat, f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a <= x && x < b)
#define out(ans) cout << ans << endl
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const int INF = 1000000;
const double PI = 3.14159265359;

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

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

int main() {

  ll n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll l = lcm(n, m);

  ll a = l / n, b = l / m;
  for (ll i = 0, j = 0; i < n && j < m;) {
    if (i * a > j * b)
      j++;
    else if (i * a == j * b) {
      if (s[i] != t[j]) {
        out(-1);
        return 0;
      }
      i++;
      j++;
    } else
      i++;
  }
  out(l);

  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  long int N, M, k, l;
  cin >> N >> M;
  char s[N], t[M];
  long int LCM, gameover = 0;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> t[i];
  }
  if (M >= N) {
    k = M;
    l = N;
  } else {
    k = N;
    l = M;
  }
  while (k != 0 && l != 0) {
    if (k >= l) {
      k = k % l;
    } else {
      l = l % k;
    }
  }
  if (k == 0) {
    LCM = l;
  } else {
    LCM = k;
  }
  if (M == LCM && N == LCM) {
    for (int i = 0; i < M; i++) {
      if (s[i] != t[i]) {
        gameover = 1;
        break;
      }
    }
  } else if (M == LCM) {
    for (int i = 0; i < M; i++) {
      if (s[i * (N / M)] != t[i]) {
        gameover = 1;
        break;
      }
    }
  } else if (N == LCM) {
    for (int i = 0; i < N; i++) {
      if (s[i] != t[i * (M / N)]) {
        gameover = 1;
        break;
      }
    }

  } else {
    int N2 = N / LCM;
    int M2 = M / LCM;
    for (int i = 0; i * N2 * M2 < min(N, M); ++i) {
      if (s[i * N2] != t[i * M2]) {
        gameover = 1;
      }
    }
    // if(s[0]!=t[0]){
    //   gameover=1;
    // }
  }
  if (gameover == 1) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << M * N / LCM << std::endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long int N, M, k, l;
  cin >> N >> M;
  char s[N], t[M];
  long int LCM, gameover = 0;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> t[i];
  }
  if (M >= N) {
    k = M;
    l = N;
  } else {
    k = N;
    l = M;
  }
  while (k != 0 && l != 0) {
    if (k >= l) {
      k = k % l;
    } else {
      l = l % k;
    }
  }
  if (k == 0) {
    LCM = l;
  } else {
    LCM = k;
  }
  if (M == LCM && N == LCM) {
    for (int i = 0; i < M; i++) {
      if (s[i] != t[i]) {
        gameover = 1;
        break;
      }
    }
  } else if (M == LCM) {
    for (int i = 0; i < M; i++) {
      if (s[i * (N / M)] != t[i]) {
        gameover = 1;
        break;
      }
    }
  } else if (N == LCM) {
    for (int i = 0; i < N; i++) {
      if (s[i] != t[i * (M / N)]) {
        gameover = 1;
        break;
      }
    }

  } else {
    int N2 = N / LCM;
    int M2 = M / LCM;
    for (int i = 0; i * N2 < N && i * M2 < M; ++i) {
      if (s[i * N2] != t[i * M2]) {
        gameover = 1;
      }
    }
    // if(s[0]!=t[0]){
    //   gameover=1;
    // }
  }
  if (gameover == 1) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << M * N / LCM << std::endl;
  }
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll lcm = N / __gcd(N, M) * M;
  ll g = __gcd(N, M);

  /*vector<char>s(lcm,' ');
  vector<char>t(lcm,' ');
  ll cnts=0,cntt=0;
  rep(i,lcm){
      if(i%(lcm/N)==0&&cnts<=N-1){
          s[i]=S[cnts];
          cnts++;
      }
      if(i%(lcm/M)==0&&cntt<=M-1){
          t[i]=T[cntt];
          cntt++;
      }
       if(s[i]!=' '&&t[i]!=' '){
          if(s[i]!=t[i]){
              cout<<-1<<endl;
              return 0;
          }
      }
      if(cnts>=N&&cntt>=M)break;

  }
  cout<<lcm<<endl;*/
  for (int i = 0, j = 0; i < M; i += N / g, j += M / g) {
    if (S[i] != T[j]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lcm << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long ll;
const int mod = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll lcm = N / __gcd(N, M) * M;
  ll g = __gcd(N, M);

  /*vector<char>s(lcm,' ');
  vector<char>t(lcm,' ');
  ll cnts=0,cntt=0;
  rep(i,lcm){
      if(i%(lcm/N)==0&&cnts<=N-1){
          s[i]=S[cnts];
          cnts++;
      }
      if(i%(lcm/M)==0&&cntt<=M-1){
          t[i]=T[cntt];
          cntt++;
      }
       if(s[i]!=' '&&t[i]!=' '){
          if(s[i]!=t[i]){
              cout<<-1<<endl;
              return 0;
          }
      }
      if(cnts>=N&&cntt>=M)break;

  }
  cout<<lcm<<endl;*/
  for (int i = 0, j = 0; i < N; i += N / g, j += M / g) {
    if (S[i] != T[j]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lcm << endl;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <stdio.h>
#define ll long long
using namespace std;
const int N = 200100, P = 998244353;
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, m, A, B;
ll ans;
char a[N], b[N];
int main() {
  int i, j, ia, ib;
  n = read();
  m = read();
  scanf("%s", a + 1);
  scanf("%s", b + 1);
  ans = 1ll * n / __gcd(n, m) * m;
  A = ans / n;
  B = ans / m;
  i = j = 1;
  ia = ib = 1;
  while (i <= ans && j <= ans) {
    if (i == j)
      if (a[ia] != b[ib])
        goto fuck;
    if (i <= j)
      ia++, i += A;
    else
      ib++, j += B;
  }
  write(ans);
  puts("");
  return 0;
fuck:
  puts("-1");
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <stdio.h>
#define ll long long
using namespace std;
const int N = 200100, P = 998244353;
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, m, A, B;
ll ans;
char a[N], b[N];
int main() {
  ll i, j, ia, ib;
  n = read();
  m = read();
  scanf("%s", a + 1);
  scanf("%s", b + 1);
  ans = 1ll * n / __gcd(n, m) * m;
  A = ans / n;
  B = ans / m;
  i = j = 1;
  ia = ib = 1;
  while (i <= ans && j <= ans) {
    if (i == j)
      if (a[ia] != b[ib])
        goto fuck;
    if (i <= j)
      ia++, i += A;
    else
      ib++, j += B;
  }
  write(ans);
  puts("");
  return 0;
fuck:
  puts("-1");
}
Problem p03231 (C++)

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

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for (int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for (int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }

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

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll L = lcm(n, m);
  string ans(L, 'a');
  set<ll> a;
  for (int i = 0; i < n; i++) {
    a.insert(i * (L / n));
  }
  bool ok = true;
  for (int i = 0; i < m; i++) {
    ll x = i * (L / m);
    if (a.count(x) && t[i] != s[x / (L / n)]) {
      ok = false;
    }
  }
  if (ok) {
    cout << L << '\n';
  } else {
    cout << -1 << '\n';
  }
  return 0;
}


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

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for (int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for (int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }

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

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  ll L = lcm(n, m);
  set<ll> a;
  for (int i = 0; i < n; i++) {
    a.insert(i * (L / n));
  }
  bool ok = true;
  for (int i = 0; i < m; i++) {
    ll x = i * (L / m);
    if (a.count(x) && t[i] != s[x / (L / n)]) {
      ok = false;
    }
  }
  if (ok) {
    cout << L << '\n';
  } else {
    cout << -1 << '\n';
  }
  return 0;
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, ans = -1;
  cin >> N >> M;
  string s, t;
  cin >> s >> t;
  long long L, a = N, b = M, c = a % b;
  while (c != 0) {
    a = b, b = c, c = a % b;
  }
  L = N * M / b;
  int x = L / N, y = L / M, z = min(N / y, M / x);
  for (int i = 0; i < z; i++) {
    if (s[i * y] != t[i * x])
      break;
    if (i == z - 1)
      ans = L;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M, ans = -1;
  cin >> N >> M;
  string s, t;
  cin >> s >> t;
  long long L, a = N, b = M, c = a % b;
  while (c != 0) {
    a = b, b = c, c = a % b;
  }
  L = N * M / b;
  int x = L / N, y = L / M, z = min(N / y, M / x);
  for (int i = 0; i < z; i++) {
    if (s[i * y] != t[i * x])
      break;
    if (i == z - 1)
      ans = L;
  }
  cout << ans << endl;
}
Problem p03231 (C++)

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

string S, T;

int main() {
  long long N, M;
  scanf("%lld%lld", &N, &M);
  cin >> S >> T;
  if (N > M)
    swap(N, M), swap(S, T);
  long long G = __gcd(N, M);
  long long ans = N / G * M;
  long long sg = ans / N, tg = ans / M, si = 0, ti = 0;
  while (si < ans && ti < ans) {
    if (si == ti && S[si] != T[ti])
      return !printf("-1");
    si += sg;
    while (ti < si)
      ti += tg;
  }
  printf("%lld", ans);
  return 0;
}


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

string S, T;

int main() {
  long long N, M;
  scanf("%lld%lld", &N, &M);
  cin >> S >> T;
  if (N > M)
    swap(N, M), swap(S, T);
  long long G = __gcd(N, M);
  long long ans = N / G * M;
  long long sg = ans / N, tg = ans / M, si = 0, ti = 0;
  while (si < ans && ti < ans) {
    if (si == ti && S[si / sg] != T[ti / tg])
      return !printf("-1");
    si += sg;
    while (ti < si)
      ti += tg;
  }
  printf("%lld", ans);
  return 0;
}
Problem p03231 (C++)

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

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {

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

int main() {
  ll m, n;
  cin >> m >> n;
  string s, t;
  cin >> s >> t;
  if (m < n) {
    string u = s;
    s = t;
    t = u;
    ll o = m;
    m = n;
    n = o;
  }
  if ((m / n) * n == m) {
    ll d = m / n;
    for (ll a = 0; a < n; a++) {
      if (s[d * a] != t[a]) {
        cout << -1 << endl;
        return 0;
      }
    }
    cout << m << endl;
  } else {
    ll h = n / gcd(m, n) * m;
    ll i = h / m;
    ll j = h / n;
    ll k = i * j / gcd(j, i);
    for (int b = 0; b < h / k; b++) {
      if (s[b * k] != t[b]) {
        cout << -1 << endl;
        return 0;
      }
      if (b == h / k - 1) {
        cout << h << endl;
      }
    }
  }
  return 0;
}

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

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {

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

int main() {
  ll m, n;
  cin >> m >> n;
  string s, t;
  cin >> s >> t;
  if (m < n) {
    string u = s;
    s = t;
    t = u;
    ll o = m;
    m = n;
    n = o;
  }
  if ((m / n) * n == m) {
    ll d = m / n;
    for (ll a = 0; a < n; a++) {
      if (s[d * a] != t[a]) {
        cout << -1 << endl;
        return 0;
      }
    }
    cout << m << endl;
  } else {
    ll h = n / gcd(m, n) * m;
    ll i = h / m;
    ll j = h / n;
    ll k = i * j / gcd(j, i);
    for (int b = 0; b < h / k; b++) {
      if (s[b * j] != t[b * i]) {
        cout << -1 << endl;
        return 0;
      }
      if (b == h / k - 1) {
        cout << h << endl;
      }
    }
  }
  return 0;
}
Problem p03231 (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 REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)

typedef long long ll;

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  int G = gcd(N, M);
  ll lcm = (ll)N * M / G;

  vector<ll> Si(N), Ti(M);
  REP(i, N) { Si[i] = i * lcm / N + 1; }
  REP(i, M) {
    Ti[i] = i * lcm / M + 1;
    // cout << Ti[i] << " ";
  }
  // cout << endl;
  REP(i, N) {
    int ite = find(Ti.begin(), Ti.end(), Si[i]) - Ti.begin();
    // cout << "Si: " << Si[i] << " ite: " << ite << endl;
    if (ite != Ti.size() && S[i] != T[ite]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lcm << endl;
  return 0;
}


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

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)

typedef long long ll;

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  int G = gcd(N, M);
  ll lcm = (ll)N * M / G;

  vector<ll> Si(N), Ti(M);
  REP(i, N) { Si[i] = i * lcm / N + 1; }
  REP(i, M) {
    Ti[i] = i * lcm / M + 1;
    // cout << Ti[i] << " ";
  }
  // cout << endl;
  REP(i, N) {
    int ite = lower_bound(Ti.begin(), Ti.end(), Si[i]) - Ti.begin();
    if (ite != Ti.size() && Si[i] != Ti[ite]) {
      ite = Ti.size();
    }
    // cout << "Si: " << Si[i] << " ite: " << ite << endl;
    if (ite != Ti.size() && S[i] != T[ite]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << lcm << endl;
  return 0;
}
Problem p03231 (C++)

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

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

int main() {
  int n, m;
  cin >> n >> m;
  string s1, s2;
  cin >> s1 >> s2;
  int x = gcd(n, m);
  long long lcm = ((long long)n * m) / x;
  vector<char> good(lcm, '0');
  for (int i = 0; i < n; i++) {
    long long id = i * (lcm / n);
    good[id] = s1[i];
  }
  for (int i = 0; i < m; i++) {
    long long id = i * (lcm / m);
    if (good[id] != '0' && good[id] != s2[i]) {
      cout << -1 << '\n';
      return 0;
    }
  }
  cout << lcm << '\n';
}


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

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

int main() {
  int n, m;
  cin >> n >> m;
  string s1, s2;
  cin >> s1 >> s2;
  int x = gcd(n, m);
  long long lcm = ((long long)n * m) / x;
  if (s1[0] != s2[0]) {
    cout << -1 << '\n';
    return 0;
  }
  if (x == 1) {
    cout << lcm << '\n';
    return 0;
  }
  vector<char> good(lcm, '0');
  for (int i = 0; i < n; i++) {
    long long id = i * (lcm / n);
    good[id] = s1[i];
  }
  for (int i = 0; i < m; i++) {
    long long id = i * (lcm / m);
    if (good[id] != '0' && good[id] != s2[i]) {
      cout << -1 << '\n';
      return 0;
    }
  }
  cout << lcm << '\n';
}
Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long int lli;

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  int g = gcd(N, M);
  int n = N / g, m = M / g;
  bool ok = true;
  rep(i, g) {
    if (S[i * n] != T[i * g])
      ok = false;
  }
  if (ok) {
    lli ans = (lli)N * M / g;
    cout << ans;
  } else
    cout << -1;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long int lli;

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

int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  int g = gcd(N, M);
  int n = N / g, m = M / g;
  bool ok = true;
  rep(i, g) {
    if (S[i * n] != T[i * m])
      ok = false;
  }
  if (ok) {
    lli ans = (lli)N * M / g;
    cout << ans;
  } else
    cout << -1;
}