prithivMLmods/Coder-Stat huggingface dataset:
Problem p03081 (Python)

Buggy submission (status: Runtime Error):
N, Q = map(int, input().split())
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))


Fixed submission:
# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define NMAX 7003
using namespace std;
int N, Q;
string s;
pair<char, char> Que[100003];
int fall(int x) {
  if (x < 0)
    return -1;
  if (x >= N)
    return 1;
  for (int i = 0; i < Q; ++i) {
    if (s[x] == Que[i].first) {
      if (Que[i].second == 'L')
        --x;
      else
        ++x;
    }
    if (x < 0)
      return -1;
    if (x >= N)
      return 1;
  }
  return 0;
}
int srch(int left, int right, int type) {
  int mid, sol;
  while (left <= right) {
    mid = (left + right) / 2;
    int x = fall(mid); // -1 L 0 STAY 1 R
    if (x == 1) {
      right = mid - 1;
    } else if (x == -1) {
      left = mid + 1;
    } else {
      if (type == 1)
        left = mid + 1;
      else
        right = mid - 1;
    }
    if (type == x)
      sol = mid;
  }
  return sol;
}
int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < Q; ++i) {
    char t, d;
    cin >> t >> d;
    Que[i] = mp(t, d);
  }
  cout << N - (srch(-1, N, -1) + 1 + N - srch(0, N, 1));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define NMAX 7003
using namespace std;
int N, Q;
string s;
pair<char, char> Que[200007];
int fall(int x) {
  if (x < 0)
    return -1;
  if (x >= N)
    return 1;
  for (int i = 0; i < Q; ++i) {
    if (s[x] == Que[i].first) {
      if (Que[i].second == 'L')
        --x;
      else
        ++x;
    }
    if (x < 0)
      return -1;
    if (x >= N)
      return 1;
  }
  return 0;
}
int srch(int left, int right, int type) {
  int mid, sol;
  while (left <= right) {
    mid = (left + right) / 2;
    int x = fall(mid); // -1 L 0 STAY 1 R
    if (x == 1) {
      right = mid - 1;
    } else if (x == -1) {
      left = mid + 1;
    } else {
      if (type == 1)
        left = mid + 1;
      else
        right = mid - 1;
    }
    if (type == x)
      sol = mid;
  }
  return sol;
}
int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < Q; ++i) {
    char t, d;
    cin >> t >> d;
    Que[i] = mp(t, d);
  }
  cout << N - (srch(-1, N, -1) + 1 + N - srch(0, N, 1));
  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
