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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PI 3.1415926535897
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const double EPS = 1e-10;

int main(void) {
  std::cout << std::fixed << std::setprecision(10);
  ll W, H, x, y;
  cin >> W >> H >> x >> y;

  printf("%llf %d\n", (double)W * (double)H / 2, (x + x == W) && (y + y == H));
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
#define PI 3.1415926535897
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const ll LINF = 1000000000000000000; // 1e18
const double EPS = 1e-10;

int main(void) {
  std::cout << std::fixed << std::setprecision(10);
  ll W, H, x, y;
  cin >> W >> H >> x >> y;

  printf("%lf %d\n", (double)W * (double)H / 2, (x + x == W) && (y + y == H));
  return (0);
}
Problem p03001 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  long W, H, x, y;
  cin >> W >> H >> x >> y;
  cout << fixed << (double(W) * double(H) / 2) << " "
       << ((H % y == 0 && W % x == 0) ? 1 : 0) << endl;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  long W, H, x, y;
  cin >> W >> H >> x >> y;
  cout << ((double)W * (double)H / 2.0) << " "
       << ((x + x == W && y + y == H) ? 1 : 0) << endl;
}
Problem p03001 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define ll long long
int main() {
  ll w, h, x, y;
  double ans1 = 0;
  scanf("%lld%lld%lld%lld", &w, &h, &x, &y);
  ans1 = (double)((w * h) / 2.0);
  if (w % x == 0 && h % y == 0) {
    printf("%.6lf 1\n", ans1);
  } else {
    printf("%.6lf 0\n", ans1);
  }
}


Fixed submission:
#include <stdio.h>
#define ll long long
int main() {
  ll w, h, x, y;
  double ans1 = 0;
  scanf("%lld%lld%lld%lld", &w, &h, &x, &y);
  ans1 = (double)((w * h) / 2.0);
  if (w == 2 * x && h == 2 * y) {
    printf("%.6lf 1\n", ans1);
  } else {
    printf("%.6lf 0\n", ans1);
  }
}
Problem p03001 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
constexpr int kMod = 1000000007;
typedef long long LL;

int main() {
  LL W, H, x, y;
  std::cin >> W >> H >> x >> y;

  LL X = (y < H - y) ? y * W : (H - y) * W;
  LL Y = (x < W - x) ? x * H : (W - x) * H;

  if (y == H - y && x == W - x) {
    double ans = y * W;
    printf("%.9f 1\n", ans);
    assert(false);
  } else {
    double ans = std::max(X, Y);
    printf("%.9f 0\n", ans);
  }
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
constexpr int kMod = 1000000007;
typedef long long LL;

int main() {
  LL W, H, x, y;
  std::cin >> W >> H >> x >> y;
  double ans = (double)(W * H) / 2;
  bool b = ((x == W - x) && (y == H - y));
  printf("%lf %d\n", ans, b);
}
Problem p03001 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int N, X;
  int L[100];
  L[0] = 0;
  cin >> N >> X;
  for (int i = 1; i <= N; i++) {
    cin >> L[i];
    L[i] += L[i - 1];
  }
  bool a = false;
  for (int i = 0; i <= N; i++) {
    if (X < L[i]) {
      cout << i << endl;
      a = true;
      break;
    }
  }
  if (!a) {
    cout << N + 1 << endl;
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  double W, H, x, y;
  cin >> W >> H >> x >> y;
  cout << W * H / 2.0 << " " << (x * 2 == W && y * 2 == H) << endl;
}
Problem p03001 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif

  double W, H, x, y;

  cin >> W >> H >> x >> y;

  cout << fixed;
  cout << setprecision(10);

  double area = (W * H) / 2;
  double midx = W / 2;
  double midy = H / 2;

  if (midx == x && midy == y)
    cout << area << " " << 1 << endl;

  else
    cout << area << " " << 0 << endl;

  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  double W, H, x, y;

  cin >> W >> H >> x >> y;

  cout << fixed;
  cout << setprecision(10);

  double area = (W * H) / 2;
  double midx = W / 2;
  double midy = H / 2;

  if (midx == x && midy == y)
    cout << area << " " << 1 << endl;

  else
    cout << area << " " << 0 << endl;

  return 0;
}
Problem p03001 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x, i;
  cin >> n >> x;
  long long int arr[n];
  for (i = 0; i < n; i++)
    cin >> arr[i];
  long long int d = 0;
  i = 0;
  long long int count = 0;
  do {
    count++;
    d = d + arr[i];
    i++;
  } while (d <= x && count <= n + 1);
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  double w, h, x, y, area;
  cin >> w >> h >> x >> y;
  cout << (w * h) / 2;
  if (x == w / 2 && y == h / 2) {
    cout << " 1" << endl;
  } else
    cout << " 0" << endl;

  return 0;
}
Problem p03001 (C++)

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

