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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
const int MAX_N = 1 << 17;

int n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  rep(i, 2 * n - 1) dat[i] = 0;
}

// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
// [a, b)の最大値を探す．
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}
int h[200010];
int a[200010];

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  init(N);
  rep(i, N) cin >> h[i];
  rep(i, N) cin >> a[i];
  rep(i, N) {
    int tmp = query(0, h[i], 0, 0, n);
    update(h[i] - 1, tmp + a[i]);
    // rep(i, N) cout << dat[n - 1 + i] << " ";
    // cout << endl;
  }
  cout << query(0, N, 0, 0, n) << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
const int MAX_N = 1 << 18;

int n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  rep(i, 2 * n - 1) dat[i] = 0;
}

// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
// [a, b)の最大値を探す．
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}
int h[200010];
int a[200010];

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  init(N);
  rep(i, N) cin >> h[i];
  rep(i, N) cin >> a[i];
  rep(i, N) {
    int tmp = query(0, h[i], 0, 0, n);
    update(h[i] - 1, tmp + a[i]);
    // rep(i, N) cout << dat[n - 1 + i] << " ";
    // cout << endl;
  }
  cout << query(0, N, 0, 0, n) << endl;

  return 0;
}
Problem p03176 (C++)

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

struct flower {
  ll hi;
  ll bt;
};

ll n;
vector<flower> v(N);

ll lis() {
  vector<ll> dp(n + 1);
  dp[0] = 0;
  dp[1] = v[1].bt;

  map<ll, ll> m;
  m[v[1].hi] = dp[1];
  ll ans = dp[1];
  for (ll i = 2; i <= n; i++) {

    dp[i] = v[i].bt;
    auto it = m.lower_bound(v[i].hi + 1);
    if (it != m.begin()) {
      it--;
      dp[i] += it->second;
    }
    m[v[i].hi] = dp[i];
    it = m.upper_bound(v[i].hi);
    while (it != m.end() && it->second <= dp[i]) {
      auto temp = it;
      temp++;
      m.erase(it);
      it = temp;
    }
    ans = max(ans, dp[i]);
  }
  return ans;
}

int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> v[i].hi;
  }

  for (ll i = 1; i <= n; i++) {
    cin >> v[i].bt;
  }

  cout << lis() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 200006;

struct flower {
  ll hi;
  ll bt;
};

ll n;
vector<flower> v(N);

ll lis() {
  vector<ll> dp(n + 1);
  dp[0] = 0;
  dp[1] = v[1].bt;

  map<ll, ll> m;
  m[v[1].hi] = dp[1];
  ll ans = dp[1];
  for (ll i = 2; i <= n; i++) {

    dp[i] = v[i].bt;
    auto it = m.lower_bound(v[i].hi + 1);
    if (it != m.begin()) {
      it--;
      dp[i] += it->second;
    }
    m[v[i].hi] = dp[i];
    it = m.upper_bound(v[i].hi);
    while (it != m.end() && it->second <= dp[i]) {
      auto temp = it;
      temp++;
      m.erase(it);
      it = temp;
    }
    ans = max(ans, dp[i]);
  }
  return ans;
}

int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> v[i].hi;
  }

  for (ll i = 1; i <= n; i++) {
    cin >> v[i].bt;
  }

  cout << lis() << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long st[400005] = {};
int h[100005], w[100005];
void update(int x, int l, int r, int z, long long int zz) {
  if (l > z || r < z)
    return;
  if (l == r) {
    st[x] = zz;
    return;
  }
  int mid = (l + r) / 2;
  update(x * 2, l, mid, z, zz);
  update(x * 2 + 1, mid + 1, r, z, zz);
  st[x] = max(st[x * 2], st[x * 2 + 1]);
}
long long query(int x, int l, int r, int ll, int rr) {
  if (l > rr || r < ll)
    return 0;
  if (l >= ll && r <= rr)
    return st[x];
  int mid = (l + r) / 2;
  return max(query(x * 2, l, mid, ll, rr),
             query(x * 2 + 1, mid + 1, r, ll, rr));
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long asd = query(1, 1, n, 1, h[i]);
    update(1, 1, n, h[i], asd + w[i]);
    ans = max(ans, asd + w[i]);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long st[800005] = {};
long long h[200005], w[200005];
void update(int x, int l, int r, int z, long long int zz) {
  if (l > z || r < z)
    return;
  if (l == r) {
    st[x] = zz;
    return;
  }
  int mid = (l + r) / 2;
  update(x * 2, l, mid, z, zz);
  update(x * 2 + 1, mid + 1, r, z, zz);
  st[x] = max(st[x * 2], st[x * 2 + 1]);
}
long long query(int x, int l, int r, int ll, int rr) {
  if (l > rr || r < ll)
    return 0;
  if (l >= ll && r <= rr)
    return st[x];
  int mid = (l + r) / 2;
  return max(query(x * 2, l, mid, ll, rr),
             query(x * 2 + 1, mid + 1, r, ll, rr));
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long asd = query(1, 1, n, 1, h[i]);
    update(1, 1, n, h[i], asd + w[i]);
    ans = max(ans, asd + w[i]);
  }
  cout << ans;
}
Problem p03177 (C++)

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

using namespace std;
using namespace __gnu_cxx;

#define ll long long
const long double EPS = 1e-8;
const long double PI = acos(-1);
const int N = 1e5 + 9;
const ll inf = (ll)-1e18;
const int mod = 1e9 + 7;
typedef vector<vector<ll>> Mat;

vector<pair<int, int>> dir = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
                              {0, 1},   {1, -1}, {1, 0},  {1, 1}};

struct Mul {
  int sz = 1;