char A[100000], B[100000];
ll n, k, s, g, m, ans;
string t;
bool f(ll p, ll q) {
  for (int i = 0; i < k; i++) {
    if (A[i] == t[p]) {
      if (B[i] == 'L')
        p--;
      else
        p++;
    }
  }
  if (p == q)
    return true;
  return false;
}
int main(void) {
  cin >> n >> k >> t;
  t = '#' + t + '#';
  for (int i = 0; i < k; i++)
    cin >> A[i] >> B[i];
  s = 0, g = n + 1;
  while (g - s > 1) {
    m = (g + s) / 2;
    if (f(m, 0))
      s = m;
    else
      g = m;
  }
  ans += s;
  s = 0, g = n + 1;
  // cout<<ans<<endl;
  while (g - s > 1) {
    m = (g + s) / 2;
    if (f(m, n + 1))
      g = m;
    else
      s = m;
    // cout<<s<<g<<endl;
  }
  ans += n - s;
  // cout<<ans<<endl;
  cout << n - ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
char A[200000], B[200000];
ll n, k, s, g, m, ans;
string t;
bool f(ll p, ll q) {
  for (int i = 0; i < k; i++) {
    if (A[i] == t[p]) {
      if (B[i] == 'L')
        p--;
      else
        p++;
    }
  }
  if (p == q)
    return true;
  return false;
}
int main(void) {
  cin >> n >> k >> t;
  t = '#' + t + '#';
  for (int i = 0; i < k; i++)
    cin >> A[i] >> B[i];
  s = 0, g = n + 1;
  while (g - s > 1) {
    m = (g + s) / 2;
    if (f(m, 0))
      s = m;
    else
      g = m;
  }
  ans += s;
  s = 0, g = n + 1;
  // cout<<ans<<endl;
  while (g - s > 1) {
    m = (g + s) / 2;
    if (f(m, n + 1))
      g = m;
    else
      s = m;
    // cout<<s<<g<<endl;
  }
  ans += n - s;
  // cout<<ans<<endl;
  cout << n - ans << endl;
}
Problem p03081 (C++)

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

int main() {
  int N, Q;
  cin >> N >> Q;
  string s;
  cin >> s;
  vector<char> j(N);
  vector<char> LR(Q);
  vector<bool> ans1(N, false);
  vector<bool> ans2(N, false);
  for (int i = 0; i < Q; i++) {
    cin >> j[i] >> LR[i];
  }
  char mL = s[0], mR = s[N - 1];
  int lp = 0, rp = N - 1;
  for (int i = Q - 1; i >= 0; i--) {
    if (LR[i] == 'L') {
      if (lp < N && lp >= 0 && j[i] == s[lp]) {
        if (lp < N)
          ans1[lp] = true;
        lp++;
      }
      if (rp < N - 1 && rp >= 0 && j[i] == s[rp + 1]) {
        if (rp < N)
          ans2[rp + 1] = false;
        rp++;
      }
    }
    if (LR[i] == 'R') {
      if (rp < N && rp >= 0 && j[i] == s[rp]) {
        if (rp > -1)
          ans2[rp] = true;
        rp--;
      }
      if (lp < N && lp > 0 && j[i] == s[lp - 1]) {
        if (lp > -1)
          ans1[lp - 1] = false;
        lp--;
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (ans1[i] || ans2[i])
      cnt++;
  }
  cout << N - cnt << endl;
}


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

int main() {
  int N, Q;
  cin >> N >> Q;
  string s;
  cin >> s;
  vector<char> j(Q);
  vector<char> LR(Q);
  vector<bool> ans1(N, false);
  vector<bool> ans2(N, false);
  for (int i = 0; i < Q; i++) {
    cin >> j[i] >> LR[i];
  }
  char mL = s[0], mR = s[N - 1];
  int lp = 0, rp = N - 1;
  for (int i = Q - 1; i >= 0; i--) {
    if (LR[i] == 'L') {
      if (lp < N && lp >= 0 && j[i] == s[lp]) {
        if (lp < N)
          ans1[lp] = true;
        lp++;
      }
      if (rp < N - 1 && rp >= 0 && j[i] == s[rp + 1]) {
        if (rp < N)
          ans2[rp + 1] = false;
        rp++;
      }
    }
    if (LR[i] == 'R') {
      if (rp < N && rp >= 0 && j[i] == s[rp]) {
        if (rp > -1)
          ans2[rp] = true;
        rp--;
      }
      if (lp < N && lp > 0 && j[i] == s[lp - 1]) {
        if (lp > -1)
          ans1[lp - 1] = false;
        lp--;
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (ans1[i] || ans2[i])
      cnt++;
  }
  cout << N - cnt << endl;
}
Problem p03081 (C++)

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

int main() {
  ll n, q;
  std::cin >> n >> q;
  string s;
  std::cin >> s;
  vector<char> t(n), d(n);
  for (int i = 0; i < q; i++) {
    std::cin >> t[i] >> d[i];
  }

  ll l = -1;
  ll r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll now = mid;
    bool ok = false;
    for (int i = 0; i < q; i++) {
      if (s[now] == t[i]) {
        if (d[i] == 'R') {
          now++;
        } else {
          now--;
        }
        if (now == -1) {
          ok = true;
          break;
        }
      }
    }
    if (ok) {
      l = mid;
    } else {
      r = mid;
    }
  }

  ll lcnt = l + 1;
  // std::cout << lcnt << std::endl;
  l = -1;
  r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll now = mid;
    bool ok = false;
    for (int i = 0; i < q; i++) {
      if (s[n - 1 - now] == t[i]) {
        if (d[i] == 'R') {
          now--;
        } else {
          now++;
        }
        if (now == -1) {
          ok = true;
          break;
        }
      }
    }
    if (ok) {
      l = mid;
    } else {
      r = mid;
    }
  }
  // std::cout << l+1 << std::endl;
  std::cout << n - min(n, lcnt + l + 1) << std::endl;
}


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

int main() {
  ll n, q;
  std::cin >> n >> q;
  string s;
  std::cin >> s;
  vector<char> t(q), d(q);
  for (int i = 0; i < q; i++) {
    std::cin >> t[i] >> d[i];
  }

  ll l = -1;
  ll r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll now = mid;
    bool ok = false;
    for (int i = 0; i < q; i++) {
      if (s[now] == t[i]) {
        if (d[i] == 'R') {
          now++;
        } else {
          now--;
        }
        if (now == -1) {
          ok = true;
          break;
        }
      }
    }
    if (ok) {
      l = mid;
    } else {
      r = mid;
    }
  }

  ll lcnt = l + 1;
  // std::cout << lcnt << std::endl;
  l = -1;
  r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll now = mid;
    bool ok = false;
    for (int i = 0; i < q; i++) {
      if (s[n - 1 - now] == t[i]) {
        if (d[i] == 'R') {
          now--;
        } else {
          now++;
        }
        if (now == -1) {
          ok = true;
          break;
        }
      }
    }
    if (ok) {
      l = mid;
    } else {
      r = mid;
    }
  }
  // std::cout << l+1 << std::endl;
  std::cout << n - min(n, lcnt + l + 1) << std::endl;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef long long int ll;

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;

typedef long long int ll;

int n, q;
string s;
char t[114514], d[114514];

int sim(int pos) {
  for (int i = 0; i < q; i++) {
    if (t[i] == s[pos]) {
      pos += (d[i] == 'R') ? 1 : -1;
    }
    if (pos < 0)
      return -1;
    if (pos >= n)
      return 1;
  }
  return 0;
}

int main(void) {
  cin >> n >> q;
  cin >> s;
  for (int i = 0; i < q; i++)
    cin >> t[i] >> d[i];

  // 何番目まで左に落ちるか
  int lo = -1, hi = n, mid1, mid2;
  while (lo + 1 < hi) {
    // cerr << lo << " " << hi << endl;
    mid1 = (lo + hi) / 2;
    if (sim(mid1) == -1) {
      lo = mid1;
    } else {
      hi = mid1;
    }
  }
  int left = lo;
  // 何番目まで右に落ちないか
  lo = -1, hi = n;
  while (lo + 1 < hi) {
    // cerr << lo << ":" << hi << endl;
    mid2 = (lo + hi) / 2;
    if (sim(mid2) == 1) {
      hi = mid2;
    } else {
      lo = mid2;
    }
  }
  int right = lo;
  // cerr << left << " and " << right << endl;
  cout << right - left << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef long long int ll;

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;

typedef long long int ll;

int n, q;
string s;
char t[214514], d[214514];

int sim(int pos) {
  for (int i = 0; i < q; i++) {
    if (t[i] == s[pos]) {
      pos += (d[i] == 'R') ? 1 : -1;
    }
    if (pos < 0)
      return -1;
    if (pos >= n)
      return 1;
  }
  return 0;
}

int main(void) {
  cin >> n >> q;
  cin >> s;
  for (int i = 0; i < q; i++)
    cin >> t[i] >> d[i];

  // 何番目まで左に落ちるか
  int lo = -1, hi = n, mid1, mid2;
  while (lo + 1 < hi) {
    // cerr << lo << " " << hi << endl;
    mid1 = (lo + hi) / 2;
    if (sim(mid1) == -1) {
      lo = mid1;
    } else {
      hi = mid1;
    }
  }
  int left = lo;
  // 何番目まで右に落ちないか
  lo = -1, hi = n;
  while (lo + 1 < hi) {
    // cerr << lo << ":" << hi << endl;
    mid2 = (lo + hi) / 2;
    if (sim(mid2) == 1) {
      hi = mid2;
    } else {
      lo = mid2;
    }
  }
  int right = lo;
  // cerr << left << " and " << right << endl;
  cout << right - left << endl;

  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int N, Q;
string s;
vector<int> c(200000, 0);
vector<vector<int>> td(200000, vector<int>(2, 0));

int bsL(int l, int r) {
  // cout << 'L' << ' ' << l << ' ' << r << endl;
  int mid = (l + r) / 2;
  if (mid == l || r == 0) {
    return l;
  }
  int now = mid;
  for (int i = 0; i < Q; i++) {
    // cout << now << endl;
    if (c[now] == td[i][0]) {
      now += td[i][1];
    }
    if (now < 0) {
      return bsL(mid, r);
    } else if (now >= N) {
      break;
    }
  }
  return bsL(l, mid);
}

int bsR(int l, int r) {
  // cout << 'R' << ' ' << l << ' ' << r << endl;
  int mid = (l + r) / 2;
  if (mid == l) {
    return r;
  }
  int now = mid;
  for (int i = 0; i < Q; i++) {
    // cout << now << endl;
    if (c[now] == td[i][0]) {
      now += td[i][1];
    }
    if (now >= N) {
      return bsR(l, mid);
    } else if (now < 0) {
      break;
    }
  }
  return bsR(mid, r);
}

int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < N; i++) {
    c[i] = s[i] - 'A';
  }
  for (int i = 0; i < Q; i++) {
    char t, d;
    cin >> t >> d;
    td[i][0] = t - 'A';
    if (d == 'L') {
      td[i][1] = -1;
    } else {
      td[i][1] = 1;
    }
  }
  int ans = N;
  ans -= bsL(-1, N) + 1;
  ans -= N - bsR(-1, N);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int N, Q;
string s;
vector<int> c(200000, 0);
vector<vector<int>> td(200000, vector<int>(2, 0));

int bsL(int l, int r) {
  // cout << 'L' << ' ' << l << ' ' << r << endl;
  int mid = (l + r) / 2;
  if (mid == l || r == 0) {
    return l;
  }
  int now = mid;
  for (int i = 0; i < Q; i++) {
    // cout << now << endl;
    if (c[now] == td[i][0]) {
      now += td[i][1];
    }
    if (now < 0) {
      return bsL(mid, r);
    } else if (now >= N) {
      break;
    }
  }
  return bsL(l, mid);
}

int bsR(int l, int r) {
  // cout << 'R' << ' ' << l << ' ' << r << endl;
  int mid = (l + r) / 2;
  if (mid == l || r == 0) {
    return r;
  }
  int now = mid;
  for (int i = 0; i < Q; i++) {
    // cout << now << endl;
    if (c[now] == td[i][0]) {
      now += td[i][1];
    }
    if (now >= N) {
      return bsR(l, mid);
    } else if (now < 0) {
      break;
    }
  }
  return bsR(mid, r);
}

int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < N; i++) {
    c[i] = s[i] - 'A';
  }
  for (int i = 0; i < Q; i++) {
    char t, d;
    cin >> t >> d;
    td[i][0] = t - 'A';
    if (d == 'L') {
      td[i][1] = -1;
    } else {
      td[i][1] = 1;
    }
  }
  int ans = N;
  ans -= bsL(-1, N) + 1;
  ans -= N - bsR(-1, N);
  cout << ans << endl;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

int pos(const string &s, const string &t, const vector<bool> &dir, int i) {
  int q = t.size();
  REP(j, q) {
    if (s[i] == t[j]) {
      if (dir[j]) {
        --i;
      } else {
        ++i;
      }
    }
  }
  return i;
}

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  string t;
  vector<bool> dir(n);
  REP(i, q) {
    string c;
    string d;
    cin >> c >> d;
    t.push_back(c[0]);
    dir[i] = d[0] == 'L';
  }
  s = "$" + s + "$";
  int lo = 0;
  int hi = n + 1;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (pos(s, t, dir, mid) == 0) {
      lo = mid;
    } else {
      hi = mid;
    }
  }
  int left = lo;
  lo = 0;
  hi = n + 1;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (pos(s, t, dir, mid) == n + 1) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  int right = n + 1 - hi;
  cout << (n - left - right) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

int pos(const string &s, const string &t, const vector<bool> &dir, int i) {
  int q = t.size();
  REP(j, q) {
    if (s[i] == t[j]) {
      if (dir[j]) {
        --i;
      } else {
        ++i;
      }
    }
  }
  return i;
}

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  string t;
  vector<bool> dir(q);
  REP(i, q) {
    string c;
    string d;
    cin >> c >> d;
    t.push_back(c[0]);
    dir[i] = d[0] == 'L';
  }
  s = "$" + s + "$";
  int lo = 0;
  int hi = n + 1;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (pos(s, t, dir, mid) == 0) {
      lo = mid;
    } else {
      hi = mid;
    }
  }
  int left = lo;
  lo = 0;
  hi = n + 1;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (pos(s, t, dir, mid) == n + 1) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  int right = n + 1 - hi;
  cout << (n - left - right) << endl;
  return 0;
}
Problem p03081 (C++)

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

