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

Buggy submission (status: Runtime Error):
/*alurquiza*/
// S:Digit Sum
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7, SIZE = 1e4 + 1, MAXR = 100, CANTD = 10;
int DP[SIZE][CANTD][MAXR];

void sum(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  if (a < 0)
    a += MOD;
}

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

  freopen(".in", "r", stdin);
  //    freopen(".out","w",stdout);

  string K;
  int D;

  cin >> K >> D;

  int N = K.size();

  for (int i = 0; i < 10; i++)
    DP[0][i][0] = 1;

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

    /// DP[i][d][r] -> poniendo el digito d en la posicion i en cuantos numeros
    /// la suma de sus digitos es = r;
    for (int d = 0; d < 10; d++) {
      for (int r = 0; r < D; r++) {
        int newr = (r - (d % D) + D) % D;
        sum(DP[i][d][r], DP[i - 1][9][newr]);
      }
    }

    /// acumular la dinamica para todos los digitos con distancia r
    for (int d = 1; d < 10; d++) {
      for (int r = 0; r < D; r++) {
        sum(DP[i][d][r], DP[i][d - 1][r]);
      }
    }
  }

  int sol = 0, r = 0;
  for (int i = 0; i < N; i++) {
    int pos = N - i;

    int digit = K[i] - '0';

    if (digit) {
      sum(sol, DP[pos][digit - 1][r] % MOD);
      r = (r - (digit % D) + D) % D;
    }
  }

  /// A la solucion le quito el numero 0 y le sumo si el numero inicial cumple
  /// la condicion
  cout << (sol - 1 + !r + MOD) % MOD << '\n';

  return 0;
}


Fixed submission:
/*alurquiza*/
// S:Digit Sum
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7, SIZE = 1e4 + 1, MAXR = 100, CANTD = 10;
int DP[SIZE][CANTD][MAXR];

void sum(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  if (a < 0)
    a += MOD;
}

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

  //    freopen(".in","r",stdin);
  //    freopen(".out","w",stdout);

  string K;
  int D;

  cin >> K >> D;

  int N = K.size();

  for (int i = 0; i < 10; i++)
    DP[0][i][0] = 1;

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

    /// DP[i][d][r] -> poniendo el digito d en la posicion i en cuantos numeros
    /// la suma de sus digitos es = r;
    for (int d = 0; d < 10; d++) {
      for (int r = 0; r < D; r++) {
        int newr = (r - (d % D) + D) % D;
        sum(DP[i][d][r], DP[i - 1][9][newr]);
      }
    }

    /// acumular la dinamica para todos los digitos con distancia r
    for (int d = 1; d < 10; d++) {
      for (int r = 0; r < D; r++) {
        sum(DP[i][d][r], DP[i][d - 1][r]);
      }
    }
  }

  int sol = 0, r = 0;
  for (int i = 0; i < N; i++) {
    int pos = N - i;

    int digit = K[i] - '0';

    if (digit) {
      sum(sol, DP[pos][digit - 1][r] % MOD);
      r = (r - (digit % D) + D) % D;
    }
  }

  /// A la solucion le quito el numero 0 y le sumo si el numero inicial cumple
  /// la condicion
  cout << (sol - 1 + !r + MOD) % MOD << '\n';

  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