  Mat operator()(const Mat &a, const Mat &b) const {
    Mat ret(a.size(), vector<ll>(b[0].size()));
    for (int i = 0; i < ret.size(); i++) {      // row in first matrix
      for (int j = 0; j < ret[0].size(); j++) { // col in second matrix
        for (int k = 0; k < b.size(); k++) {
          ret[i][j] = (ret[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
        }
      }
    }
    return ret;
  }
};

Mat identity_element(const Mul &mul) {
  Mat ret(mul.sz, vector<ll>(mul.sz));
  for (int i = 0; i < mul.sz; i++)
    ret[i][i] = 1;
  return ret;
}

int main() {
  freopen("input.txt", "r", stdin);
  int t;
  //    scanf("%d", &t);
  //    while (t--) {
  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  Mat v(n, vector<ll>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%lld", &v[i][j]);
    }
  }
  Mul multiplyOperator;
  multiplyOperator.sz = n * n;

  Mat ans = power(v, k, multiplyOperator);
  ll a = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      a = (a + ans[i][j]) % mod;
    }
  }
  printf("%lld\n", a);
  //    }
}

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

using namespace std;
using namespace __gnu_cxx;

#define ll long long
const long double EPS = 1e-8;
const long double PI = acos(-1);
const int N = 1e5 + 9;
const ll inf = (ll)-1e18;
const int mod = 1e9 + 7;
typedef vector<vector<ll>> Mat;

vector<pair<int, int>> dir = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
                              {0, 1},   {1, -1}, {1, 0},  {1, 1}};

struct Mul {
  int sz = 1;

  Mat operator()(const Mat &a, const Mat &b) const {
    Mat ret(a.size(), vector<ll>(b[0].size()));
    for (int i = 0; i < ret.size(); i++) {      // row in first matrix
      for (int j = 0; j < ret[0].size(); j++) { // col in second matrix
        for (int k = 0; k < b.size(); k++) {
          ret[i][j] = (ret[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
        }
      }
    }
    return ret;
  }
};

Mat identity_element(const Mul &mul) {
  Mat ret(mul.sz, vector<ll>(mul.sz));
  for (int i = 0; i < mul.sz; i++)
    ret[i][i] = 1;
  return ret;
}

int main() {
  //    freopen("input.txt", "r", stdin);
  int t;
  //    scanf("%d", &t);
  //    while (t--) {
  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  Mat v(n, vector<ll>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%lld", &v[i][j]);
    }
  }
  Mul multiplyOperator;
  multiplyOperator.sz = n * n;

  Mat ans = power(v, k, multiplyOperator);
  ll a = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      a = (a + ans[i][j]) % mod;
    }
  }
  printf("%lld\n", a);
  //    }
}
Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define double long double
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
int main() {
  ll n, K;
  cin >> n >> K;
  vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(n, vector<ll>(65)));
  rep(i, n) rep(j, n) {
    ll a;
    cin >> a;
    if (a)
      dp[i][j][0] = 1;
  }
  REP(l, 1, 65) {
    rep(i, n) {
      rep(j, n) {
        rep(k, n) {
          dp[i][j][l] += dp[i][k][l - 1] % inf * dp[k][j][l - 1] % inf;
          dp[i][j][l] %= inf;
        }
      }
    }
  }
  vector<vector<ll>> path(n, vector<ll>(65));
  rep(i, n) path[i][0] = 1;
  rep(k, 65) {
    if ((1LL << k) & K) {
      rep(i, n) {
        rep(j, n) {
          path[i][k + 1] += path[j][k] % inf * dp[j][i][k] % inf;
          path[i][k + 1] %= inf;
        }
      }
    } else {
      rep(i, n) { path[i][k + 1] = path[i][k]; }
    }
  }
  ll ans = 0;
  rep(i, n) {
    ans += path[i][64];
    ans %= inf;
  }
  rep(i, 3) {
    rep(j, n) {
      rep(k, n) {
        // cout<<i<<' '<<j<<' '<<k<<' '<<dp[j][k][i]<<endl;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define double long double
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
int main() {
  ll n, K;
  cin >> n >> K;
  vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(n, vector<ll>(65)));
  rep(i, n) rep(j, n) {
    ll a;
    cin >> a;
    if (a)
      dp[i][j][0] = 1;
  }
  REP(l, 1, 65) {
    rep(i, n) {
      rep(j, n) {
        rep(k, n) {
          dp[i][j][l] += dp[i][k][l - 1] % inf * dp[k][j][l - 1] % inf;
          dp[i][j][l] %= inf;
        }
      }
    }
  }
  vector<vector<ll>> path(n, vector<ll>(65));
  rep(i, n) path[i][0] = 1;
  rep(k, 64) {
    if ((1LL << k) & K) {
      rep(i, n) {
        rep(j, n) {
          path[i][k + 1] += path[j][k] % inf * dp[j][i][k] % inf;
          path[i][k + 1] %= inf;
        }
      }
    } else {
      rep(i, n) { path[i][k + 1] = path[i][k]; }
    }
  }
  ll ans = 0;
  rep(i, n) {
    ans += path[i][64];
    ans %= inf;
  }
  rep(i, 3) {
    rep(j, n) {
      rep(k, n) {
        // cout<<i<<' '<<j<<' '<<k<<' '<<dp[j][k][i]<<endl;
      }
    }
  }
  cout << ans << endl;
}
Problem p03177 (C++)

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

typedef struct matrix {
  long long int m[55][55];
} M;
const int MOD = 1000000007;
long long int N, K;
M g;

M mul(M a, M b) {
  M c;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      c.m[i][j] = 0;
      for (int k = 0; k < N; k++) {
        c.m[i][j] += (a.m[i][k] * b.m[k][j]) % MOD;
        c.m[i][j] %= MOD;
      }
    }
  }
  return c;
}

M exp(M a, int p) {
  if (p == 1) {
    return a;
  }
  M c = exp(a, p / 2);
  c = mul(c, c);
  if (p % 2 == 1)
    c = mul(a, c);
  return c;
}
int main() {
  // freopen("test.in", "r+", stdin);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cin >> g.m[i][j];

  g = exp(g, K);
  long long int result = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      result += g.m[i][j];
      result %= MOD;
    }
  }
  cout << result << endl;
}


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