using namespace std;

void solve(void) {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<char> ts(n), ds(n);
  for (int i = 0; i < q; i++) {
    cin >> ts[i] >> ds[i];
  }
  int l = -1, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2, p = m;
    char t = s[p];
    for (int i = 0; i < q; i++) {
      if (ts[i] == t) {
        if (ds[i] == 'L') {
          p--;
        } else
          p++;
      }
      if (p < 0 || p >= n)
        break;
      t = s[p];
    }
    if (p < 0)
      l = m;
    else
      r = m;
  }
  int ans = n - l - 1;
  l = -1, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2, p = m;
    char t = s[p];
    for (int i = 0; i < q; i++) {
      if (ts[i] == t) {
        if (ds[i] == 'L') {
          p--;
        } else
          p++;
      }
      if (p < 0 || p >= n)
        break;
      t = s[p];
    }
    if (p >= n)
      r = m;
    else
      l = m;
  }
  ans -= n - r;
  cout << ans << endl;
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


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

using namespace std;

void solve(void) {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<char> ts(q), ds(q);
  for (int i = 0; i < q; i++) {
    cin >> ts[i] >> ds[i];
  }
  int l = -1, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2, p = m;
    char t = s[p];
    for (int i = 0; i < q; i++) {
      if (ts[i] == t) {
        if (ds[i] == 'L') {
          p--;
        } else
          p++;
      }
      if (p < 0 || p >= n)
        break;
      t = s[p];
    }
    if (p < 0)
      l = m;
    else
      r = m;
  }
  int ans = n - l - 1;
  l = -1, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2, p = m;
    char t = s[p];
    for (int i = 0; i < q; i++) {
      if (ts[i] == t) {
        if (ds[i] == 'L') {
          p--;
        } else
          p++;
      }
      if (p < 0 || p >= n)
        break;
      t = s[p];
    }
    if (p >= n)
      r = m;
    else
      l = m;
  }
  ans -= n - r;
  cout << ans << endl;
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int const NMAX = 201;
char s[NMAX], t[NMAX], d[NMAX];
int n, q;
int lastposition(int i) {
  int c = i;
  for (int qi = 0; qi < q; qi++) {
    if (s[c] == t[qi]) {
      if (d[qi] == 'L') {
        c--;
        if (c < 0)
          return -1;
      } else {
        c++;
        if (c >= n)
          return n;
      }
    }
  }
  return c;
}