int main() {
  i64 w, h, x, y;
  cin >> w >> h >> x >> y;
  if ((x == 0 && y == 0) || (x == w && y == 0) || (x == w && y == h) ||
      (x == 0 && y == h))
    return -1; // cout << fixed << setprecision(10) << (double)w * h / 2 << " 0"
               // << endl;
  else if (x * 2 == w || y * 2 == h)
    return -1; // cout << fixed << setprecision(10) << (double)w * h / 2 << (x
               // == 0 || x == h || y == 0 || y == w ? " 0" : " 1") << endl;
  else
    cout << fixed << setprecision(10)
         << max(min(w - x, x) * h, min(h - y, y) * w) << " 0" << endl;
  return 0;
}


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

int main() {
  i64 w, h, x, y;
  cin >> w >> h >> x >> y;
  cout << fixed << setprecision(10) << (double)w * h / 2 << " "
       << (x * 2 == w && y * 2 == h ? 1 : 0) << endl;
  return 0;
}
Problem p03001 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>   // abs() for float, and fabs()
#include <cstdlib> // abs() for integer 絶対値求めやつ
#include <iomanip> //浮動小数点数を出力する制度の指定
#include <limits>
#include <numeric>
#include <string>
#include <type_traits>
#include <vector>
// 使い方　数値をnumとして　cout<<setprecision(n)<<num ;nは桁数
#define rep(i, n) for (int i = 0; i < (n); i++)
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
using namespace std;
using ll = long long;
#define INF 2000000000
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define che(a, string) cout << "//" << string << "==" << (a) << "//" << endl;
bool IsInt(double a) {
  int b = a / 1;
  if (a == b) {
    return true;
  } else {
    return false;
  }
}
/*覚えてないことメaモ
  数値型から文字列に変換　to_string(number);これは簡単
  文字列から数値型に変換　stoi(number)
  文字列からllに変換　stoll(number)*/

// ここから書き始める
// 実験

int main() {
  ll w, h, x, y;
  cin >> w >> h >> x >> y;
  // 正確にxがwの半分 y がhの半分なら複数ある。
  int way = 0;
  // wとhの半分を正確にとる それって、w/xが2.0になっればいいんじゃね？

  if ((w / x) == 2 && w % x == 0 && (h / y) == 0 && h % y == 0) {
    way = 1;
  }
  cout << 1. * (w * h) / 2. << " " << way << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>   // abs() for float, and fabs()
#include <cstdlib> // abs() for integer 絶対値求めやつ
#include <iomanip> //浮動小数点数を出力する制度の指定
#include <limits>
#include <numeric>
#include <string>
#include <type_traits>
#include <vector>
// 使い方　数値をnumとして　cout<<setprecision(n)<<num ;nは桁数
#define rep(i, n) for (int i = 0; i < (n); i++)
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
using namespace std;
using ll = long long;
#define INF 2000000000
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define che(a, string) cout << "//" << string << "==" << (a) << "//" << endl;
bool IsInt(double a) {
  int b = a / 1;
  if (a == b) {
    return true;
  } else {
    return false;
  }
}
/*覚えてないことメaモ
  数値型から文字列に変換　to_string(number);これは簡単
  文字列から数値型に変換　stoi(number)
  文字列からllに変換　stoll(number)*/

// ここから書き始める
// 実験

int main() {
  ll w, h, x, y;
  cin >> w >> h >> x >> y;
  // 正確にxがwの半分 y がhの半分なら複数ある。
  int way = 0;
  // wとhの半分を正確にとる それって、w/xが2.0になっればいいんじゃね？

  if (x != 0 && y != 0) {
    if ((w / x) == 2 && w % x == 0 && (h / y) == 2 && h % y == 0) {
      way = 1;
    }
  }
  cout << 1. * (w * h) / 2. << " " << way << endl;
}
Problem p03003 (C++)

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

typedef long long int ll;

#define endl '\n'

ll n, m;
ll dp[1005][1005];
ll mod = (1e9) + 7;
int main() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;

  ll arr[n + 1];
  ll brr[m + 1];

  for (ll i = 1; i <= n; i++)
    cin >> arr[i];

  for (ll i = 1; i <= m; i++)
    cin >> brr[i];

  memset(dp, 0, sizeof(dp));
  /*for(ll i=0;i<=n;i++){
      dp[i][0]=1;
  }
  for(ll j=0;j<=m;j++){
      dp[0][j]=1;
  }*/
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (arr[i] == brr[j]) {
        dp[i][j] = (1 + dp[i][j - 1] + dp[i - 1][j] + mod) % mod;
      } else {
        dp[i][j] =
            (-dp[i - 1][j - 1] + dp[i][j - 1] + dp[i - 1][j] + mod) % mod;
      }
    }
  }

  cout << (dp[n][m] + 1) % mod << endl;
}


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

typedef long long int ll;

#define endl '\n'

ll n, m;
ll dp[2005][2005];
ll mod = (1e9) + 7;
int main() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;

  ll arr[n + 1];
  ll brr[m + 1];

  for (ll i = 1; i <= n; i++)
    cin >> arr[i];

  for (ll i = 1; i <= m; i++)
    cin >> brr[i];

  memset(dp, 0, sizeof(dp));
  /*for(ll i=0;i<=n;i++){
      dp[i][0]=1;
  }
  for(ll j=0;j<=m;j++){
      dp[0][j]=1;
  }*/
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (arr[i] == brr[j]) {
        dp[i][j] = (1 + dp[i][j - 1] + dp[i - 1][j] + mod) % mod;
      } else {
        dp[i][j] =
            (-dp[i - 1][j - 1] + dp[i][j - 1] + dp[i - 1][j] + mod) % mod;
      }
    }
  }

  cout << (dp[n][m] + 1) % mod << endl;
}
Problem p03003 (C++)

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