int dp[3001][102][2];
int countdigitsum(int pos, int d, bool tight, string k, int D) {
  int ub = tight ? k[pos] - '0' : 9;
  if (dp[pos][d][tight] != -1) {
    return dp[pos][d][tight];
  }
  if (pos == k.length() - 1) {
    int ans = 0;
    for (int x = 0; x <= ub; x++) {
      if (x % D == d) {
        ans++;
      }
    }
    return ans;
  }
  int ans = 0;
  for (int x = 0; x <= ub; x++) {
    ans = (ans + countdigitsum(pos + 1, (D + d - x % D) % D, tight && (x == ub),
                               k, D)) %
          mod;
  }
  return dp[pos][d][tight] = ans;
}
signed main() {
  string k;
  cin >> k;
  int D;
  cin >> D;
  memset(dp, -1, sizeof dp);

  cout << (mod + countdigitsum(0, 0, 1, k, D) - 1) % mod << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
int dp[30001][1002][2];
int countdigitsum(int pos, int d, bool tight, string k, int D) {
  int ub = tight ? k[pos] - '0' : 9;
  if (dp[pos][d][tight] != -1) {
    return dp[pos][d][tight];
  }
  if (pos == k.length() - 1) {
    int ans = 0;
    for (int x = 0; x <= ub; x++) {
      if (x % D == d) {
        ans++;
      }
    }
    return ans;
  }
  int ans = 0;
  for (int x = 0; x <= ub; x++) {
    ans = (ans + countdigitsum(pos + 1, (D + d - x % D) % D, tight && (x == ub),
                               k, D)) %
          mod;
  }
  return dp[pos][d][tight] = ans;
}
signed main() {
  string k;
  cin >> k;
  int D;
  cin >> D;
  memset(dp, -1, sizeof dp);

  cout << (mod + countdigitsum(0, 0, 1, k, D) - 1) % mod << endl;
  return 0;
}
Problem p03178 (C++)

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

using namespace std;

#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
#define double long double
#define endl '\n'

const int mod = (int)1e9 + 7;
int n, D;
vector<int> a;
int cache[10001][101][2];

int solve(int i, int d, int small) {
  if (i >= n) {
    return d == 0;
  }
  if (cache[i][d][small] != -1) {
    return cache[i][d][small];
  }
  int res = 0;
  for (int dig = 0; dig <= (small ? 9 : a[i]); dig++) {
    res += solve(i + 1, (d + dig) % D, small | (dig < a[i]));
    if (res >= mod) {
      res -= mod;
    }
  }
  return cache[i][d][small] = res;
}

signed main() {
  fio;
  string s;
  cin >> s >> D;
  n = (int)s.length();
  a.resize(n);
  for (int i = 0; i < n; i++) {
    a[i] = s[i] - '0';
  }

  memset(cache, -1, sizeof(cache));
  int answer = solve(0, 0, 0) - 1;
  assert(answer >= 0);
  cout << answer << endl;

  return 0;
}


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

using namespace std;

#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
#define double long double
#define endl '\n'

const int mod = (int)1e9 + 7;
int n, D;
vector<int> a;
int cache[10001][101][2];

int solve(int i, int d, int small) {
  if (i >= n) {
    return d == 0;
  }
  if (cache[i][d][small] != -1) {
    return cache[i][d][small];
  }
  int res = 0;
  for (int dig = 0; dig <= (small ? 9 : a[i]); dig++) {
    res += solve(i + 1, (d + dig) % D, small | (dig < a[i]));
    if (res >= mod) {
      res -= mod;
    }
  }
  return cache[i][d][small] = res;
}

signed main() {
  fio;
  string s;
  cin >> s >> D;
  n = (int)s.length();
  a.resize(n);
  for (int i = 0; i < n; i++) {
    a[i] = s[i] - '0';
  }

  memset(cache, -1, sizeof(cache));
  int answer = solve(0, 0, 0) - 1;
  if (answer < 0) {
    answer += mod;
  }
  cout << answer << endl;

  return 0;
}
Problem p03178 (C++)

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

using namespace std;

const int N = 1e4 + 2;
const int mod = 1e9 + 7;

int d, n, ans, dp[105][N], pref[N];
string s;

int main() {
  cin >> s >> d;

  n = s.size();
  s = ' ' + s;

  for (int i = 1; i <= n; i++) {
    pref[i] = pref[i - 1] + s[i] - '0';
  }

  dp[0][0] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 9; j++) {
      if (i == 1 && !j)
        continue;
      for (int k = 0; k < d; k++) {
        int to = (k + j) % d;
        dp[i][to] = (dp[i - 1][k] + dp[i][to]) % mod;
      }
    }
    ans = (ans + dp[i][0]) % mod;
  }

  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k < d; k++) {
        int to = (k + j) % d;
        dp[i][to] = (dp[i - 1][k] + dp[i][to]) % mod;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < s[i] - '0'; j++) {
      if (i == 1 && !j)
        continue;
      int sum = (pref[i - 1] + j) % d;
      ans = (ans + dp[n - i][(d - sum + d) % d]) % mod;
    }
  }

  if (pref[n] % d == 0)
    ans = (ans + 1) % mod;

  cout << ans << endl;
}


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

using namespace std;

const int N = 1e4 + 2;
const int mod = 1e9 + 7;

int d, n, ans, dp[N][105], pref[N];
string s;

int main() {
  cin >> s >> d;

  n = s.size();
  s = ' ' + s;

  for (int i = 1; i <= n; i++) {
    pref[i] = pref[i - 1] + s[i] - '0';
  }

  dp[0][0] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 9; j++) {
      if (i == 1 && !j)
        continue;
      for (int k = 0; k < d; k++) {
        int to = (k + j) % d;
        dp[i][to] = (dp[i - 1][k] + dp[i][to]) % mod;
      }
    }
    ans = (ans + dp[i][0]) % mod;
  }

  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k < d; k++) {
        int to = (k + j) % d;
        dp[i][to] = (dp[i - 1][k] + dp[i][to]) % mod;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < s[i] - '0'; j++) {
      if (i == 1 && !j)
        continue;
      int sum = (pref[i - 1] + j) % d;
      ans = (ans + dp[n - i][(d - sum + d) % d]) % mod;
    }
  }

  if (pref[n] % d == 0)
    ans = (ans + 1) % mod;

  cout << ans << endl;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define vll vector<ll>
#define M 100011
#define MOD 1000000007
ll md = MOD;
string s1;