int main() {
  scanf("%d%d%s", &n, &q, s);
  for (int i = 0; i < q; i++) {
    char tmp0[2], tmp1[2];
    scanf("%s%s", tmp0, tmp1);
    t[i] = tmp0[0];
    d[i] = tmp1[0];
  }
  // for(int i=0;i<n;i++)printf("%d->%d\n",i,lastposition(i));
  // 端にあるゴーレムが反対側の端に落ちるならすべて消滅する
  if (lastposition(n - 1) == -1 || lastposition(0) == n) {
    printf("0\n");
    return 0;
  }
  // l,l+1,...,rにいたゴーレムが生き残るとする
  // l-1は-1に消える
  int lmin = 0, lmax = n - 1, l;
  while (lmin + 1 < lmax) {
    int lmid = (lmin + lmax) / 2;
    if (lastposition(lmid) >= 0)
      lmax = lmid;
    else
      lmin = lmid;
  }
  if (lastposition(lmin) == -1)
    l = lmax;
  else
    l = lmin;
  // r+1はnに消える
  int rmin = 0, rmax = n - 1, r;
  while (rmin + 1 < rmax) {
    int rmid = (rmin + rmax) / 2;
    if (lastposition(rmid) < n)
      rmin = rmid;
    else
      rmax = rmid;
  }
  if (lastposition(rmax) == n)
    r = rmin;
  else
    r = rmax;
  printf("%d\n", r - l + 1);
  return 0;
}

Fixed submission:
#include <cstdio>
int const NMAX = 200001;
char s[NMAX], t[NMAX], d[NMAX];
int n, q;
int lastposition(int i) {
  int c = i;
  for (int qi = 0; qi < q; qi++) {
    if (s[c] == t[qi]) {
      if (d[qi] == 'L') {
        c--;
        if (c < 0)
          return -1;
      } else {
        c++;
        if (c >= n)
          return n;
      }
    }
  }
  return c;
}

int main() {
  scanf("%d%d%s", &n, &q, s);
  for (int i = 0; i < q; i++) {
    char tmp0[2], tmp1[2];
    scanf("%s%s", tmp0, tmp1);
    t[i] = tmp0[0];
    d[i] = tmp1[0];
  }
  // for(int i=0;i<n;i++)printf("%d->%d\n",i,lastposition(i));
  // 端にあるゴーレムが反対側の端に落ちるならすべて消滅する
  if (lastposition(n - 1) == -1 || lastposition(0) == n) {
    printf("0\n");
    return 0;
  }
  // l,l+1,...,rにいたゴーレムが生き残るとする
  // l-1は-1に消える
  int lmin = 0, lmax = n - 1, l;
  while (lmin + 1 < lmax) {
    int lmid = (lmin + lmax) / 2;
    if (lastposition(lmid) >= 0)
      lmax = lmid;
    else
      lmin = lmid;
  }
  if (lastposition(lmin) == -1)
    l = lmax;
  else
    l = lmin;
  // r+1はnに消える
  int rmin = 0, rmax = n - 1, r;
  while (rmin + 1 < rmax) {
    int rmid = (rmin + rmax) / 2;
    if (lastposition(rmid) < n)
      rmin = rmid;
    else
      rmax = rmid;
  }
  if (lastposition(rmax) == n)
    r = rmin;
  else
    r = rmax;
  printf("%d\n", r - l + 1);
  return 0;
}
Problem p03081 (C++)

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