#define F first
#define S second
using namespace std;
typedef long long ll;
typedef long double ld;

const int mod = 1000000007;
const int max_n = 1011;

int main() {
  int n, a[max_n], m, b[max_n];
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", &b[i]);
  }
  int dp[max_n][max_n];
  for (int i = 0; i <= max(n, m); ++i) {
    dp[i][0] = 1;
    dp[0][i] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    for (int q = 1; q <= m; ++q) {
      dp[i][q] = 0;
      if (i)
        dp[i][q] += dp[i - 1][q];
      if (q)
        dp[i][q] += dp[i][q - 1];
      if (i && q && a[i - 1] != b[q - 1])
        dp[i][q] -= dp[i - 1][q - 1];
      if (dp[i][q] < 0)
        dp[i][q] += mod;
      dp[i][q] %= mod;
      /*if (a[i - 1] == b[q - 1]) {
        dp[i][q] = dp[i - 1][q - 1] * 2 % mod;
      } else {
        dp[i][q] = 0;
        if (i) dp[i][q] += dp[i - 1][q];
        if (q) dp[i][q] += dp[i][q - 1];
        if (i && q) dp[i][q] -= dp[i - 1][q - 1];
        if (dp[i][q] < 0) dp[i][q] += mod;
        dp[i][q] %= mod;
      }*/
      // cout << i << " " << q << " " << dp[i][q] << endl;
    }
  }
  cout << dp[n][m] << endl;
}

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

#define F first
#define S second
using namespace std;
typedef long long ll;
typedef long double ld;

const int mod = 1000000007;
const int max_n = 2011;

int main() {
  int n, a[max_n], m, b[max_n];
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", &b[i]);
  }
  int dp[max_n][max_n];
  for (int i = 0; i <= max(n, m); ++i) {
    dp[i][0] = 1;
    dp[0][i] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    for (int q = 1; q <= m; ++q) {
      dp[i][q] = 0;
      if (i)
        dp[i][q] += dp[i - 1][q];
      if (q)
        dp[i][q] += dp[i][q - 1];
      if (i && q && a[i - 1] != b[q - 1])
        dp[i][q] -= dp[i - 1][q - 1];
      if (dp[i][q] < 0)
        dp[i][q] += mod;
      dp[i][q] %= mod;
      /*if (a[i - 1] == b[q - 1]) {
        dp[i][q] = dp[i - 1][q - 1] * 2 % mod;
      } else {
        dp[i][q] = 0;
        if (i) dp[i][q] += dp[i - 1][q];
        if (q) dp[i][q] += dp[i][q - 1];
        if (i && q) dp[i][q] -= dp[i - 1][q - 1];
        if (dp[i][q] < 0) dp[i][q] += mod;
        dp[i][q] %= mod;
      }*/
      // cout << i << " " << q << " " << dp[i][q] << endl;
    }
  }
  cout << dp[n][m] << endl;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  std::vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &b[i]);

  const int MOD = 1e9 + 7;

  std::vector<i64> s(m + 1, 1);
  s[0] = 0;
  auto dp = make_v<i64>(n + 1, m + 1);
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j])
        (dp[i + 1][j + 1] += s[j + 1]) %= MOD;
    }
    i64 tmp = 0;
    for (int j = 0; j <= m; j++) {
      (s[j] += tmp) %= MOD;
      (tmp += dp[i + 1][j]) %= MOD;
    }
  }

  i64 ans = 0;
  for (auto v : dp)
    for (auto w : v)
      (ans += w) %= MOD;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  std::vector<int> a(n), b(m);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &b[i]);

  const int MOD = 1e9 + 7;

  std::vector<i64> s(m + 1, 1);
  s[0] = 0;
  auto dp = make_v<i64>(n + 1, m + 1);
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j])
        (dp[i + 1][j + 1] += s[j + 1]) %= MOD;
    }
    i64 tmp = 0;
    for (int j = 0; j <= m; j++) {
      (s[j] += tmp) %= MOD;
      (tmp += dp[i + 1][j]) %= MOD;
    }
  }

  i64 ans = 0;
  for (auto v : dp)
    for (auto w : v)
      (ans += w) %= MOD;
  printf("%lld\n", ans);
  return 0;
}
Problem p03003 (C++)

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

long long MOD = 1000000007;

