Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[11000][13];

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

  string S;
  cin >> S;
  int len = S.size();
  reverse(ALL(S));

  REP(i, 13) { dp[0][i] = 0; }

  if (S[0] == '?') {
    REP(i, 10) { dp[0][i] = 1; }
  } else {
    int index = S[0] - '0';
    dp[0][index] = 1;
  }

  int num = 1;
  FOR(i, 1, len) {
    // cout << i << ":" << S[i] << endl;

    num *= 10;
    num %= 13;

    if (S[i] == '?') {
      REP(j, 10) {
        REP(k, 13) {
          int index = (k + j * num) % 13;
          dp[i][index] += dp[i - 1][k];
          dp[i][index] %= MOD;
        }
      }
    } else {
      REP(k, 13) {
        int j = (S[i] - '0');
        int index = (k + j * num) % 13;
        dp[i][index] += dp[i - 1][k];
        dp[i][index] %= MOD;
      }
    }
  }

  cout << dp[len - 1][5] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[110000][13];

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

  string S;
  cin >> S;
  int len = S.size();
  reverse(ALL(S));

  REP(i, 13) { dp[0][i] = 0; }

  if (S[0] == '?') {
    REP(i, 10) { dp[0][i] = 1; }
  } else {
    int index = S[0] - '0';
    dp[0][index] = 1;
  }

  int num = 1;
  FOR(i, 1, len) {
    // cout << i << ":" << S[i] << endl;

    num *= 10;
    num %= 13;

    if (S[i] == '?') {
      REP(j, 10) {
        REP(k, 13) {
          int index = (k + j * num) % 13;
          dp[i][index] += dp[i - 1][k];
          dp[i][index] %= MOD;
        }
      }
    } else {
      REP(k, 13) {
        int j = (S[i] - '0');
        int index = (k + j * num) % 13;
        dp[i][index] += dp[i - 1][k];
        dp[i][index] %= MOD;
      }
    }
  }

  cout << dp[len - 1][5] << endl;

  return 0;
}