typedef struct matrix {
  long long int m[55][55];
} M;
const int MOD = 1000000007;
long long int N, K;
M g;

M mul(M a, M b) {
  M c;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      c.m[i][j] = 0;
      for (int k = 0; k < N; k++) {
        c.m[i][j] += (a.m[i][k] * b.m[k][j]) % MOD;
        c.m[i][j] %= MOD;
      }
    }
  }
  return c;
}

M exp(M a, long long int p) {
  if (p == 1) {
    return a;
  }
  M c = exp(a, p / 2);
  c = mul(c, c);
  if (p % 2 == 1)
    c = mul(a, c);
  return c;
}
int main() {
  // freopen("test.in", "r+", stdin);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cin >> g.m[i][j];

  g = exp(g, K);
  long long int result = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      result += g.m[i][j];
      result %= MOD;
    }
  }
  cout << result << endl;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define in(v)                                                                  \
  v;                                                                           \
  cin >> v;
#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)

template <int MOD> struct MInt {
  long long val;
  constexpr MInt(long long val = 0) : val(val % MOD) {
    if (val < 0)
      val += MOD;
  }
  MInt operator-() const { return MInt(-val); }
  MInt operator+(const MInt &n) const { return MInt(val) += n; }
  MInt operator-(const MInt &n) const { return MInt(val) -= n; }
  MInt &operator+=(const MInt &n) {
    val = (val + n.val) % MOD;
    return *this;
  }
  MInt &operator-=(const MInt &n) {
    val = (MOD + val - n.val) % MOD;
    return *this;
  }
  friend ostream &operator<<(ostream &os, const MInt &n) {
    os << n.val;
    return os;
  }
  friend istream &operator>>(istream &os, MInt &n) {
    os >> n.val;
    return os;
  }
};
constexpr int MOD = 1e9 + 7;
using mint = MInt<MOD>;

string K;
int D;
constexpr int MAX_N = 10000, MAX_D = 100;
mint dp[MAX_N][MAX_D][2];

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

  dp[K.size()][0][0] = dp[K.size()][0][1] = 1;

  rrep(i, K.size() - 1) rep(r, D) rep(lower, 2) {
    int n = K[i] - '0';
    rep(j, (lower ? 9 : n) + 1) dp[i][r][lower] +=
        dp[i + 1][(r + j) % D][lower || (j < n)];
  }

  cout << dp[0][0][false] - 1 << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define in(v)                                                                  \
  v;                                                                           \
  cin >> v;
#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)

template <int MOD> struct MInt {
  long long val;
  constexpr MInt(long long val = 0) : val(val % MOD) {
    if (val < 0)
      val += MOD;
  }
  MInt operator-() const { return MInt(-val); }
  MInt operator+(const MInt &n) const { return MInt(val) += n; }
  MInt operator-(const MInt &n) const { return MInt(val) -= n; }
  MInt &operator+=(const MInt &n) {
    val = (val + n.val) % MOD;
    return *this;
  }
  MInt &operator-=(const MInt &n) {
    val = (MOD + val - n.val) % MOD;
    return *this;
  }
  friend ostream &operator<<(ostream &os, const MInt &n) {
    os << n.val;
    return os;
  }
  friend istream &operator>>(istream &os, MInt &n) {
    os >> n.val;
    return os;
  }
};
constexpr int MOD = 1e9 + 7;
using mint = MInt<MOD>;

string K;
int D;
constexpr int MAX_N = 10000, MAX_D = 100;
mint dp[MAX_N + 1][MAX_D][2];

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

  dp[K.size()][0][0] = dp[K.size()][0][1] = 1;

  rrep(i, K.size() - 1) rep(r, D) rep(lower, 2) {
    int n = K[i] - '0';
    rep(j, (lower ? 9 : n) + 1) dp[i][r][lower] +=
        dp[i + 1][(r + j) % D][lower || (j < n)];
  }

  cout << dp[0][0][false] - 1 << endl;

  return 0;
}
Problem p03178 (C++)

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

#define endl '\n'
#define left aba
#define right abc
#define oo if (LOG)
#define pb push_back
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

#ifdef LOCAL
#define LOG 1
#else
#define LOG 0
#endif

using namespace std;

const int DIG = 10005, MAXD = 105, MOD = (int)(1e9) + 7;

string k;
int d, n;

inline void add(long long &aa, long long bb) {
  aa += bb;
  aa %= MOD;
}

long long dp[DIG][2][MAXD];

long long go(int pos, bool fl, int sum) {
  if (pos >= n)
    return sum % d == 0 ? 1 : 0;
  if (dp[pos][fl][sum % d] != -1)
    return dp[pos][fl][sum % d];

  long long ret = 0;
  for (int i = 0; i <= 9; ++i) {
    if (fl || (!fl && i <= k[pos] - '0')) {
      long long nxt = go(pos + 1, fl | (i < k[pos] - '0'), sum + i);
      add(ret, nxt);
    }
  }
  return dp[pos][fl][sum % d] = ret;
}

int main() {
  FIO;

  cin >> k >> d;
  n = (int)k.size();
  for (int i = 0; i < DIG; ++i)
    for (int j = 0; j < 2; ++j)
      for (int l = 0; l < MAXD; ++l)
        dp[i][j][l] = -1;
  long long ans = go(0, 0, 0) - 1;
  assert(ans >= 0);
  cout << ans << endl;

  oo cout << "Time: " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
  return 0;
}


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

#define endl '\n'
#define left aba
#define right abc
#define oo if (LOG)
#define pb push_back
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

#ifdef LOCAL
#define LOG 1
#else
#define LOG 0
#endif

using namespace std;

const int DIG = 10005, MAXD = 105, MOD = (int)(1e9) + 7;

string k;
int d, n;

inline void add(long long &aa, long long bb) {
  aa += bb;
  aa %= MOD;
}