int main() {
  // input
  long long N, M;
  cin >> N >> M;
  long long S[N + 1], T[N + 1];
  long long i, j;
  for (i = 1; i <= N; i++) {
    cin >> S[i];
  }
  for (j = 1; j <= M; j++) {
    cin >> T[j];
  }

  // calc
  long long sum[N + 1][M + 1];
  for (i = 0; i <= N; i++) {
    sum[i][0] = 1;
  }
  for (j = 0; j <= M; j++) {
    sum[0][j] = 1;
  }
  for (i = 1; i <= N; i++) {
    for (j = 1; j <= M; j++) {
      if (S[i] == T[j]) {
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1];
        sum[i][j] %= MOD;
      } else {
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + MOD - sum[i - 1][j - 1];
        sum[i][j] %= MOD;
      }
    }
  }

  // answer
  cout << sum[N][M] << endl;
  system("pause");
  return 0;
}

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

long long MOD = 1000000007;

int main() {
  // input
  long long N, M;
  cin >> N >> M;
  long long S[N + 1], T[M + 1];
  long long i, j;
  for (i = 1; i <= N; i++) {
    cin >> S[i];
  }
  for (j = 1; j <= M; j++) {
    cin >> T[j];
  }

  // calc
  long long sum[N + 1][M + 1];
  for (i = 0; i <= N; i++) {
    sum[i][0] = 1;
  }
  for (j = 0; j <= M; j++) {
    sum[0][j] = 1;
  }
  for (i = 1; i <= N; i++) {
    for (j = 1; j <= M; j++) {
      if (S[i] == T[j]) {
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1];
        sum[i][j] %= MOD;
      } else {
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + MOD - sum[i - 1][j - 1];
        sum[i][j] %= MOD;
      }
    }
  }

  // answer
  cout << sum[N][M] << endl;
  system("pause");
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

constexpr long long MOD = 1e9 + 7;
int s[1010], t[1010];
long long dp[1010][1010];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> s[i];
  for (int j = 1; j <= m; ++j)
    cin >> t[j];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      dp[i][j] %= MOD;
      if (s[i] == t[j]) {
        dp[i][j] += dp[i - 1][j - 1] + 1;
        dp[i][j] %= MOD;
      }
    }
  }
  if (dp[n][m] < 0)
    dp[n][m] += MOD;
  cout << (dp[n][m] + 1) % MOD << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

constexpr long long MOD = 1e9 + 7;
int s[2010], t[2010];
long long dp[2010][2010];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> s[i];
  for (int j = 1; j <= m; ++j)
    cin >> t[j];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      dp[i][j] %= MOD;
      if (s[i] == t[j]) {
        dp[i][j] += dp[i - 1][j - 1] + 1;
        dp[i][j] %= MOD;
      }
    }
  }
  if (dp[n][m] < 0)
    dp[n][m] += MOD;
  cout << (dp[n][m] + 1) % MOD << endl;
  return 0;
}
Problem p03003 (Python)

Buggy submission (status: Runtime Error):
import itertools

N, M = [int(_) for _ in input().split()]
S = [int(_) for _ in input().split()]
T = [int(_) for _ in input().split()]
mod = 10**9 + 7
dp = [[1] * (N + 1)] + [[1] + [0] * M for _ in range(N)]
for n, m in itertools.product(range(N), range(M)):
    dp[n + 1][m + 1] = dp[n][m + 1] + dp[n + 1][m] - dp[n][m]
    if S[n] == T[m]:
        dp[n + 1][m + 1] += dp[n][m]
    dp[n + 1][m + 1] %= mod
print(dp[-1][-1])


Fixed submission:
import itertools

N, M = [int(_) for _ in input().split()]
S = [int(_) for _ in input().split()]
T = [int(_) for _ in input().split()]
mod = 10**9 + 7
dp = [[1] * (M + 1)] + [[1] + [0] * M for _ in range(N)]
for n, m in itertools.product(range(N), range(M)):
    dp[n + 1][m + 1] = dp[n][m + 1] + dp[n + 1][m] - dp[n][m]
    if S[n] == T[m]:
        dp[n + 1][m + 1] += dp[n][m]
    dp[n + 1][m + 1] %= mod
print(dp[-1][-1])
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define P 1000000007
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> S(N), T(M);
  for (long long i = 0; i < N; i++)
    cin >> S[i];
  for (long long i = 0; i < M; i++)
    cin >> T[i];

  vector<vector<long long>> DP(N, vector<long long>(M, 0));
  for (long long s = 0; s < N; s++) {
    for (long long t = 0; t < M; t++) {
      if (s == 0 && t == 0) {
        DP[s][t] = (S[s] == T[t]) + 1;
      } else if (s == 0) {
        DP[s][t] = ((S[s] == T[t]) + DP[s][t - 1]) % P;
      } else if (t == 0) {
        DP[s][t] = ((S[s] == T[t]) + DP[s - 1][t]) % P;
      } else {
        long long a = ((S[s] == T[t]) * (DP[s - 1][t - 1])) % P;
        long long b =
            ((DP[s - 1][t] - DP[s - 1][t - 1]) % P + DP[s][t - 1]) % P;
        DP[s][t] = (a + b) % P;
      }
    }
  }
  assert(DP[N - 1][M - 1] > 0);
  cout << DP[N - 1][M - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define P 1000000007
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> S(N), T(M);
  for (long long i = 0; i < N; i++)
    cin >> S[i];
  for (long long i = 0; i < M; i++)
    cin >> T[i];

  vector<vector<long long>> DP(N, vector<long long>(M, 0));
  for (long long s = 0; s < N; s++) {
    for (long long t = 0; t < M; t++) {
      if (s == 0 && t == 0) {
        DP[s][t] = (S[s] == T[t]) + 1;
      } else if (s == 0) {
        DP[s][t] = ((S[s] == T[t]) + DP[s][t - 1]) % P;
      } else if (t == 0) {
        DP[s][t] = ((S[s] == T[t]) + DP[s - 1][t]) % P;
      } else {
        long long a = ((S[s] == T[t]) * (DP[s - 1][t - 1])) % P;
        long long b =
            ((DP[s - 1][t] - DP[s - 1][t - 1]) % P + DP[s][t - 1]) % P;
        DP[s][t] = (a + b) % P;
      }
    }
  }
  while (DP[N - 1][M - 1] < 0)
    DP[N - 1][M - 1] += P;
  assert(DP[N - 1][M - 1] >= 0);
  cout << DP[N - 1][M - 1];
}
Problem p03003 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define MOD 1000000007;

