Problem p00990 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n; // 桁数
string str;
int kouho[11];
int sum = 0;
int dp[100001][10]; // dp[i][j]インデックスj以降を使ってmod10の値jがいくつあるか
int calc(int x, int a) {
  if (x % 2 == 1)
    a *= 2;
  return a / 10 + a % 10;
}

int main() {
  int m;
  cin >> n;
  cin >> str;
  cin >> m;
  int ans = 0;
  for (int i = 0; i < m; i++) {
    cin >> kouho[i];
  }
  fill(dp[0], dp[100001], 0);
  reverse(str.begin(), str.end());
  if (str[0] != '*')
    dp[0][str[0] - '0'] = 1;
  else
    for (int i = 0; i < m; i++) {
      dp[0][kouho[i]] = 1;
    }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      if (str[i] == '*') {
        for (int k = 0; k < m; k++) {
          dp[i][(calc(i, kouho[k]) + j) % 10] += dp[i - 1][j];
        }
      } else
        dp[i][(calc(i, str[i] - '0') + j) % 10] += dp[i - 1][j];
    }
  }
  cout << dp[n - 1][0] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n; // 桁数
string str;
int kouho[11];
int sum = 0;
int dp[10000001]
      [10]; // dp[i][j]インデックスj以降を使ってmod10の値jがいくつあるか
int calc(int x, int a) {
  if (x % 2 == 1)
    a *= 2;
  return a / 10 + a % 10;
}

int main() {
  int m;
  cin >> n;
  cin >> str;
  cin >> m;
  int ans = 0;
  for (int i = 0; i < m; i++) {
    cin >> kouho[i];
  }
  fill(dp[0], dp[10000001], 0);
  reverse(str.begin(), str.end());
  if (str[0] != '*')
    dp[0][str[0] - '0'] = 1;
  else
    for (int i = 0; i < m; i++) {
      dp[0][kouho[i]] = 1;
    }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      if (str[i] == '*') {
        for (int k = 0; k < m; k++) {
          dp[i][(calc(i, kouho[k]) + j) % 10] += dp[i - 1][j];
        }
      } else
        dp[i][(calc(i, str[i] - '0') + j) % 10] += dp[i - 1][j];
    }
  }
  cout << dp[n - 1][0] << endl;
  return 0;
}