#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++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll n, q;
string s;
vector<char> t(100010), d(100010);

ll check(ll x) {
  REP(i, q) {
    if (t[i] != s[x])
      continue;
    if (d[i] == 'L')
      --x;
    else
      ++x;

    if (x == -1 || x == n)
      return x;
  }
  return 0;
}

int main() {
  cin >> n >> q;

  cin >> s;

  REP(i, q) { cin >> t[i] >> d[i]; }
  t.resize(q);
  d.resize(q);

  ll sum = 0;
  ll lb = -1, ub = n;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (check(mid) == -1)
      lb = mid;
    else
      ub = mid;
  }
  sum += ub;

  lb = -1, ub = n;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (check(mid) == n)
      ub = mid;
    else
      lb = mid;
  }
  sum += n - ub;

  cout << n - sum << endl;
  return 0;
}

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

#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++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll n, q;
string s;
vector<char> t(200010), d(200010);

ll check(ll x) {
  REP(i, q) {
    if (t[i] != s[x])
      continue;
    if (d[i] == 'L')
      --x;
    else
      ++x;

    if (x == -1 || x == n)
      return x;
  }
  return 0;
}

int main() {
  cin >> n >> q;

  cin >> s;

  REP(i, q) { cin >> t[i] >> d[i]; }
  t.resize(q);
  d.resize(q);

  ll sum = 0;
  ll lb = -1, ub = n;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (check(mid) == -1)
      lb = mid;
    else
      ub = mid;
  }
  sum += ub;

  lb = -1, ub = n;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    if (check(mid) == n)
      ub = mid;
    else
      lb = mid;
  }
  sum += n - ub;

  cout << n - sum << endl;
  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  char t[N], d[N];
  int ts = -1, te = N;
  for (int i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }
  for (int i = Q - 1; i >= 0; i--) {
    if (te - ts <= 1)
      break;
    if (d[i] == 'L') {
      if (ts < N - 1 && S[ts + 1] == t[i])
        ts++;
      if (te < N && S[te] == t[i])
        te++;
    } else if (d[i] == 'R') {
      if (0 <= ts && S[ts] == t[i])
        ts--;
      if (0 < te && S[te - 1] == t[i])
        te--;
    }
  }
  cout << N - (ts + 1 + N - te) << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  char t[Q], d[Q];
  int ts = -1, te = N;
  for (int i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }
  for (int i = Q - 1; i >= 0; i--) {
    if (te - ts <= 1)
      break;
    if (d[i] == 'L') {
      if (ts < N - 1 && S[ts + 1] == t[i])
        ts++;
      if (te < N && S[te] == t[i])
        te++;
    } else if (d[i] == 'R') {
      if (0 <= ts && S[ts] == t[i])
        ts--;
      if (0 < te && S[te - 1] == t[i])
        te--;
    }
  }
  cout << N - (ts + 1 + N - te) << endl;
  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;

ll N, Q;
string s;
vector<char> t, d;

void input() {
  cin >> N >> Q >> s;
  t.resize(N);
  d.resize(N);
  for (ll i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }
}

ll goal(ll p) {
  for (ll i = 0; i < Q; i++) {
    if (s[p] == t[i]) {
      if (d[i] == 'L')
        p--;
      else
        p++;
    }
    if (p < 0 or p >= N)
      return p;
  }
  return p;
}

ll solve() {
  vector<ll> spos[0xff];
  for (ll i = 0; i < N; i++) {
    spos[ll(s[i])].push_back(i);
  }
  ll dels = 0;
  ll l = 0, r = N - 1;
  ll c = (l + r) / 2;
  while (l <= r) {
    if (goal(c) < 0) {
      dels += c - l + 1;
      l = c + 1;
    } else {
      r = c - 1;
    }
    c = (l + r) / 2;
  }
  l = 0, r = N - 1;
  while (l <= r) {
    if (goal(c) >= N) {
      dels += r - c + 1;
      r = c - 1;
    } else {
      l = c + 1;
    }
    c = (l + r) / 2;
  }

  return N - dels;
}