ll dp[2020][2020];
ll ac[2001][100001];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, 2020) rep(j, 2020) dp[i][j] = 0;
  rep(i, n + 1) dp[i][0] = 1;
  rep(i, m + 1) dp[0][i] = 1;
  vector<long long int> s(n), t(m);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++)
    cin >> t[i];
  for (int i = 1; i < n + 1; i++)
    for (int j = 0; j < 100001; j++)
      ac[i][j] = 0;
  for (int j = 0; j < 100001; j++)
    ac[0][j] = 0;

  for (int j = 1; j <= m; j++) {
    for (int i = 0; i <= n; i++)
      ac[i][t[j - 1]] = (ac[i][t[j - 1]] + dp[i][j - 1]) % MOD;
    for (int i = 1; i <= n; i++) {
      ll sum = 0;
      // for(int k=j-1;k>=0;k--) if(s[i-1]==t[k]) sum = (dp[i-1][k] + sum) %
      // MOD;
      sum = (sum + ac[i - 1][s[i - 1]]) % MOD;
      //   cout << ac[i-1][s[i-1]] << endl;
      dp[i][j] = (dp[i - 1][j] + sum) % MOD;
    }
  }
  rep(i, n + 1) {
    //  rep(j,m+1) cout << dp[i][j] << " ";
    //    cout << endl;
  }
  cout << dp[n][m] << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define MOD 1000000007;

ll dp[2020][2020];
int ac[2001][100001];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, 2020) rep(j, 2020) dp[i][j] = 0;
  rep(i, n + 1) dp[i][0] = 1;
  rep(i, m + 1) dp[0][i] = 1;
  vector<long long int> s(n), t(m);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < m; i++)
    cin >> t[i];
  for (int i = 1; i < n + 1; i++)
    for (int j = 0; j < 100001; j++)
      ac[i][j] = 0;
  for (int j = 0; j < 100001; j++)
    ac[0][j] = 0;

  for (int j = 1; j <= m; j++) {
    for (int i = 0; i <= n; i++)
      ac[i][t[j - 1]] = (ac[i][t[j - 1]] + dp[i][j - 1]) % MOD;
    for (int i = 1; i <= n; i++) {
      ll sum = 0;
      // for(int k=j-1;k>=0;k--) if(s[i-1]==t[k]) sum = (dp[i-1][k] + sum) %
      // MOD;
      sum = (sum + ac[i - 1][s[i - 1]]) % MOD;
      //   cout << ac[i-1][s[i-1]] << endl;
      dp[i][j] = (dp[i - 1][j] + sum) % MOD;
    }
  }
  rep(i, n + 1) {
    //  rep(j,m+1) cout << dp[i][j] << " ";
    //    cout << endl;
  }
  cout << dp[n][m] << endl;
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define P pair<int, int>
#define LL long long
#define LD long double
#define PLL pair<LL, LL>
#define mset(a, b) memset(a, b, sizeof(a))
#define PI acos(-1.0)
#define random(x) rand() % x
using namespace std;
const int inf = 0x3f3f3f3f;
const LL __64inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = 1e5 + 5;
const int Mod = 1e9 + 7;

LL dp[1005][1005];
LL sum[1005][1005];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> A(n), B(m);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 0; i < m; i++)
    cin >> B[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (A[i - 1] == B[j - 1])
        dp[i][j] = sum[i - 1][j - 1] + 1;
      else
        dp[i][j] = 0;
      sum[i][j] =
          (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + dp[i][j]) % Mod;
      if (sum[i][j] < 0)
        sum[i][j] += Mod;
    }
  cout << sum[n][m] + 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define P pair<int, int>
#define LL long long
#define LD long double
#define PLL pair<LL, LL>
#define mset(a, b) memset(a, b, sizeof(a))
#define PI acos(-1.0)
#define random(x) rand() % x
using namespace std;
const int inf = 0x3f3f3f3f;
const LL __64inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = 1e5 + 5;
const int Mod = 1e9 + 7;