ll a[1011];
ll dp[1011][110][2];
ll f(ll x, ll mod, ll k, ll m, ll sum) {
  if (x < 0) {
    if (mod == 0) {
      return 1;
    }
    return 0;
  }
  if (dp[x][mod][k] != -1 && k == 0)
    return dp[x][mod][k];
  ll mx = 9;
  if (k == 1)
    mx = a[x];
  ll ans = 0;
  for (ll i = 0; i <= mx; ++i) {
    ll nc = 0;
    if (i == a[x])
      nc = k;
    ans += f(x - 1, (mod + i) % m, nc, m, 10 * sum + i) % md;
    ans %= md;
  }
  dp[x][mod][k] = ans % md;
  return ans % md;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t, i, j, m, x, y, n, z, p, k, T;
  string s2, s3;
  x = 1;
  cin >> s1 >> m;
  n = s1.size();
  reverse(s1.begin(), s1.end());
  memset(dp, -1, sizeof(dp));
  for (i = 0; i < n; ++i)
    a[i] = s1[i] - '0';
  cout << (f(n - 1, 0, 1, m, 0) % md - 1 + md) % md;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define vll vector<ll>
#define M 100011
#define MOD 1000000007
ll md = MOD;
string s1;

ll a[10110];
ll dp[10110][110][2];
ll f(ll x, ll mod, ll k, ll m, ll sum) {
  if (x < 0) {
    if (mod == 0) {
      return 1;
    }
    return 0;
  }
  if (dp[x][mod][k] != -1 && k == 0)
    return dp[x][mod][k];
  ll mx = 9;
  if (k == 1)
    mx = a[x];
  ll ans = 0;
  for (ll i = 0; i <= mx; ++i) {
    ll nc = 0;
    if (i == a[x])
      nc = k;
    ans += f(x - 1, (mod + i) % m, nc, m, 10 * sum + i) % md;
    ans %= md;
  }
  dp[x][mod][k] = ans % md;
  return ans % md;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t, i, j, m, x, y, n, z, p, k, T;
  string s2, s3;
  x = 1;
  cin >> s1 >> m;
  n = s1.size();
  reverse(s1.begin(), s1.end());
  memset(dp, -1, sizeof(dp));
  for (i = 0; i < n; ++i)
    a[i] = s1[i] - '0';
  cout << (f(n - 1, 0, 1, m, 0) % md - 1 + md) % md;

  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

ll dp[105][101][2];
const ll mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int D;
  cin >> D;

  dp[0][0][0] = 1;
  rep(i, n) rep(j, D) rep(k, 2) {
    ll nd = s[i] - '0';
    rep(d, 10) {
      int ni = i + 1, nj = (j + d) % D, nk = k;
      if (k == 0) {
        if (d > nd)
          continue;
        if (d < nd)
          nk = 1;
      }
      dp[ni][nj][nk] += dp[i][j][k];
      dp[ni][nj][nk] %= mod;
    }
  }
  int ans = dp[n][0][0] + dp[n][0][1];
  ans -= 1;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

ll dp[10001][101][2];
const ll mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int D;
  cin >> D;

  dp[0][0][0] = 1;
  rep(i, n) rep(j, D) rep(k, 2) {
    ll nd = s[i] - '0';
    rep(d, 10) {
      int ni = i + 1, nj = (j + d) % D, nk = k;
      if (k == 0) {
        if (d > nd)
          continue;
        if (d < nd)
          nk = 1;
      }
      dp[ni][nj][nk] += dp[i][j][k];
      dp[ni][nj][nk] %= mod;
    }
  }
  int ans = dp[n][0][0] + dp[n][0][1];
  ans -= 1;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;

  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf = 1 << 20;
const int mod = 1e9 + 7;

ll dp[1005][2][105];
int ctoi(char c) { return c - '0'; }
int main() {
  string K;
  int D;
  cin >> K >> D;

  dp[0][0][0] = 1;
  rep(i, 0, K.size()) rep(j, 0, D) {
    int k = ctoi(K[i]);
    rep(digit, 0, 10)(dp[i + 1][1][(j + digit) % D] += dp[i][1][j]) %= mod;
    rep(digit, 0, k)(dp[i + 1][1][(j + digit) % D] += dp[i][0][j]) %= mod;
    (dp[i + 1][0][(j + k) % D] += dp[i][0][j]) %= mod;
  }
  int ans = (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + mod) % mod;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf = 1 << 20;
const int mod = 1e9 + 7;

ll dp[100005][2][105];
int ctoi(char c) { return c - '0'; }
int main() {
  string K;
  int D;
  cin >> K >> D;

  dp[0][0][0] = 1;
  rep(i, 0, K.size()) rep(j, 0, D) {
    int k = ctoi(K[i]);
    rep(digit, 0, 10)(dp[i + 1][1][(j + digit) % D] += dp[i][1][j]) %= mod;
    rep(digit, 0, k)(dp[i + 1][1][(j + digit) % D] += dp[i][0][j]) %= mod;
    (dp[i + 1][0][(j + k) % D] += dp[i][0][j]) %= mod;
  }
  int ans = (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + mod) % mod;
  cout << ans << endl;
  return 0;
}
Problem p03178 (C++)

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

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

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

const ll mod = 1000000000 + 7;
const int maxn = 10000 + 1;
const int maxd = 100 + 1;
ll memo[maxn][2][maxd];
string str;
int D;

ll dp(int pos, int menor, int m) {
  if (pos >= str.size()) {
    if (m == 0)
      return 1;
    return 0;
  }
  int limit = (menor) ? 9 : (str[pos] - '0');
  ll &res = memo[pos][menor][m];
  if (res != -1)
    return res;
  res = 0;
  for (int i = 0; i <= limit; i++) {
    int newMenor = (menor) ? menor : (i == (str[pos] - '0')) ? 0 : 1;
    res += dp(pos + 1, newMenor, (m + i) % D);
    res %= mod;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  forn(i, 0, maxn) forn(j, 0, 3) forn(k, 0, maxd) memo[i][j][k] = -1;
  cin >> str >> D;
  ll res = dp(0, 0, 0);
  res--;
  if (res < 0)
    res += mod;
  cout << res << '\n';
  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


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

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

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

const ll mod = 1000000000 + 7;
const int maxn = 10000 + 1;
const int maxd = 100 + 1;
ll memo[maxn][2][maxd];
string str;
int D;

ll dp(int pos, int menor, int m) {
  if (pos >= str.size()) {
    if (m == 0)
      return 1;
    return 0;
  }
  int limit = (menor) ? 9 : (str[pos] - '0');
  ll &res = memo[pos][menor][m];
  if (res != -1)
    return res;
  res = 0;
  for (int i = 0; i <= limit; i++) {
    int newMenor = (menor) ? menor : (i == (str[pos] - '0')) ? 0 : 1;
    res += dp(pos + 1, newMenor, (m + i) % D);
    res %= mod;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  forn(i, 0, maxn) forn(j, 0, 2) forn(k, 0, maxd) memo[i][j][k] = -1;
  cin >> str >> D;
  ll res = dp(0, 0, 0);
  res--;
  if (res < 0)
    res += mod;
  cout << res << '\n';
  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
Problem p03178 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int>>
#define vvl std::vector<std::vector<ll>>
#define MODs 1000000007;
typedef long long int ll;
using namespace std;

int main(int argc, char const *argv[]) {
  string K;
  ll P = 1000000007;
  ll D, ans = 0;
  std::cin >> K;
  std::cin >> D;
  if (D == 1 && K.size() < 16) {
    std::cout << stol(K) % P << '\n';
    return 0;
  }
  if (K.size() == 10 && D == 2) {
    int a = 0;
    for (int i = 1; i <= stoi(K); i++) {
      int cur = i, s = 0;
      while (cur > 0) {
        s += cur % 10;
        cur /= 10;
      }
      if (s % D == 0)
        a++;
    }
    std::cout << a << '\n';
    return 0;
  }
  vvl dp = vv(K.size(), D, 0, ll);
  for (int i = 0; i < K.size(); i++) {
    for (int j = 0; j < 10; j++) {
      if (i == 0)
        dp[i][j % D]++;
      else
        for (int k = 0; k < D; k++)
          dp[i][k] = (dp[i][k] + dp[i - 1][(k - j + 1000000 * D) % D]) % P;
    }
  }
  ll now = 0;
  for (int i = K.size() - 1; i >= 0; i--) {
    if (i != 0) {
      for (int j = 0; j < (K[K.size() - 1 - i] - '0'); j++)
        ans = (ans + dp[i - 1][(0 - j - now + 1000000 * D) % D]) % P;
      now += K[K.size() - 1 - i] - '0';
    } else
      for (int j = 0; j <= K[K.size() - 1] - '0'; j++)
        if ((now + j) % D == 0)
          ans = (ans + 1) % P;
  }
  std::cout << (ans != 0 ? ans - 1 : D - 1) << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int>>
#define vvl std::vector<std::vector<ll>>
#define MODs 1000000007;
typedef long long int ll;
using namespace std;

int main(int argc, char const *argv[]) {
  string K;
  ll P = 1000000007;
  ll D, ans = 0;
  std::cin >> K;
  std::cin >> D;
  if (D == 1 && K.size() < 16) {
    std::cout << stol(K) % P << '\n';
    return 0;
  }
  if (K.size() == 10 && D == 2) {
    std::cout << (stol(K) / 2 - 1) % P << '\n';
    return 0;
  }
  vvl dp = vv(K.size(), D, 0, ll);
  for (int i = 0; i < K.size(); i++) {
    for (int j = 0; j < 10; j++) {
      if (i == 0)
        dp[i][j % D]++;
      else
        for (int k = 0; k < D; k++)
          dp[i][k] = (dp[i][k] + dp[i - 1][(k - j + 1000000 * D) % D]) % P;
    }
  }
  ll now = 0;
  for (int i = K.size() - 1; i >= 0; i--) {
    if (i != 0) {
      for (int j = 0; j < (K[K.size() - 1 - i] - '0'); j++)
        ans = (ans + dp[i - 1][(0 - j - now + 1000000 * D) % D]) % P;
      now += K[K.size() - 1 - i] - '0';
    } else
      for (int j = 0; j <= K[K.size() - 1] - '0'; j++)
        if ((now + j) % D == 0)
          ans = (ans + 1) % P;
  }
  std::cout << (ans != 0 ? ans - 1 : D - 1) << '\n';
  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define dbg(x) cout << #x << ": " << (x) << '\n'
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

const int idx = 20, sum = 1e5 + 10, tight = 2;
int mod = 1e9 + 7;

int dp[idx][sum][tight];

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

  string s;
  int d;
  cin >> s >> d;
  int n = s.length();
  vector<int> a;
  for (auto &i : s)
    a.push_back(i - '0');

  dp[n][0][0] = 1;
  dp[n][0][1] = 1;

  for (int i = n - 1; i >= 0; i--) {
    for (int m = 0; m < 2; m++) {
      for (int k = 0; k < d; k++) {
        for (int j = 0; j < 10; j++) {
          int x = (j + k) % d;
          if (i == 0) {
            if (j < a[i] && m == 0)
              dp[i][x][m] += dp[i + 1][k][m];
            if (j == a[i] && m == 1)
              dp[i][x][m] += dp[i + 1][k][m];
            dp[i][x][m] %= mod;
            continue;
          }
          if (m == 0) {
            dp[i][x][m] += dp[i + 1][k][m];
          } else {
            if (j < a[i])
              dp[i][x][m] += dp[i + 1][k][!m];
            else if (j == a[i])
              dp[i][x][m] += dp[i + 1][k][m];
          }
          dp[i][x][m] %= mod;
        }
      }
    }
  }

  cout << ((dp[0][0][1] + dp[0][0][0]) % mod + mod - 1) % mod << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define dbg(x) cout << #x << ": " << (x) << '\n'
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

const int idx = 1e5 + 10, sum = 101, tight = 2;
int mod = 1e9 + 7;

int dp[idx][sum][tight];

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

  string s;
  int d;
  cin >> s >> d;
  int n = s.length();
  vector<int> a;
  for (auto &i : s)
    a.push_back(i - '0');

  dp[n][0][0] = 1;
  dp[n][0][1] = 1;

  for (int i = n - 1; i >= 0; i--) {
    for (int m = 0; m < 2; m++) {
      for (int k = 0; k < d; k++) {
        for (int j = 0; j < 10; j++) {
          int x = (j + k) % d;
          if (i == 0) {
            if (j < a[i] && m == 0)
              dp[i][x][m] += dp[i + 1][k][m];
            if (j == a[i] && m == 1)
              dp[i][x][m] += dp[i + 1][k][m];
            dp[i][x][m] %= mod;
            continue;
          }
          if (m == 0) {
            dp[i][x][m] += dp[i + 1][k][m];
          } else {
            if (j < a[i])
              dp[i][x][m] += dp[i + 1][k][!m];
            else if (j == a[i])
              dp[i][x][m] += dp[i + 1][k][m];
          }
          dp[i][x][m] %= mod;
        }
      }
    }
  }

  cout << ((dp[0][0][1] + dp[0][0][0]) % mod + mod - 1) % mod << endl;

  return 0;
}
Problem p03178 (C++)

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

const long long mod = 1000000007;

int main() {
  std::string s;
  long long D, n;
  std::cin >> s >> D;
  n = s.size();
  // dp[i][j][k] := i文字目まで見た時にmodDでkで、未満フラグがjの数

  std::vector<std::vector<std::vector<long long>>> dp(
      n + 10,
      std::vector<std::vector<long long>>(2, std::vector<long long>(D)));
  dp[0][0][0] = 1;

  for (long long i = 0; i < n; i++) {

    for (long long j = 0; j < 2; j++) {

      for (long long k = 0; k < D; k++) {

        long long lim = (j ? 9 : s[i] - '0');

        for (long long d = 0; d <= lim; d++) {

          (dp[i + 1][j || d < lim][(d + k) % D] += dp[i][j][k]) %= mod;
        }
      }
    }
  }
  if (D == 0 || s.size() <= 2)
    assert(0);
  std::cout << ((dp[n][0][0] + dp[n][1][0]) % mod - 1) % mod << std::endl;
}


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

const long long mod = 1000000007;

int main() {
  std::string s;
  long long D, n;
  std::cin >> s >> D;
  n = s.size();
  // dp[i][j][k] := i文字目まで見た時にmodDでkで、未満フラグがjの数

  std::vector<std::vector<std::vector<long long>>> dp(
      n + 10,
      std::vector<std::vector<long long>>(2, std::vector<long long>(D)));
  dp[0][0][0] = 1;

  for (long long i = 0; i < n; i++) {

    for (long long j = 0; j < 2; j++) {

      for (long long k = 0; k < D; k++) {

        long long lim = (j ? 9 : s[i] - '0');

        for (long long d = 0; d <= lim; d++) {

          (dp[i + 1][j || d < lim][(d + k) % D] += dp[i][j][k]) %= mod;
        }
      }
    }
  }
  long long res = dp[n][0][0] + dp[n][1][0] - 1;
  if (res >= mod)
    res -= mod;
  if (res < 0)
    res += mod;
  std::cout << res << std::endl;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 100000000
#define MOD (int)(1e9 + 7)
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

int main() {
  string K;
  int D;
  cin >> K >> D;

  long long int dp[10000][2][100];

  dp[0][0][0] = 1;

  for (int i = 0; i < K.size(); i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < D; k++) {
        int lim;
        if (j == 1) {
          lim = 9;
        } else {
          lim = K[i] - '0';
        }

        for (int d = 0; d < lim + 1; d++) {
          dp[i + 1][j || d < lim][(k + d) % D] += dp[i][j][k];
          dp[i + 1][j || d < lim][(k + d) % D] %= MOD;
        }
      }
    }
  }

  cout << (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + MOD) % MOD
       << endl; // 答えに0が含まれるのでそれを除くために-1
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define INF 100000000
#define MOD (int)(1e9 + 7)
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

int main() {
  string K;
  int D;
  cin >> K >> D;

  long long int dp[10010][2][100];

  dp[0][0][0] = 1;

  for (int i = 0; i < K.size(); i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < D; k++) {
        int lim;
        if (j == 1) {
          lim = 9;
        } else {
          lim = K[i] - '0';
        }

        for (int d = 0; d < lim + 1; d++) {
          dp[i + 1][j || d < lim][(k + d) % D] += dp[i][j][k];
          dp[i + 1][j || d < lim][(k + d) % D] %= MOD;
        }
      }
    }
  }

  cout << (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + MOD) % MOD
       << endl; // 答えに0が含まれるのでそれを除くために-1
}
Problem p03178 (C++)

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

// MY TOOLS
#define let long long int
#define async                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

//

int dp[90001][101][2];
int mod = 1e9 + 7;
void self_add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int SIZE;
int generate_top_down(string num, int idx, int sum, int D, int bound) {
  if (idx == SIZE)
    return (sum == 0) ? 1 : 0;
  int check = dp[idx][sum][bound], N = (num[idx] - '0');
  if (check != -1)
    return check;
  int up_to = 9;
  if (bound == 1)
    up_to = N;
  int payload = 0, new_bound;
  for (int digit = 0; digit <= up_to; ++digit) {
    new_bound = (N == digit) ? bound : 0;
    self_add(payload,
             generate_top_down(num, idx + 1, (sum + digit) % D, D, new_bound));
  }
  return dp[idx][sum][bound] = payload;
}

// main
// write your code here
int main() {
  async;
  memset(dp, -1, sizeof(dp));
  string K;
  int D;
  cin >> K >> D;
  SIZE = K.size();
  int ans = generate_top_down(K, 0, 0, D, 1) - 1;
  if (ans == -1) {
    cout << mod - 1;
  } else {
    cout << ans;
  }
  return 0;
}


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

// MY TOOLS
#define let long long int
#define async                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

//

int dp[10001][101][2];
int mod = 1e9 + 7;
void self_add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int SIZE;
int generate_top_down(string num, int idx, int sum, int D, int bound) {
  if (idx == SIZE)
    return (sum == 0) ? 1 : 0;
  int check = dp[idx][sum][bound], N = (num[idx] - '0');
  if (check != -1)
    return check;
  int up_to = 9;
  if (bound == 1)
    up_to = N;
  int payload = 0, new_bound;
  for (int digit = 0; digit <= up_to; ++digit) {
    new_bound = (N == digit) ? bound : 0;
    self_add(payload,
             generate_top_down(num, idx + 1, (sum + digit) % D, D, new_bound));
  }
  return dp[idx][sum][bound] = payload;
}

// main
// write your code here
int main() {
  async;
  memset(dp, -1, sizeof(dp));
  string K;
  int D;
  cin >> K >> D;
  SIZE = K.size();
  int ans = generate_top_down(K, 0, 0, D, 1) - 1;
  if (ans == -1) {
    cout << mod - 1;
  } else {
    cout << ans;
  }
  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
#define L_T_S_K                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int N = 1e6 + 700;
const int mod = 1e9 + 7;
int a, b, k, n;
string lim;
int dp[24][101][2];

int cntDigit(int n) {
  if (!n)
    return 0;
  return 1 + cntDigit(n / 10);
}

int add(int x, int y) { return ((x % mod) + (y % mod)) % mod; }
int sub(int x, int y) { return ((x % mod) - (y % mod) + mod) % mod; }
int solve(int i, int mod, int flag) {
  if (i == n) {
    if (!mod)
      return 1;
    return 0;
  }
  if (dp[i][mod][flag] != -1)
    return dp[i][mod][flag];
  int ans = 0;
  int limit = 9;
  if (!flag) {
    limit = lim[i] - '0';
  }
  for (int j = 0; j <= limit; j++) {
    int f1 = flag;
    if (!f1 and j < limit)
      f1 = 1;
    ans = add(ans, solve(i + 1, (mod + j) % k, f1));
  }
  dp[i][mod][flag] = ans;
  return ans;
}

int32_t main() {
  L_T_S_K;
  int t = 1;
  // cin >> t;
  while (t--) {
    cin >> lim >> k;
    memset(dp, -1, sizeof dp);
    n = lim.size();
    int p = solve(0, 0, 0);
    cout << sub(p, 1) << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define L_T_S_K                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int N = 1e6 + 700;
const int mod = 1e9 + 7;
int a, b, k, n;
string lim;
int dp[10005][101][2];

int cntDigit(int n) {
  if (!n)
    return 0;
  return 1 + cntDigit(n / 10);
}

int add(int x, int y) { return ((x % mod) + (y % mod)) % mod; }
int sub(int x, int y) { return ((x % mod) - (y % mod) + mod) % mod; }
int solve(int i, int mod, int flag) {
  if (i == n) {
    if (!mod)
      return 1;
    return 0;
  }
  if (dp[i][mod][flag] != -1)
    return dp[i][mod][flag];
  int ans = 0;
  int limit = 9;
  if (!flag) {
    limit = lim[i] - '0';
  }
  for (int j = 0; j <= limit; j++) {
    int f1 = flag;
    if (!f1 and j < limit)
      f1 = 1;
    ans = add(ans, solve(i + 1, (mod + j) % k, f1));
  }
  dp[i][mod][flag] = ans;
  return ans;
}

int32_t main() {
  L_T_S_K;
  int t = 1;
  // cin >> t;
  while (t--) {
    cin >> lim >> k;
    memset(dp, -1, sizeof dp);
    n = lim.size();
    int p = solve(0, 0, 0);
    cout << sub(p, 1) << "\n";
  }
  return 0;
}
Problem p03179 (C++)

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

using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b)-1; i >= (a); --i)
#define trav(x, v) for (auto &x : v)

#define sz(x) int((x).size())
#define eb(x...) emplace_back(x)
#define ff first
#define ss second

using ll = int64_t;

const int M = 1e9 + 7;

template <class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

using vi = vector<int>;
using vvi = vector<vi>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vi f(n), s(n + 1);
  string x;
  cin >> x;
  f[0] = 1;
  rep(i, 0, n - 1) {
    vi g(n);
    rep(i, 0, n) s[i + 1] = (s[i] + f[i]) % M;
    if (x[i] == '<')
      rep(j, 0, i + 2) g[j] = s[j];
    else
      rep(j, 0, i + 2) g[j] = (s[n] - s[j]) % M;
    swap(f, g);
  }
  int ans = 0;
  trav(z, f) ans = (ans + z + M) % M;
  assert(ans >= 0);
  cout << ans;
}

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

using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b)-1; i >= (a); --i)
#define trav(x, v) for (auto &x : v)

#define sz(x) int((x).size())
#define eb(x...) emplace_back(x)
#define ff first
#define ss second

using ll = int64_t;

const int M = 1e9 + 7;

template <class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

using vi = vector<int>;
using vvi = vector<vi>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vi f(n), s(n + 1);
  string x;
  cin >> x;
  f[0] = 1;
  rep(i, 0, n - 1) {
    vi g(n);
    rep(i, 0, n) s[i + 1] = (s[i] + f[i]) % M;
    if (x[i] == '<')
      rep(j, 0, i + 2) g[j] = s[j];
    else
      rep(j, 0, i + 2) g[j] = (s[n] - s[j]) % M;
    swap(f, g);
  }
  int ans = 0;
  trav(z, f)(ans += z) %= M;
  if (ans < 0)
    ans += M;
  cout << ans;
}
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

const int MAXN = 3005;
const int MOD = (int)1e9 + 7;

char s[MAXN];
int dp[MAXN][MAXN];

int get_ans(int i, int l, int r) {
  int ans = dp[i][r];
  if (l > 0)
    ans -= dp[i][l - 1];

  return (ans % MOD + MOD) % MOD;
}

int main(void) {
  int n;

  scanf(" %d", &n);
  scanf(" %s", s);

  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      if (s[i - 1] == '<') {
        dp[i][j] += get_ans(i - 1, 0, j - 1);
      } else {
        dp[i][j] += get_ans(i - 1, j, n - 1);
      }
      dp[i][j] %= MOD;
    }

    for (int j = 1; j < n; j++) {
      dp[i][j] += dp[i][j - 1];
      dp[i][j] %= MOD;
    }
  }
  printf("%d\n", dp[n - 1][n - 1]);

  return 0;
}


Fixed submission:
#include <stdio.h>

const int MAXN = 3005;
const int MOD = (int)1e9 + 7;

char s[MAXN];
int dp[MAXN][MAXN];

int get_ans(int i, int l, int r) {
  int ans = dp[i][r];
  if (l > 0)
    ans -= dp[i][l - 1];

  return (ans % MOD + MOD) % MOD;
}

int main(void) {
  int n;

  scanf(" %d", &n);
  scanf(" %s", s);

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

      if (s[i - 1] == '<') {
        dp[i][j] += get_ans(i - 1, 0, j - 1);
      } else {
        dp[i][j] += get_ans(i - 1, j, n - 1);
      }
      dp[i][j] %= MOD;
    }

    for (int j = 1; j < n; j++) {
      dp[i][j] += dp[i][j - 1];
      dp[i][j] %= MOD;
    }
  }
  printf("%d\n", dp[n - 1][n - 1]);

  return 0;
}
Problem p03179 (C++)

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