int main() {
  input();
  cout << solve() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;

ll N, Q;
string s;
vector<char> t, d;

void input() {
  cin >> N >> Q >> s;
  t.resize(Q);
  d.resize(Q);
  for (ll i = 0; i < Q; i++) {
    cin >> t[i] >> d[i];
  }
}

ll goal(ll p) {
  for (ll i = 0; i < Q; i++) {
    if (s[p] == t[i]) {
      if (d[i] == 'L')
        p--;
      else
        p++;
    }
    if (p < 0 or p >= N)
      return p;
  }
  return p;
}

ll solve() {
  vector<ll> spos[0xff];
  for (ll i = 0; i < N; i++) {
    spos[ll(s[i])].push_back(i);
  }
  ll dels = 0;
  ll l = 0, r = N - 1;
  ll c = (l + r) / 2;
  while (l <= r) {
    if (goal(c) < 0) {
      dels += c - l + 1;
      l = c + 1;
    } else {
      r = c - 1;
    }
    c = (l + r) / 2;
  }
  l = 0, r = N - 1;
  while (l <= r) {
    if (goal(c) >= N) {
      dels += r - c + 1;
      r = c - 1;
    } else {
      l = c + 1;
    }
    c = (l + r) / 2;
  }

  return N - dels;
}

int main() {
  input();
  cout << solve() << endl;

  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
// #include <unordered_map>
// #include <unordered_set>
// #include <boost/container/static_vector.hpp>
// #include <boost/unordered_set.hpp>
// #include <boost/unordered_map.hpp>
// #include <unistd.h>

const int MAX_N = 100050;

int N, Q;
std::string S;

char tile[MAX_N];
char t[MAX_N], d[MAX_N];

int cal_pos(int pos) {
  for (int i = 1; i <= Q; i++) {
    if (t[i] == tile[pos]) {
      if (d[i] == 'L') {
        pos--;
      } else {
        pos++;
      }
    }
    if (pos == 0 || pos == N + 1) {
      break;
    }
  }
  return pos;
}

int main(int argc, char **argv) {
  std::cin >> N >> Q;
  std::cin >> S;

  for (int i = 1; i <= Q; i++) {
    std::cin >> t[i] >> d[i];
  }

  for (int i = 0; i < S.size(); i++) {
    tile[i + 1] = S[i];
  }

  int lb = 0;
  int ub = N + 1;

  while (lb + 1 < ub) {
    int mid = (lb + ub) / 2;
    if (cal_pos(mid) == 0) {
      lb = mid;
    } else {
      ub = mid;
    }
  }

  int left_cond = lb;

  lb = 0;
  ub = N + 1;

  while (lb + 1 < ub) {
    int mid = (lb + ub) / 2;
    if (cal_pos(mid) == N + 1) {
      ub = mid;
    } else {
      lb = mid;
    }
  }

  int right_cond = ub;

  // std::cout << left_cond << " " << right_cond << std::endl;

  int ret = 0;
  for (int i = 1; i <= N; i++) {
    if (left_cond < i && i < right_cond) {
      ret++;
    }
  }
  std::cout << ret << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
// #include <unordered_map>
// #include <unordered_set>
// #include <boost/container/static_vector.hpp>
// #include <boost/unordered_set.hpp>
// #include <boost/unordered_map.hpp>
// #include <unistd.h>

const int MAX_N = 200050;

int N, Q;
std::string S;

char tile[MAX_N];
char t[MAX_N], d[MAX_N];

int cal_pos(int pos) {
  for (int i = 1; i <= Q; i++) {
    if (t[i] == tile[pos]) {
      if (d[i] == 'L') {
        pos--;
      } else {
        pos++;
      }
    }
    if (pos == 0 || pos == N + 1) {
      break;
    }
  }
  return pos;
}

int main(int argc, char **argv) {
  std::cin >> N >> Q;
  std::cin >> S;

  for (int i = 1; i <= Q; i++) {
    std::cin >> t[i] >> d[i];
  }

  for (int i = 0; i < S.size(); i++) {
    tile[i + 1] = S[i];
  }

  int lb = 0;
  int ub = N + 1;

  while (lb + 1 < ub) {
    int mid = (lb + ub) / 2;
    if (cal_pos(mid) == 0) {
      lb = mid;
    } else {
      ub = mid;
    }
  }

  int left_cond = lb;

  lb = 0;
  ub = N + 1;

  while (lb + 1 < ub) {
    int mid = (lb + ub) / 2;
    if (cal_pos(mid) == N + 1) {
      ub = mid;
    } else {
      lb = mid;
    }
  }

  int right_cond = ub;

  // std::cout << left_cond << " " << right_cond << std::endl;

  int ret = 0;
  for (int i = 1; i <= N; i++) {
    if (left_cond < i && i < right_cond) {
      ret++;
    }
  }
  std::cout << ret << std::endl;

  return 0;
}
Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int maxn = 222;
const int maxs = 200100;
const int mod = 1000000007;
char t[maxn], d[maxn];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 0; i < q; i++)
    cin >> t[i] >> d[i];
  int tot = 0;
  int lo = 0, hi = n - 1, ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int pv = mid;
    for (int i = 0; i < q && pv > -1 && pv < n; i++)
      if (s[pv] == t[i]) {
        if (d[i] == 'L')
          pv--;
        else
          pv++;
      }
    if (pv < 0)
      ans = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  tot += ans + 1;
  lo = 0, hi = n - 1, ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int pv = mid;
    for (int i = 0; i < q && pv > -1 && pv < n; i++)
      if (s[pv] == t[i]) {
        if (d[i] == 'L')
          pv--;
        else
          pv++;
      }
    if (pv >= n)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  if (ans != -1)
    tot += n - ans;
  cout << n - tot << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int maxn = 200200;
const int maxs = 200100;
const int mod = 1000000007;
char t[maxn], d[maxn];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 0; i < q; i++)
    cin >> t[i] >> d[i];
  int tot = 0;
  int lo = 0, hi = n - 1, ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int pv = mid;
    for (int i = 0; i < q && pv > -1 && pv < n; i++)
      if (s[pv] == t[i]) {
        if (d[i] == 'L')
          pv--;
        else
          pv++;
      }
    if (pv < 0)
      ans = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  tot += ans + 1;
  lo = 0, hi = n - 1, ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    int pv = mid;
    for (int i = 0; i < q && pv > -1 && pv < n; i++)
      if (s[pv] == t[i]) {
        if (d[i] == 'L')
          pv--;
        else
          pv++;
      }
    if (pv >= n)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  if (ans != -1)
    tot += n - ans;
  cout << n - tot << endl;
  return 0;
}
Problem p03081 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

#define S second
#define F first
#define kill(x) return (cout << x << endl, 0)
#define debug(x) cerr << #x << " : " << x << endl
#define ddebug(x, y)                                                           \
  cerr << #x << " : " << x << ", " << #y << " : " << y << endl
#define sz(s) (int)s.size()
#define sq(x) (x) * (x)
#define PB(x) push_back(x)
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) x.begin(), x.end()
#define SP fixed << setprecision(10)
#define UB upper_bound

const int MAXN = 100 * 1000 + 10;
int n, q;
char c[MAXN], s[MAXN];
bool d[MAXN];

bool dead(int ind) {
  for (int i = 0; i < q; i++) {
    if (c[i] == s[ind])
      ind += (d[i] ? +1 : -1);
    if (ind < 0)
      return true;
  }
  return false;
}