LL dp[2005][2005];
LL sum[2005][2005];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> A(n), B(m);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 0; i < m; i++)
    cin >> B[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (A[i - 1] == B[j - 1])
        dp[i][j] = sum[i - 1][j - 1] + 1;
      else
        dp[i][j] = 0;
      sum[i][j] =
          (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + dp[i][j]) % Mod;
      if (sum[i][j] < 0)
        sum[i][j] += Mod;
    }
  cout << sum[n][m] + 1 << endl;
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << -1 << endl;                                                          \
  return 0
#define Endl endl

signed main(void) {
  int N, M;
  const int MEM = 201;
  int S[MEM], T[MEM];
  int dp[MEM][MEM] = {};
  cin >> N >> M;
  REP(i, N) { cin >> S[i]; }
  REP(i, M) { cin >> T[i]; }
  dp[0][0] = 1;
  REP(i, N + 1) {
    REP(j, M + 1) {
      if (i > 0)
        dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD;
      if (j > 0)
        dp[i][j] = (dp[i][j] + dp[i][j - 1]) % MOD;
      if (i > 0 && j > 0 && S[i - 1] != T[j - 1])
        dp[i][j] = (dp[i][j] - dp[i - 1][j - 1] + MOD) % MOD;
    }
  }

  cout << dp[N][M] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << -1 << endl;                                                          \
  return 0
#define Endl endl

signed main(void) {
  int N, M;
  const int MEM = 2019;
  int S[MEM], T[MEM];
  int dp[MEM][MEM] = {};
  cin >> N >> M;
  REP(i, N) { cin >> S[i]; }
  REP(i, M) { cin >> T[i]; }
  dp[0][0] = 1;
  REP(i, N + 1) {
    REP(j, M + 1) {
      if (i > 0)
        dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD;
      if (j > 0)
        dp[i][j] = (dp[i][j] + dp[i][j - 1]) % MOD;
      if (i > 0 && j > 0 && S[i - 1] != T[j - 1])
        dp[i][j] = (dp[i][j] - dp[i - 1][j - 1] + MOD) % MOD;
    }
  }

  cout << dp[N][M] << endl;
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MOD 1000000007

using namespace std;

int s[1008], t[1008];
long long dp[1008][1008];
int n, m;

int main() {
  memset(dp, 0, sizeof(dp));

  int i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    cin >> s[i];
  for (i = 1; i <= m; i++)
    cin >> t[i];

  dp[0][0] = 1;
  for (i = 1; i <= n; i++)
    dp[i][0] = 1;
  for (j = 1; j <= m; j++)
    dp[0][j] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i] == t[j])
        dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
      else {
        dp[i][j] =
            ((dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % MOD + MOD) %
            MOD;
      }
    }
  }

  cout << dp[n][m];

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MOD 1000000007

using namespace std;

int s[2008], t[2008];
long long dp[2008][2008];
int n, m;

int main() {
  memset(dp, 0, sizeof(dp));

  int i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    cin >> s[i];
  for (i = 1; i <= m; i++)
    cin >> t[i];

  dp[0][0] = 1;
  for (i = 1; i <= n; i++)
    dp[i][0] = 1;
  for (j = 1; j <= m; j++)
    dp[0][j] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i] == t[j])
        dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
      else {
        dp[i][j] =
            ((dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % MOD + MOD) %
            MOD;
      }
    }
  }

  cout << dp[n][m];

  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e3 + 10;
const int MOD = 1e9 + 7;
int a[maxn], b[maxn];
LL dp[maxn][maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  for (int i = 0; i <= n; i++)
    dp[i][0] = 1;
  for (int i = 0; i <= m; i++)
    dp[0][i] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = ((dp[i - 1][j] + dp[i][j - 1]) % MOD - dp[i - 1][j - 1]) % MOD;
      if (a[i] == b[j]) {
        dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
      }
    }
  }
  assert(dp[n][m] > MOD);
  cout << ((dp[n][m] % MOD) + MOD) % MOD << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e3 + 10;
const int MOD = 1e9 + 7;
int a[maxn], b[maxn];
LL dp[maxn][maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  for (int i = 0; i <= n; i++)
    dp[i][0] = 1;
  for (int i = 0; i <= m; i++)
    dp[0][i] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = ((dp[i - 1][j] + dp[i][j - 1]) % MOD - dp[i - 1][j - 1]) % MOD;
      if (a[i] == b[j]) {
        dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
      }
    }
  }
  cout << ((dp[n][m] % MOD) + MOD) % MOD << endl;
}
Problem p03003 (C++)

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

using namespace std;
#define int long long int

constexpr int MOD = 1000000007; // 10 ** 9 + 7
constexpr int SIZE = 2010;

int dp[SIZE][SIZE];
int sum[SIZE][SIZE];

void solve() {
  int N, M;
  cin >> N >> M;

  vector<int> s(N);
  for (auto &x : s) {
    cin >> x;
  }
  vector<int> t(M);
  for (auto &x : t) {
    cin >> x;
  }

  for (int i = 0; i <= N; i++) {

    sum[i][0] = 1;
  }

  for (int i = 0; i <= M; i++) {

    sum[0][i] = 1;
  }

  int ret = 1;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = sum[i - 1][j - 1] + 1;
        ret += dp[i + 1][j + 1];
      }
      sum[i + 1][j + 1] =
          (sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + dp[i + 1][j + 1] + MOD) %
          MOD;
    }
  }
  cout << ret % MOD << endl;
}