long long dp[DIG][2][MAXD];

long long go(int pos, bool fl, int sum) {
  if (pos >= n)
    return sum % d == 0 ? 1 : 0;
  if (dp[pos][fl][sum % d] != -1)
    return dp[pos][fl][sum % d];

  long long ret = 0;
  for (int i = 0; i <= 9; ++i) {
    if (fl || (!fl && i <= k[pos] - '0')) {
      long long nxt = go(pos + 1, fl | (i < k[pos] - '0'), sum + i);
      add(ret, nxt);
    }
  }
  return dp[pos][fl][sum % d] = ret;
}

int main() {
  FIO;

  cin >> k >> d;
  n = (int)k.size();
  for (int i = 0; i < DIG; ++i)
    for (int j = 0; j < 2; ++j)
      for (int l = 0; l < MAXD; ++l)
        dp[i][j][l] = -1;
  long long ans = go(0, 0, 0) - 1;
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;

  oo cout << "Time: " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
string k;
ll n, d;
const ll mod = 1e9 + 7;
ll memo[100001][2][101];
ll go(ll pos, ll par, ll val) {
  if (pos == n) {
    if (val % d == 0)
      return 1;
    return 0;
  }
  if (memo[pos][par][val] != -1)
    return memo[pos][par][val];
  ll ans = 0;
  if (par == 0) {
    for (ll i = 0; i < 10; i++) {
      ans += go(pos + 1, par, (val + i) % d);
      ans %= mod;
    }
  } else {
    ans += go(pos + 1, par, (val + k[pos] - '0' + d) % d);
    ans %= mod;
    for (ll i = 0; i < k[pos] - '0'; i++) {
      ans += go(pos + 1, 0, (val + i) % d);
      ans %= mod;
    }
  }
  return memo[pos][par][val] = ans;
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  cin >> k >> d;
  memset(memo, -1, sizeof(memo));
  n = k.size();
  ll ans = (go(0, 1, 0) - 1 + mod) % mod;
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
string k;
ll n, d;
const ll mod = 1e9 + 7;
ll memo[100001][2][101];
ll go(ll pos, ll par, ll val) {
  if (pos == n) {
    if (val % d == 0)
      return 1;
    return 0;
  }
  if (memo[pos][par][val] != -1)
    return memo[pos][par][val];
  ll ans = 0;
  if (par == 0) {
    for (ll i = 0; i < 10; i++) {
      ans += go(pos + 1, par, (val + i) % d);
      ans %= mod;
    }
  } else {
    ans += go(pos + 1, par, (val + k[pos] - '0' + d) % d);
    ans %= mod;
    for (ll i = 0; i < k[pos] - '0'; i++) {
      ans += go(pos + 1, 0, (val + i) % d);
      ans %= mod;
    }
  }
  return memo[pos][par][val] = ans;
}

int main() {

  cin >> k >> d;
  memset(memo, -1, sizeof(memo));
  n = k.size();
  ll ans = (go(0, 1, 0) - 1 + mod) % mod;
  cout << ans;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (ll i = x; i < y; i++)
#define MOD 1000000007
typedef long long ll;
using namespace std;

ll dp[1001][101];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  ll d;
  cin >> s >> d;
  dp[0][0] = 1;
  FOR(i, 1, s.size()) {
    FOR(j, 0, d) {
      dp[i][j] = 0;
      FOR(k, 0, 10) {
        dp[i][j] = (dp[i][j] + dp[i - 1][(j - k + 100 * d) % d]) % MOD;
      }
    }
  }
  ll ans = dp[s.size() - 1][0] - 1;
  ll pref = 0;
  FOR(j, 1, s[0] - '0') {
    ans = (ans + dp[s.size() - 1][(100000 * d - (pref + j)) % d]) % MOD;
  }
  pref += (s[0] - '0');
  FOR(i, 1, s.size()) {
    FOR(j, 0, s[i] - '0') {
      ans = (ans + dp[s.size() - i - 1][(100000 * d - (pref + j)) % d]) % MOD;
    }
    pref += (s[i] - '0');
  }
  ans += (pref % d == 0);
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (ll i = x; i < y; i++)
#define MOD 1000000007
typedef long long ll;
using namespace std;

ll dp[10001][101];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  ll d;
  cin >> s >> d;
  dp[0][0] = 1;
  FOR(i, 1, s.size()) {
    FOR(j, 0, d) {
      dp[i][j] = 0;
      FOR(k, 0, 10) {
        dp[i][j] = (dp[i][j] + dp[i - 1][(j - k + 100 * d) % d]) % MOD;
      }
    }
  }
  ll ans = dp[s.size() - 1][0] - 1;
  ll pref = 0;
  FOR(j, 1, s[0] - '0') {
    ans = (ans + dp[s.size() - 1][(100000 * d - (pref + j)) % d]) % MOD;
  }
  pref += (s[0] - '0');
  FOR(i, 1, s.size()) {
    FOR(j, 0, s[i] - '0') {
      ans = (ans + dp[s.size() - i - 1][(100000 * d - (pref + j)) % d]) % MOD;
    }
    pref += (s[i] - '0');
  }
  ans += (pref % d == 0);
  cout << ans << '\n';
  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> pr;
typedef vector<ll> vc;
typedef unordered_map<ll, ll> umap;
#define pb emplace_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;

int main() {
  // your code goes here
  string k;
  ll n, d, pb, dp[105][1005] = {0}, sum[1005] = {0};
  cin >> k >> d;
  n = k.size();
  dp[0][0] = 1;
  rep(i, 1, n) {
    pb = k[i - 1] - '0';
    sum[i] = (sum[i - 1] + pb) % d;
    rep(j, 0, d - 1) rep(k, 0, 9) dp[(j + k) % d][i] =
        (dp[(j + k) % d][i] + dp[j][i - 1]) % mod;
    rep(k, pb + 1, 9) dp[(sum[i - 1] + k) % d][i] =
        (dp[(sum[i - 1] + k) % d][i] - 1 + mod) % mod;
  }
  cout << (dp[0][n] - 1 + mod) % mod << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> pr;
typedef vector<ll> vc;
typedef unordered_map<ll, ll> umap;
#define pb emplace_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;

int main() {
  // your code goes here
  string k;
  ll n, d, pb, dp[105][10005] = {0}, sum[1005] = {0};
  cin >> k >> d;
  n = k.size();
  dp[0][0] = 1;
  rep(i, 1, n) {
    pb = k[i - 1] - '0';
    sum[i] = (sum[i - 1] + pb) % d;
    rep(j, 0, d - 1) rep(k, 0, 9) dp[(j + k) % d][i] =
        (dp[(j + k) % d][i] + dp[j][i - 1]) % mod;
    rep(k, pb + 1, 9) dp[(sum[i - 1] + k) % d][i] =
        (dp[(sum[i - 1] + k) % d][i] - 1 + mod) % mod;
  }
  cout << (dp[0][n] - 1 + mod) % mod << endl;
  return 0;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define lsb(x) (x & -x)

using namespace std;

const int MOD = 1000000007;

inline void mod(int &x) {
  if (x >= MOD)
    x -= MOD;
}

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

  cin.tie(0);
  cout.tie(0);
  freopen("a.in", "r", stdin);

  string s;
  int d;
  cin >> s >> d;
  int ndigits = s.size();

  vector<vector<int>> dp(d, vector<int>(ndigits + 1, 0));
  dp[0][0] = 1;

  for (int i = 0; i < ndigits; i++)
    for (int r = 0; r < d; r++)
      for (int digit = 0; digit < 10; digit++)
        dp[(r + digit) % d][i + 1] =
            (dp[(r + digit) % d][i + 1] + dp[r][i]) % MOD;

  int ans = dp[0][ndigits - 1];
  ans--; // il scot pe 0
  if (ans < 0)
    ans += MOD;

  int sumofdigits = (d * 10 - (s[0] - '0')) % d;
  for (int digit = 1; digit < s[0] - '0'; digit++) {
    ans += dp[(d * 10 - digit) % d][ndigits - 1];
    mod(ans);
  }

  for (int i = 1; i < s.size(); i++) {
    for (int digit = 0; digit < s[i] - '0'; digit++) {
      ans += dp[(sumofdigits + d * 10 - digit) % d][ndigits - i - 1];
      mod(ans);
    }

    sumofdigits = (sumofdigits + d * 10 - (s[i] - '0')) % d;
  }

  if (sumofdigits == 0) {
    ans++;
    mod(ans);
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define lsb(x) (x & -x)

using namespace std;

const int MOD = 1000000007;

inline void mod(int &x) {
  if (x >= MOD)
    x -= MOD;
}

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

  cin.tie(0);
  cout.tie(0);
  // freopen("a.in", "r", stdin);

  string s;
  int d;
  cin >> s >> d;
  int ndigits = s.size();

  vector<vector<int>> dp(d, vector<int>(ndigits + 1, 0));
  dp[0][0] = 1;

  for (int i = 0; i < ndigits; i++)
    for (int r = 0; r < d; r++)
      for (int digit = 0; digit < 10; digit++)
        dp[(r + digit) % d][i + 1] =
            (dp[(r + digit) % d][i + 1] + dp[r][i]) % MOD;

  int ans = dp[0][ndigits - 1];
  ans--; // il scot pe 0
  if (ans < 0)
    ans += MOD;

  int sumofdigits = (d * 10 - (s[0] - '0')) % d;
  for (int digit = 1; digit < s[0] - '0'; digit++) {
    ans += dp[(d * 10 - digit) % d][ndigits - 1];
    mod(ans);
  }

  for (int i = 1; i < s.size(); i++) {
    for (int digit = 0; digit < s[i] - '0'; digit++) {
      ans += dp[(sumofdigits + d * 10 - digit) % d][ndigits - i - 1];
      mod(ans);
    }

    sumofdigits = (sumofdigits + d * 10 - (s[i] - '0')) % d;
  }

  if (sumofdigits == 0) {
    ans++;
    mod(ans);
  }

  cout << ans;

  return 0;
}
Problem p03178 (C++)

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

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

string K;
int D;

ll dp[10001][2][101];
// dp[i][j][k] := i - 1桁目(1-idx)まで決定済みで、これからi番目を決定する
// jが1ならばKとスレスレの状態、0ならばそうでない
// kはその時の各桁の和のmod D

int main() {
  cin >> K >> D;
  dp[0][1][0] = 1;
  for (int i = 0; i <= K.size(); i++) {
    for (int j = 0; j < 2; j++) {
      for (int amari = 0; amari <= D; amari++) {
        dp[i][j][amari] %= MOD;
        int limit = j ? K[i] - '0' : 9;
        for (int keta_val = 0; keta_val <= limit; keta_val++) {
          dp[i + 1][(!j) ? 0 : ((keta_val == limit) ? 1 : 0)]
            [(amari + keta_val) % D] += dp[i][j][amari];
        }
      }
    }
  }
  // 000...000はレグレーション違反なので、その分を引く
  cout << (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + MOD) % MOD << endl;

  return 0;
}

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

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

string K;
int D;

ll dp[10002][2][101];
// dp[i][j][k] := i - 1桁目(1-idx)まで決定済みで、これからi番目を決定する
// jが1ならばKとスレスレの状態、0ならばそうでない
// kはその時の各桁の和のmod D

int main() {
  cin >> K >> D;
  dp[0][1][0] = 1;
  for (int i = 0; i <= K.size(); i++) {
    for (int j = 0; j < 2; j++) {
      for (int amari = 0; amari <= D; amari++) {
        dp[i][j][amari] %= MOD;
        int limit = j ? K[i] - '0' : 9;
        for (int keta_val = 0; keta_val <= limit; keta_val++) {
          dp[i + 1][(!j) ? 0 : ((keta_val == limit) ? 1 : 0)]
            [(amari + keta_val) % D] += dp[i][j][amari];
        }
      }
    }
  }
  // 000...000はレグレーション違反なので、その分を引く
  cout << (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + MOD) % MOD << endl;

  return 0;
}
Problem p03178 (C++)

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

#define ll long long int

ll d, dp[2002][2002][2][2], mod = 1e9 + 7;
string a, b;

ll rec(ll pos, ll rem, bool flag1, bool flag2) {
  if (dp[pos][rem][flag1][flag2] != -1)
    return dp[pos][rem][flag1][flag2];
  if (pos == a.length())
    return !rem;
  ll ans = 0, start1 = 0, end1 = 9;
  if (!flag1)
    start1 = a[pos] - '0';
  if (!flag2)
    end1 = b[pos] - '0';
  for (int i = start1; i <= end1; i++) {
    ans = (ans + rec(pos + 1, (rem + i) % d, (flag1 | i > start1),
                     (flag2 | i < end1))) %
          mod;
  }
  return dp[pos][rem][flag1][flag2] = ans;
}

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

  cin >> b >> d;
  a = "";
  for (int i = 0; i < b.length() - 1; i++)
    a += '0';
  a += '1';
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0, 0, 0) << endl;
  return 0;
}

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

#define ll long long int

const int MAXK = 10005;
const int MAXD = 105;

ll d, dp[MAXK][MAXD][2][2], mod = 1e9 + 7;
string a, b;

ll rec(ll pos, ll rem, bool flag1, bool flag2) {
  if (dp[pos][rem][flag1][flag2] != -1)
    return dp[pos][rem][flag1][flag2];
  if (pos == a.length())
    return !rem;
  ll ans = 0, start1 = 0, end1 = 9;
  if (!flag1)
    start1 = a[pos] - '0';
  if (!flag2)
    end1 = b[pos] - '0';
  for (int i = start1; i <= end1; i++) {
    ans = (ans + rec(pos + 1, (rem + i) % d, (flag1 | i > start1),
                     (flag2 | i < end1))) %
          mod;
  }
  return dp[pos][rem][flag1][flag2] = ans;
}

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

  cin >> b >> d;
  a = "";
  for (int i = 0; i < b.length() - 1; i++)
    a += '0';
  a += '1';
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0, 0, 0) << endl;
  return 0;
}
Problem p03178 (C++)

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

#define ll long long int

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

ll mod = 1000000007;
string k;
ll d;

ll dp[10005][105][3];

ll recur(ll ind, ll rem, ll check) {
  if (ind == k.size() && rem == 0) {
    return 1;
  }
  if (ind == k.size())
    return 0;
  if (dp[ind][rem][check] != -1)
    return dp[ind][rem][check];

  ll ans = 0;
  if (check) {
    for (ll i = 0; i <= 9; i++) {
      ans = (ans + recur(ind + 1, (rem + i) % d, 1)) % mod;
    }
  } else {
    for (ll i = 0; i <= k[ind] - '0'; i++) {
      if (i < k[ind] - '0')
        ans = (ans + recur(ind + 1, (rem + i) % d, 1)) % mod;
      else
        ans = (ans + recur(ind + 1, (rem + i) % d, 0)) % mod;
    }
  }
  dp[ind][rem][check] = ans;
  return ans;
}

int main() {
  fastio();
  input();
  cin >> k >> d;
  memset(dp, -1, sizeof(dp));
  ll ans = recur(0, 0, 0) - 1;
  if (ans < 0)
    ans += mod;
  cout << ans;
}


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

#define ll long long int

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

ll mod = 1000000007;
string k;
ll d;

ll dp[10005][105][3];

ll recur(ll ind, ll rem, ll check) {
  if (ind == k.size() && rem == 0) {
    return 1;
  }
  if (ind == k.size())
    return 0;
  if (dp[ind][rem][check] != -1)
    return dp[ind][rem][check];

  ll ans = 0;
  if (check) {
    for (ll i = 0; i <= 9; i++) {
      ans = (ans + recur(ind + 1, (rem + i) % d, 1)) % mod;
    }
  } else {
    for (ll i = 0; i <= k[ind] - '0'; i++) {
      if (i < k[ind] - '0')
        ans = (ans + recur(ind + 1, (rem + i) % d, 1)) % mod;
      else
        ans = (ans + recur(ind + 1, (rem + i) % d, 0)) % mod;
    }
  }
  dp[ind][rem][check] = ans;
  return ans;
}

int main() {
  fastio();
  cin >> k >> d;
  memset(dp, -1, sizeof(dp));
  ll ans = recur(0, 0, 0) - 1;
  if (ans < 0)
    ans += mod;
  cout << ans;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 1e4, M = 7 + 1e9;
int n, d, a[N], dp[N][101][2];
string s;
vector<vector<int>> moves;
int cal(int i, int sum, bool clicked) {
  if (i == n)
    return sum % d == 0;
  int &ret = dp[i][sum][clicked];
  if (~ret)
    return ret;
  ret = 0;
  if (!clicked) {
    f(j, 0, s[i] - '0') ret = (ret + cal(i + 1, (sum + j) % d, 1)) % M;
    ret = (ret + cal(i + 1, (sum + s[i] - '0') % d, 0)) % M;
  } else
    f(j, 0, 10) ret = (ret + cal(i + 1, (sum + j) % d, 1)) % M;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  memset(dp, -1, sizeof dp);
  cin >> s >> d;
  n = (int)s.size();
  cout << (cal(0, 0, 0) - 1 + M) % M;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 1e4, M = 7 + 1e9;
int n, d, a[N], dp[N][101][2];
string s;
vector<vector<int>> moves;
int cal(int i, int sum, bool clicked) {
  if (i == n)
    return sum % d == 0;
  int &ret = dp[i][sum][clicked];
  if (~ret)
    return ret;
  ret = 0;
  if (!clicked) {
    f(j, 0, s[i] - '0') ret = (ret + cal(i + 1, (sum + j) % d, 1)) % M;
    ret = (ret + cal(i + 1, (sum + s[i] - '0') % d, 0)) % M;
  } else
    f(j, 0, 10) ret = (ret + cal(i + 1, (sum + j) % d, 1)) % M;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> s >> d;
  n = (int)s.size();
  cout << (cal(0, 0, 0) - 1 + M) % M;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const LL MOD = 1e9 + 7;
LL dp[1005][105][2], D;
vector<int> arr;
LL solve(int i, int d, int op) {
  if (i == -1)
    return d == 0;
  if (~dp[i][d][op])
    return dp[i][d][op];
  dp[i][d][op] = 0;
  int up = op ? arr[i] : 9;
  for (int k = 0; k <= up; k++)
    (dp[i][d][op] += solve(i - 1, (d + k) % D, op && (k == arr[i]))) %= MOD;
  return dp[i][d][op];
}
void marmot0814() {
  string s;
  cin >> s >> D;
  for (auto &c : s)
    arr.push_back(c - '0');
  reverse(arr.begin(), arr.end());
  memset(dp, -1, sizeof(dp));
  cout << (solve(arr.size() - 1, 0, 1) + MOD - 1) % MOD << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1, kase = 0; // cin >> t;
  while (t--) {
    // cout << "Case #" << ++kase << ":";
    marmot0814();
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const LL MOD = 1e9 + 7;
LL dp[10005][105][2], D;
vector<int> arr;
LL solve(int i, int d, int op) {
  if (i == -1)
    return d == 0;
  if (~dp[i][d][op])
    return dp[i][d][op];
  dp[i][d][op] = 0;
  int up = op ? arr[i] : 9;
  for (int k = 0; k <= up; k++)
    (dp[i][d][op] += solve(i - 1, (d + k) % D, op && (k == arr[i]))) %= MOD;
  return dp[i][d][op];
}
void marmot0814() {
  string s;
  cin >> s >> D;
  for (auto &c : s)
    arr.push_back(c - '0');
  reverse(arr.begin(), arr.end());
  memset(dp, -1, sizeof(dp));
  cout << (solve(arr.size() - 1, 0, 1) + MOD - 1) % MOD << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1, kase = 0; // cin >> t;
  while (t--) {
    // cout << "Case #" << ++kase << ":";
    marmot0814();
  }
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MSIZE = 101;
const long long mod = 1e9 + 7;

long long dp[MSIZE][101];

void Precalc(int d) {
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1;
  for (int i = 1; i < MSIZE; i++) {
    for (int c = 0; c < 10; c++) {
      for (int p = 0; p < d; p++) {
        int n = (c + p) % d;
        dp[i][n] += dp[i - 1][p];
        dp[i][n] %= mod;
      }
    }
  }
}

long long Get(int len, int cur, int d) { return dp[len][(d - (cur % d)) % d]; }

long long Get(string &arr, int d, int i, int n, int cur) {
  if (i == n)
    return (cur % d == 0);
  long long ans = 0;
  for (int dig = 0; dig < arr[i]; dig++) {
    ans += Get(n - 1 - i, cur + dig, d);
    ans %= mod;
  }
  ans += Get(arr, d, i + 1, n, cur + arr[i]);
  ans %= mod;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  string k;
  cin >> k;
  int d;
  cin >> d;
  Precalc(d);
  for (char &ch : k)
    ch -= '0';
  cout << (mod - 1 + Get(k, d, 0, k.size(), 0)) % mod << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MSIZE = 10101;
const long long mod = 1e9 + 7;

long long dp[MSIZE][101];

void Precalc(int d) {
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1;
  for (int i = 1; i < MSIZE; i++) {
    for (int c = 0; c < 10; c++) {
      for (int p = 0; p < d; p++) {
        int n = (c + p) % d;
        dp[i][n] += dp[i - 1][p];
        dp[i][n] %= mod;
      }
    }
  }
}

long long Get(int len, int cur, int d) { return dp[len][(d - (cur % d)) % d]; }

long long Get(string &arr, int d, int i, int n, int cur) {
  if (i == n)
    return (cur % d == 0);
  long long ans = 0;
  for (int dig = 0; dig < arr[i]; dig++) {
    ans += Get(n - 1 - i, cur + dig, d);
    ans %= mod;
  }
  ans += Get(arr, d, i + 1, n, cur + arr[i]);
  ans %= mod;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  string k;
  cin >> k;
  int d;
  cin >> d;
  Precalc(d);
  for (char &ch : k)
    ch -= '0';
  cout << (mod - 1 + Get(k, d, 0, k.size(), 0)) % mod << endl;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string.h>
using namespace std;
const int mod = 1000000007;

int dp[12345][123];
string k;
int a[123];
int l, d;
long long ans;
long long dfs(int pos, int site, bool flag) {
  // cout<<"POS:"<<pos<<"   SITE:"<<site<<"   FLAG:"<<flag<<endl;
  if (pos == l + 1)
    return dp[pos][site] = (site == 0); // site等于0，dp等于1
  if (!flag && dp[pos][site] != -1)
    return dp[pos][site];
  int len;
  if (flag)
    len = a[pos];
  else
    len = 9;
  long long sum = 0;
  for (int i = 0; i <= len; ++i) {
    sum += dfs(pos + 1, (site + i) % d, flag && i == len);
    sum %= mod;
  }
  if (!flag)
    dp[pos][site] = sum;
  return sum;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> k >> d;
  l = k.size();
  for (int i = 1; i <= l; i++) {
    a[i] = k[i - 1] - '0';
  }

  ans = (dfs(1, 0, true) - 1 + mod) % mod;

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string.h>
using namespace std;
const int mod = 1000000007;

int dp[12345][123];
string k;
int a[12345];
int l, d;
long long ans;
long long dfs(int pos, int site, bool flag) {
  // cout<<"POS:"<<pos<<"   SITE:"<<site<<"   FLAG:"<<flag<<endl;
  if (pos == l + 1)
    return dp[pos][site] = (site == 0); // site等于0，dp等于1
  if (!flag && dp[pos][site] != -1)
    return dp[pos][site];
  int len;
  if (flag)
    len = a[pos];
  else
    len = 9;
  long long sum = 0;
  for (int i = 0; i <= len; ++i) {
    sum += dfs(pos + 1, (site + i) % d, flag && i == len);
    sum %= mod;
  }
  if (!flag)
    dp[pos][site] = sum;
  return sum;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> k >> d;
  l = k.size();
  for (int i = 1; i <= l; i++) {
    a[i] = k[i - 1] - '0';
  }

  ans = (dfs(1, 0, true) - 1 + mod) % mod;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;

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

  string K;
  cin >> K;
  int n = K.size();
  int D;
  cin >> D;

  vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(D, vector<ll>(2, 0)));
  dp[0][0][1] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < D; j++) {
      for (int b = 0; b < 2; b++) {
        for (int k = 0; k < 10; k++) {
          int ni = (i + 1) % 2, nj = (j + k) % D, nb = 0;
          int curNum = K[i] - '0';

          if (b) {
            if (k > curNum) {
              break;
            } else if (k == curNum) {
              nb = 1;
            }
          }
          dp[ni][nj][nb] += dp[(i % 2)][j][b];
          dp[ni][nj][nb] %= mod;
        }
        dp[i][j][b] = 0;
      }
    }
  }

  ll ans = dp[(n % 2)][0][0] + dp[(n % 2)][0][1];
  ans %= mod;
  ans--;
  ans %= mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << '\n';
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;

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

  string K;
  cin >> K;
  int n = K.size();
  int D;
  cin >> D;

  vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(D, vector<ll>(2, 0)));
  dp[0][0][1] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < D; j++) {
      for (int b = 0; b < 2; b++) {
        for (int k = 0; k < 10; k++) {
          int ni = (i + 1) % 2, nj = (j + k) % D, nb = 0;
          int curNum = K[i] - '0';

          if (b) {
            if (k > curNum) {
              break;
            } else if (k == curNum) {
              nb = 1;
            }
          }
          dp[ni][nj][nb] += dp[(i % 2)][j][b];
          dp[ni][nj][nb] %= mod;
        }
        dp[i % 2][j][b] = 0;
      }
    }
  }

  ll ans = dp[(n % 2)][0][0] + dp[(n % 2)][0][1];
  ans %= mod;
  ans--;
  ans %= mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << '\n';
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
string k;
ll d;
ll dp[10000][100][2];

ll solve(ll n, ll md, ll s) {
  if ((n == k.length()) && (md == 0))
    return 1;
  if (n == k.length())
    return 0;
  if (dp[n][md][s] != -1)
    return dp[n][md][s];
  dp[n][md][s] = 0;
  int mx = (s == 0 ? k[n] : '9') - '0';
  for (ll i = 0; i <= mx; i++) {
    ll nmd = (md + i) % d;
    ll ns = (s || (i < mx)) ? 1 : 0;
    dp[n][md][s] = (dp[n][md][s] + solve(n + 1, nmd, ns)) % mod;
  }
  return dp[n][md][s];
}
int main() {
  cin >> k >> d;
  for (ll i = 0; i <= k.length(); i++)
    for (ll j = 0; j < d; j++)
      dp[i][j][0] = dp[i][j][1] = -1;
  cout << ((solve(0, 0, 0) + mod - 1) % mod) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
string k;
ll d;
ll dp[20000][500][2];

ll solve(ll n, ll md, ll s) {
  if ((n == k.length()) && (md == 0))
    return 1;
  if (n == k.length())
    return 0;
  if (dp[n][md][s] != -1)
    return dp[n][md][s];
  dp[n][md][s] = 0;
  int mx = (s == 0 ? k[n] : '9') - '0';
  for (ll i = 0; i <= mx; i++) {
    ll nmd = (md + i) % d;
    ll ns = (s || (i < mx)) ? 1 : 0;
    dp[n][md][s] = (dp[n][md][s] + solve(n + 1, nmd, ns)) % mod;
  }
  return dp[n][md][s];
}
int main() {
  cin >> k >> d;
  for (ll i = 0; i <= k.length(); i++)
    for (ll j = 0; j < d; j++)
      dp[i][j][0] = dp[i][j][1] = -1;
  cout << ((solve(0, 0, 0) + mod - 1) % mod) << endl;
}
Problem p03178 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int dp[10001][10][2];

int solve(string &K, int &D, int pos, int d, bool tight) {
  if (dp[pos][d][tight] != -1)
    return dp[pos][d][tight];

  int ub = (tight) ? (K[pos] - '0') : 9;
  int len = K.length() - 1;
  if (pos == len) {
    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 + solve(K, D, pos + 1, (D + d - x % D) % D, tight && (x == ub))) %
        mod;
  return dp[pos][d][tight] = ans;
}

int32_t main() {
  string K;
  int D;
  cin >> K >> D;
  memset(dp, -1, sizeof dp);
  cout << (mod + solve(K, D, 0, 0, 1) - 1) % mod;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int dp[10001][101][2];

int solve(string &K, int &D, int pos, int d, bool tight) {
  if (dp[pos][d][tight] != -1)
    return dp[pos][d][tight];

  int ub = (tight) ? (K[pos] - '0') : 9;
  int len = K.length() - 1;
  if (pos == len) {
    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 + solve(K, D, pos + 1, (D + d - x % D) % D, tight && (x == ub))) %
        mod;
  return dp[pos][d][tight] = ans;
}

int32_t main() {
  string K;
  int D;
  cin >> K >> D;
  memset(dp, -1, sizeof dp);
  cout << (mod + solve(K, D, 0, 0, 1) - 1) % mod;
  return 0;
}