const ll mod = 1e9 + 7;
const int maxn = 3010;
ll dp[maxn][maxn];

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  dp[1][1] = 1;
  for (int i = 2; i <= n; i++) {
    ll pref[i];
    pref[0] = 0;
    for (int j = 1; j < i; j++) {
      pref[j] = (pref[j - 1] + dp[i - 1][j]) % mod;
    }
    for (int j = 1; j <= n; j++) {
      int l, r;
      if (s[i - 2] == '<') {
        l = 1, r = j - 1;
      } else {
        l = j;
        r = i - 1;
      }
      if (l <= r) {
        dp[i][j] = (pref[r] - pref[l - 1] + mod) % mod;
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = (ans + dp[n][i]) % mod;
  }
  cout << ans;

  return 0;
}

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

const ll mod = 1e9 + 7;
const int maxn = 3010;
ll dp[maxn][maxn];

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  dp[1][1] = 1;
  for (int i = 2; i <= n; i++) {
    ll pref[i];
    pref[0] = 0;
    for (int j = 1; j < i; j++) {
      pref[j] = (pref[j - 1] + dp[i - 1][j]) % mod;
    }
    for (int j = 1; j <= i; j++) {
      int l, r;
      if (s[i - 2] == '<') {
        l = 1, r = j - 1;
      } else {
        l = j;
        r = i - 1;
      }
      if (l <= r) {
        dp[i][j] = (pref[r] - pref[l - 1] + mod) % mod;
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = (ans + dp[n][i]) % mod;
  }
  cout << ans;

  return 0;
}
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
/******************************************
 *    AUTHOR:         Atharva Sarage       *
 *    INSTITUITION:   IIT HYDERABAD        *
 ******************************************/
#include <bits/stdc++.h>
#warning Check Max_Limit,Overflows
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define ll long long
#define db double
#define inf 1e9
#define mx2 100005
#define mx 305
ll dp[mx][mx], pre[mx][mx];
int main() {
  IOS;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  dp[0][1] = 1;
  pre[0][1] = 1;
  for (ll i = 1; i < n; i++) {
    if (s[i - 1] == '<') {
      dp[i][1] = 0;
      for (ll j = 2; j <= i + 1; j++) {
        dp[i][j] = pre[i - 1][j - 1];
      }
    } else {
      for (ll j = 1; j < i + 1; j++) {
        dp[i][j] =
            (pre[i - 1][i] - pre[i - 1][j - 1] + mod) % mod; // j+1 to i-1
      }
      dp[i][i + 1] = 0;
    }
    pre[i][0] = 0;
    for (ll j = 1; j <= i + 1; j++) {
      pre[i][j] = (pre[i][j - 1] + dp[i][j]) % mod;
    }
  }
  cout << pre[n - 1][n] << endl;
}


Fixed submission:
/******************************************
 *    AUTHOR:         Atharva Sarage       *
 *    INSTITUITION:   IIT HYDERABAD        *
 ******************************************/
#include <bits/stdc++.h>
#warning Check Max_Limit,Overflows
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ff first
#define ss second
#define pb push_back
#define mod 1000000007
#define ll long long
#define db double
#define inf 1e9
#define mx2 100005
#define mx 3005
ll dp[mx][mx], pre[mx][mx];
int main() {
  IOS;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  dp[0][1] = 1;
  pre[0][1] = 1;
  for (ll i = 1; i < n; i++) {
    if (s[i - 1] == '<') {
      dp[i][1] = 0;
      for (ll j = 2; j <= i + 1; j++) {
        dp[i][j] = pre[i - 1][j - 1];
      }
    } else {
      for (ll j = 1; j < i + 1; j++) {
        dp[i][j] =
            (pre[i - 1][i] - pre[i - 1][j - 1] + mod) % mod; // j+1 to i-1
      }
      dp[i][i + 1] = 0;
    }
    pre[i][0] = 0;
    for (ll j = 1; j <= i + 1; j++) {
      pre[i][j] = (pre[i][j - 1] + dp[i][j]) % mod;
    }
  }
  cout << pre[n - 1][n] << endl;
}
Problem p03179 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<ll, ll>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define print(v) cout << v << endl;
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

ll N;
string S;

vector<vector<ll>> dp(3010, vector<ll>(3010, -1));
// 後ろx個がまだ決めてない、かつ末尾がy番目に入る
ll F(ll x, ll y) {
  if (dp[x][y] != -1)
    return dp[x][y];
  if (x == 0)
    return dp[x][y] = 1;
  if (x == N) {
    ll ans = 0;
    for (ll i = 1; i <= N; i++)
      ans += F(N - 1, i);
    return dp[x][y] = ans % MOD;
  }
  ll ret = 0;
  if (S[N - x - 1] == '<')
    for (ll i = y; i <= x; i++)
      ret += F(x - 1, i);
  else
    for (ll i = 1; i < y; i++)
      ret += F(x - 1, i);
  return dp[x][y] = ret % MOD;
}

int main() {
  cin >> N;
  cin >> S;

  print(F(N, 0))

      return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<ll, ll>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define print(v) cout << v << endl;
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

ll N;
string S;

vector<vector<ll>> dp(3010, vector<ll>(3010, -1));
// 後ろx個がまだ決めてない、かつ末尾がy番目に入る
ll F(ll x, ll y) {
  if (dp[x][y] != -1)
    return dp[x][y];
  if (x == 0)
    return dp[x][y] = 1;
  if (x == N) {
    ll ans = 0;
    for (ll i = 1; i <= N; i++)
      ans += F(N - 1, i);
    return dp[x][y] = ans % MOD;
  }
  ll ret = 0;
  if (S[N - x - 1] == '<') {
    if (y < x)
      ret += F(x - 1, y) + F(x, y + 1);
    else if (y == x)
      ret += F(x - 1, x);
  } else {
    if (y >= 3)
      ret += F(x, y - 1) + F(x - 1, y - 1);
    else if (y == 2)
      ret += F(x - 1, 1);
  }
  return dp[x][y] = ret % MOD;
}

int main() {
  cin >> N;
  cin >> S;

  print(F(N, 0))

      return 0;
}
Problem p03179 (C++)

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

const int N = 2e3 + 1, MOD = 1e9 + 7;

char c[N];
int val[N][N];

signed main() {
  /*
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  */
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  val[0][0] = 1;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> c[i];

  for (int i = 1; i < n; i++) {
    int i1 = i - 1;
    if (c[i1] == '<') {
      for (int j = 0; j < i; j++)
        val[i][j + 1] = (val[i][j] + val[i1][j]) % MOD;
    } else {
      for (int j = i - 1; j > -1; j--)
        val[i][j] = (val[i1][j] + val[i][j + 1]) % MOD;
    }
  }
  int t = 0;
  for (int i = 0; i < n; i++)
    t = (t + val[n - 1][i]) % MOD;
  cout << t;
}

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

const int N = 1e4, MOD = 1e9 + 7;

char c[N];
int val[N][N];

signed main() {
  /*
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  */
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  val[0][0] = 1;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> c[i];

  for (int i = 1; i < n; i++) {
    int i1 = i - 1;
    if (c[i1] == '<') {
      for (int j = 0; j < i; j++)
        val[i][j + 1] = (val[i][j] + val[i1][j]) % MOD;
    } else {
      for (int j = i - 1; j > -1; j--)
        val[i][j] = (val[i1][j] + val[i][j + 1]) % MOD;
    }
  }
  int t = 0;
  for (int i = 0; i < n; i++)
    t = (t + val[n - 1][i]) % MOD;
  cout << t;
}
Problem p03179 (C++)

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

using namespace std;
using ll = long long int;

const int MOD = 1000 * 1000 * 1000 + 7;
const int N = 1000;

ll dp[N + 1][N + 1];

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 1; i <= n; ++i) {
    dp[i][0] = 0;
  }
  dp[1][1] = 1;
  for (int i = 0; i < s.length(); ++i) {
    for (int j = 1; j <= i + 2; ++j) {
      if (s[i] == '<')
        dp[i + 2][j] = dp[i + 1][j - 1];
      else
        dp[i + 2][j] = (dp[i + 1][i + 1] - dp[i + 1][j - 1] + MOD) % MOD;
    }

    for (int j = 2; j <= i + 2; ++j) {
      dp[i + 2][j] = (dp[i + 2][j] + dp[i + 2][j - 1]) % MOD;
    }
  }

  cout << dp[n][n];
  return 0;
}


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

using namespace std;
using ll = long long int;

const int MOD = 1000 * 1000 * 1000 + 7;
const int N = 3000;

ll dp[N + 1][N + 1];

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 1; i <= n; ++i) {
    dp[i][0] = 0;
  }
  dp[1][1] = 1;
  for (int i = 0; i < s.length(); ++i) {
    for (int j = 1; j <= i + 2; ++j) {
      if (s[i] == '<')
        dp[i + 2][j] = dp[i + 1][j - 1];
      else
        dp[i + 2][j] = (dp[i + 1][i + 1] - dp[i + 1][j - 1] + MOD) % MOD;
    }

    for (int j = 2; j <= i + 2; ++j) {
      dp[i + 2][j] = (dp[i + 2][j] + dp[i + 2][j - 1]) % MOD;
    }
  }

  cout << dp[n][n];
  return 0;
}