int bs() {
  int l = 0, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (dead(mid - 1))
      l = mid;
    else
      r = mid;
  }
  return l;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < q; i++) {
    char cc;
    cin >> c[i] >> cc;
    d[i] = (cc == 'R');
  }

  int ans1 = bs();
  reverse(s, s + n);
  for (int i = 0; i < q; i++)
    d[i] ^= 1;
  int ans2 = bs();

  cout << n - min(ans1 + ans2, n) << endl;

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

#define S second
#define F first
#define kill(x) return (cout << x << endl, 0)
#define debug(x) cerr << #x << " : " << x << endl
#define ddebug(x, y)                                                           \
  cerr << #x << " : " << x << ", " << #y << " : " << y << endl
#define sz(s) (int)s.size()
#define sq(x) (x) * (x)
#define PB(x) push_back(x)
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) x.begin(), x.end()
#define SP fixed << setprecision(10)
#define UB upper_bound

const int MAXN = 200 * 1000 + 10;
int n, q;
char c[MAXN], s[MAXN];
bool d[MAXN];

bool dead(int ind) {
  for (int i = 0; i < q; i++) {
    if (c[i] == s[ind])
      ind += (d[i] ? +1 : -1);
    if (ind < 0)
      return true;
  }
  return false;
}

int bs() {
  int l = 0, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (dead(mid - 1))
      l = mid;
    else
      r = mid;
  }
  return l;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < q; i++) {
    char cc;
    cin >> c[i] >> cc;
    d[i] = (cc == 'R');
  }

  int ans1 = bs();
  reverse(s, s + n);
  for (int i = 0; i < q; i++)
    d[i] ^= 1;
  int ans2 = bs();

  cout << n - min(ans1 + ans2, n) << endl;

  return 0;
}
Problem p03081 (C++)

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

using namespace std;
using ll = long long;

string s;
ll N, Q;
vector<char> t(20000);
vector<char> d(20000);

int simulation(int pos) {
  for (int i = 0; i < Q; ++i) {
    if (pos == 0)
      return -1;
    if (pos == N + 1)
      return 1;

    if (s[pos - 1] == t[i]) {
      if (d[i] == 'R')
        pos++;
      else
        pos--;
    }
  }

  if (pos == 0)
    return -1;
  if (pos == N + 1)
    return 1;
  return 0;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < Q; ++i) {
    cin >> t[i] >> d[i];
  }

  int L1 = 0, R1 = N + 1, M1, maxn = 0;
  while (L1 + 1 < R1) {
    M1 = (L1 + R1) / 2;
    int a = simulation(M1);
    if (a == -1) {
      maxn = max(maxn, M1);
      L1 = M1;
    } else {
      R1 = M1;
    }
  }
  int L2 = 0, R2 = N + 1, M2, minn = N + 1;
  while (L2 + 1 < R2) {
    M2 = (L2 + R2) / 2;
    int a = simulation(M2);
    if (a == 1) {
      minn = min(minn, M2);
      R2 = M2;
    } else {
      L2 = M2;
    }
  }

  cout << minn - maxn - 1 << endl;
}


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

using namespace std;
using ll = long long;

string s;
ll N, Q;
vector<char> t(200000);
vector<char> d(200000);

int simulation(int pos) {
  for (int i = 0; i < Q; ++i) {
    if (pos == 0)
      return -1;
    if (pos == N + 1)
      return 1;

    if (s[pos - 1] == t[i]) {
      if (d[i] == 'R')
        pos++;
      else
        pos--;
    }
  }

  if (pos == 0)
    return -1;
  if (pos == N + 1)
    return 1;
  return 0;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  for (int i = 0; i < Q; ++i) {
    cin >> t[i] >> d[i];
  }

  int L1 = 0, R1 = N + 1, M1, maxn = 0;
  while (L1 + 1 < R1) {
    M1 = (L1 + R1) / 2;
    int a = simulation(M1);
    if (a == -1) {
      maxn = max(maxn, M1);
      L1 = M1;
    } else {
      R1 = M1;
    }
  }
  int L2 = 0, R2 = N + 1, M2, minn = N + 1;
  while (L2 + 1 < R2) {
    M2 = (L2 + R2) / 2;
    int a = simulation(M2);
    if (a == 1) {
      minn = min(minn, M2);
      R2 = M2;
    } else {
      L2 = M2;
    }
  }

  cout << minn - maxn - 1 << endl;
}
Problem p03082 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define PB push_back
#define MP make_pair
#define all(x) x.begin(), x.end()

#define MOD 1000000007LL

#define int long long

int N, X, S[200];
int dp[200][100001];
bool already[200][100001] = {};
int solve(int now, int X) {
  if (now == N - 1)
    return X % S[now];
  if (already[now][X])
    return dp[now][X];

  if (S[now] > X) {
    dp[now][X] = (N - now) * solve(now + 1, X);
  } else {
    dp[now][X] = (N - now - 1) * solve(now + 1, X);
    dp[now][X] %= MOD;
    dp[now][X] += solve(now + 1, X % S[now]);
  }
  dp[now][X] = dp[now][X] % MOD;
  // cout << now << " " << X << " " << dp[now][X] << endl;
  return dp[now][X];
}