signed main() {
  solve();
  return 0;
}


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

using namespace std;
#define int long long int

constexpr int MOD = 1000000007; // 10 ** 9 + 7
constexpr int SIZE = 2010;

int dp[SIZE][SIZE];
int sum[SIZE][SIZE];

void solve() {
  int N, M;
  cin >> N >> M;

  vector<int> s(N);
  for (auto &x : s) {
    cin >> x;
  }
  vector<int> t(M);
  for (auto &x : t) {
    cin >> x;
  }

  for (int i = 0; i <= N; i++) {

    sum[i][0] = 1;
  }

  for (int i = 0; i <= M; i++) {

    sum[0][i] = 1;
  }

  int ret = 1;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = sum[i][j];
        ret += dp[i + 1][j + 1];
      }
      sum[i + 1][j + 1] =
          (sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + dp[i + 1][j + 1] + MOD) %
          MOD;
    }
  }
  cout << ret % MOD << endl;
}

signed main() {
  solve();
  return 0;
}
Problem p03003 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
MOD = 10**9 + 7
dp = [[0] * (M + 2) for _ in range(N + 2)]
sdp = [[0] * (M + 2) for _ in range(N + 2)]
dp[0][0] = 1
sdp[1][1] = 1
for i in range(N + 1):
    for j in range(M + 1):
        if i - 1 >= 0 and j - 1 >= 0 and S[i - 1] == T[j - 1]:
            dp[i][j] = sdp[i][j]
        sdp[i + 1][j + 1] = sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j] + dp[i][j]
        sdp[i + 1][j + 1] %= MOD
        sdp[i + 1][j + 1] %= MOD
print(sdp[N + 1][M + 1])


Fixed submission:
# dp[i][j] SとTの最後の文字を採用するような文字列の個数。これにより各DPテーブルの値が排反になる。元の文字列の最後を必ず採用するので
N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
MOD = 10**9 + 7
dp = [[0] * (M + 2) for _ in range(N + 2)]
sdp = [[0] * (M + 2) for _ in range(N + 2)]
dp[0][0] = 1
sdp[1][1] = 1
for i in range(N + 1):
    for j in range(M + 1):
        if i - 1 >= 0 and j - 1 >= 0 and S[i - 1] == T[j - 1]:
            dp[i][j] = sdp[i][j]
        sdp[i + 1][j + 1] = sdp[i + 1][j] + sdp[i][j + 1] - sdp[i][j] + dp[i][j]
        sdp[i + 1][j + 1] %= MOD
        sdp[i + 1][j + 1] %= MOD
print(sdp[N + 1][M + 1])
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
//
// Created by keane on 19-6-16.
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 50;
const ll mod = 1e9 + 7;
int n, m;
int a[N], b[N];
ll dp[N][N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i] == b[j]) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + 1;
      } else {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      }
      dp[i][j] %= mod;
    }
  }
  printf("%lld\n", (dp[n][m] + 1 + mod) % mod);
}

Fixed submission:
//
// Created by keane on 19-6-16.
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e3 + 50;
const ll mod = 1e9 + 7;
int n, m;
int a[N], b[N];
ll dp[N][N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i] == b[j]) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + 1;
      } else {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      }
      dp[i][j] %= mod;
    }
  }
  printf("%lld\n", (dp[n][m] + 1 + mod) % mod);
}
Problem p03003 (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, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define repr(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define all(v) (v).begin(), (v).end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define pb push_back
#define MOD 1000000007
#define inf 1000000000
#define llinf 1000000000000000
#define MAX 1000000

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef vector<pint> vpint;

ll dp[2005][2005];

int main() {
  int n, m;
  cin >> n >> m;
  vint a(n), b(n);

  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  rep(i, n + 1) dp[i][0] = 1;
  rep(i, m + 1) dp[0][i] = 1;
  rep(i, n + 1) rep(j, m + 1) {
    if (i == 0 || j == 0) {
      continue;
    }
    // dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1])%MOD;
    if (a[i - 1] == b[j - 1]) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] + MOD) % MOD;
    } else {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
    }
  }
  cout << dp[n][m] << endl;
}


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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define repr(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define all(v) (v).begin(), (v).end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define pb push_back
#define MOD 1000000007
#define inf 1000000000
#define llinf 1000000000000000
#define MAX 1000000

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef vector<pint> vpint;

ll dp[2005][2005];

int main() {
  int n, m;
  cin >> n >> m;
  vll a(n), b(m);

  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  rep(i, n + 1) dp[i][0] = 1;
  rep(i, m + 1) dp[0][i] = 1;
  rep(i, n + 1) rep(j, m + 1) {
    if (i == 0 || j == 0) {
      continue;
    }
    // dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1])%MOD;
    if (a[i - 1] == b[j - 1]) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] + MOD) % MOD;
    } else {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
    }
  }
  cout << dp[n][m] << endl;
}
Problem p03003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define INF 1LL << 60

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  ll mod = 1e9 + 7;
  int N, M;
  cin >> N >> M;
  vector<int> S(N), T(M);
  REP(i, N) cin >> S[i];
  REP(i, M) cin >> T[i];

  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, 1));

  FOR(i, 1, N)
  FOR(j, 1, M) {
    if (S[i - 1] == T[j - 1])
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % mod;
    else
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] + mod) % mod;
  }

  cerr << "debug" << endl;
  FOR(i, 0, N) {
    FOR(j, 0, M) { cerr << dp[i][j] << " "; }
    cerr << endl;
  }

  cout << dp[N][M] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define INF 1LL << 60

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  ll mod = 1e9 + 7;
  int N, M;
  cin >> N >> M;
  vector<int> S(N), T(M);
  REP(i, N) cin >> S[i];
  REP(i, M) cin >> T[i];

  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, 1));

  FOR(i, 1, N)
  FOR(j, 1, M) {
    if (S[i - 1] == T[j - 1])
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % mod;
    else
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] + mod) % mod;
  }

  // cerr << "debug" << endl;
  // FOR(i, 0, N) {
  //     FOR(j, 0, M) {
  //         cerr << dp[i][j] << " ";
  //     }
  //     cerr << endl;
  // }

  cout << dp[N][M] << endl;

  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