signed main() {
  cin >> N >> X;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  sort(S, S + N);
  reverse(S, S + N);
  cout << solve(0, X) << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define PB push_back
#define MP make_pair
#define all(x) x.begin(), x.end()

#define MOD 1000000007LL

#define int long long

int N, X, S[200];
int dp[200][100001];
bool already[200][100001] = {};
int solve(int now, int X) {
  if (now == N - 1)
    return X % S[now];
  if (already[now][X])
    return dp[now][X];

  if (S[now] > X) {
    dp[now][X] = (N - now) * solve(now + 1, X);
  } else {
    dp[now][X] = (N - now - 1) * solve(now + 1, X);
    dp[now][X] %= MOD;
    dp[now][X] += solve(now + 1, X % S[now]);
  }
  dp[now][X] = dp[now][X] % MOD;
  // cout << now << " " << X << " " << dp[now][X] << endl;
  already[now][X] = true;
  return dp[now][X];
}

signed main() {
  cin >> N >> X;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  sort(S, S + N);
  reverse(S, S + N);
  cout << solve(0, X) << endl;

  return 0;
}
Problem p03082 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int mod = 1e9 + 7;
const int N = 201;

int s[200];
ll f[N], fr[N];

ll binpow(ll a, int p) {
  ll res = 1;
  while (p) {
    if (p & 1) {
      (res *= a) %= mod;
    }
    (a *= a) %= mod;
    p >>= 1;
  }
  return res;
}

ll cnk(int n, int k) { return f[n] * fr[k] % mod * fr[n - k] % mod; }

int mp[200][100005];

int solve(int x, int pref) {
  if (pref == 0) {
    return x;
  }
  if (mp[pref][x] != -1) {
    return mp[pref][x];
  }
  ll res = 0;
  for (int i = 0; i < pref; i++) {
    (res += solve(x % s[i], i) * cnk(pref - 1, i) % mod * f[pref - i - 1]) %=
        mod;
  }
  return mp[pref][x] = res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, x;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    fill(mp[i + 1], mp[i + 1] + x + 1, -1);
  }
  f[0] = fr[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = (f[i - 1] * i) % mod;
    fr[i] = binpow(f[i], mod - 2);
  }
  sort(s, s + n);
  cout << solve(x, n);
  return 0;
}

Fixed submission:
// #pragma GCC optimize("O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int mod = 1e9 + 7;
const int N = 201;

int s[200];
ll f[N], fr[N];

ll binpow(ll a, int p) {
  ll res = 1;
  while (p) {
    if (p & 1) {
      (res *= a) %= mod;
    }
    (a *= a) %= mod;
    p >>= 1;
  }
  return res;
}

ll cnk(int n, int k) { return f[n] * fr[k] % mod * fr[n - k] % mod; }

int mp[201][100005];

int solve(int x, int pref) {
  if (pref == 0) {
    return x;
  }
  if (mp[pref][x] != -1) {
    return mp[pref][x];
  }
  ll res = 0;
  for (int i = 0; i < pref; i++) {
    (res += solve(x % s[i], i) * cnk(pref - 1, i) % mod * f[pref - i - 1]) %=
        mod;
  }
  return mp[pref][x] = res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, x;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    fill(mp[i + 1], mp[i + 1] + x + 1, -1);
  }
  f[0] = fr[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = (f[i - 1] * i) % mod;
    fr[i] = binpow(f[i], mod - 2);
  }
  sort(s, s + n);
  cout << solve(x, n);
  return 0;
}
Problem p03082 (Python)

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

from collections import defaultdict


n, X = map(int, input().split())
s = list(map(int, input().split()))
s = sorted(s, reverse=True)

mod = 10**9 + 7

dp = defaultdict(int)
dp[X] = 1

for i in range(n):
    new_dp = defaultdict(int)

    for x in dp:
        mod_x = x % s[i]
        new_dp[x] += dp[x] * (n - i - 1)
        new_dp[mod_x] += dp[x]
        print("new_dp", new_dp)

    dp = new_dp

total = 0
for x in dp:
    total += dp[x] * x

total %= mod

print(total)


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

from collections import defaultdict


n, X = map(int, input().split())
s = list(map(int, input().split()))
s = sorted(s, reverse=True)

mod = 10**9 + 7

dp = defaultdict(int)
dp[X] = 1

for i in range(n):
    new_dp = defaultdict(int)

    for x in dp:
        mod_x = x % s[i]
        new_dp[x] += dp[x] * (n - i - 1)
        new_dp[mod_x] += dp[x]

    dp = new_dp

total = 0
for x in dp:
    total += dp[x] * x

total %= mod

print(total)
Problem p03082 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int N = 202;

int n, x, s[N];
int fac[N];
LL dp[202][100000 + 10];
LL A[202][202];
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
  }
  dp[0][x] = 1;
  sort(s + 1, s + 1 + n);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= x; j++) {
      int cnt = n - i;
      for (int k = 1; k <= n; k++) {
        if (s[k] <= j) {
          (dp[i + 1][j % s[k]] += dp[i][j]);
          if (dp[i + 1][j % s[k]] > MOD)
            dp[i + 1][j % s[k]] -= MOD;
          cnt--;
        } else {
          break;
        }
      }
      (dp[i + 1][j] += dp[i][j] * cnt) %= MOD;
    }
  }
  LL ans = 0;
  for (int i = 0; i <= x; i++) {
    ans = (ans + dp[n][i] * i) % MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int N = 202;

int n, x, s[N];
int fac[N];
LL dp[202][100000 + 10];
LL A[202][202];
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
  }
  dp[0][x] = 1;
  sort(s + 1, s + 1 + n);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= x; j++) {
      if (dp[i][j] == 0)
        continue;
      int cnt = n - i;
      for (int k = 1; k <= n; k++) {
        if (s[k] <= j) {
          (dp[i + 1][j % s[k]] += dp[i][j]);
          if (dp[i + 1][j % s[k]] > MOD)
            dp[i + 1][j % s[k]] -= MOD;
          cnt--;
        } else {
          break;
        }
      }
      (dp[i + 1][j] += dp[i][j] * cnt) %= MOD;
    }
  }
  LL ans = 0;
  for (int i = 0; i <= x; i++) {
    ans = (ans + dp[n][i] * i) % MOD;
  }
  cout << ans << endl;
}