typedef long long int ll;

ll mod = 1e9 + 7;
ll dp[2010][2010], sum[2010][2010];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> s(n), t(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i) {
        (sum[i][j] += sum[i - 1][j]) %= mod;
      }
      if (j) {
        (sum[i][j] += sum[i][j - 1]) %= mod;
      }
      if (i && j) {
        (sum[i][j] -= sum[i - 1][j - 1] - mod) %= mod;
        dp[i][j] += sum[i - 1][j - 1];
      }
      dp[i][j]++;
      dp[i][j] %= mod;
      if (s[i] != t[j])
        dp[i][j] = 0;
      (sum[i][j] += dp[i][j]) %= mod;
    }
  }
  cout << (sum[n - 1][m - 1] + 1) % mod << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
typedef long long int ll;

ll mod = 1e9 + 7;
ll dp[2010][2010], sum[2010][2010];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> s(n), t(m);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i) {
        (sum[i][j] += sum[i - 1][j]) %= mod;
      }
      if (j) {
        (sum[i][j] += sum[i][j - 1]) %= mod;
      }
      if (i && j) {
        (sum[i][j] -= sum[i - 1][j - 1] - mod) %= mod;
        dp[i][j] += sum[i - 1][j - 1];
      }
      dp[i][j]++;
      dp[i][j] %= mod;
      if (s[i] != t[j])
        dp[i][j] = 0;
      (sum[i][j] += dp[i][j]) %= mod;
    }
  }
  cout << (sum[n - 1][m - 1] + 1) % mod << endl;
}
Problem p03003 (C++)

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

ll inf = 1e9 + 7;
ll n, m, s[2020], t[2020];
ll dp[2020][2020] = {};
vector<ll> s_list[100010], t_list[100010];

/*
ll solve(int k, int l) {
    if (dp[k][l]) return dp[k][l];

    if (k * l == 0) dp[k][l] = 1;
    else {

    }

    dp[k][l] %= inf;
    return dp[k][l];
}*/

int main(void) {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    s_list[s[i]].push_back(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }

  dp[0][0] = 1;
  for (int i = 0; i < n; i++)
    dp[i + 1][0] = 1;
  for (int j = 0; j < m; j++)
    dp[0][j + 1] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i + 1][j + 1] = dp[i + 1][j];
      for (int k = 0; k < s_list[t[j]].size() && s_list[t[j]][k] < i + 1; k++) {
        dp[i + 1][j + 1] += dp[s_list[t[j]][k]][j];
      }
      dp[i + 1][j + 1] %= inf;
    }
  }

  cout << dp[n][m] << endl;

  /*
  for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
          if (j != 0) cout << " ";
          cout << dp[i][j];
      }
      cout << endl;
  }*/

  // cout << solve(n, m) << endl;
}


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

ll inf = 1e9 + 7;
ll n, m, s[2020], t[2020];
ll dp[2020][2020] = {};
vector<ll> s_list[100010], t_list[100010];

/*
ll solve(int k, int l) {
    if (dp[k][l]) return dp[k][l];

    if (k * l == 0) dp[k][l] = 1;
    else {

    }

    dp[k][l] %= inf;
    return dp[k][l];
}*/

int main(void) {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    s_list[s[i]].push_back(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }

  dp[0][0] = 1;
  for (int i = 0; i < n; i++)
    dp[i + 1][0] = 1;
  for (int j = 0; j < m; j++)
    dp[0][j + 1] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1];
      else
        dp[i + 1][j + 1] = inf + dp[i + 1][j] + dp[i][j + 1] - dp[i][j];
      dp[i + 1][j + 1] %= inf;
    }
  }

  cout << dp[n][m] << endl;

  /*
  for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
          if (j != 0) cout << " ";
          cout << dp[i][j];
      }
      cout << endl;
  }*/

  // cout << solve(n, m) << endl;
}