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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N;
string s;
int M;
int a[10];
int L;
int toidx[10];
int dfs(int idx, int sum) {
  if (idx == L) {
    if (sum % 10 == 0)
      return 1;
    else
      return 0;
  } else {
    int res = 0;
    REP(i, M) {
      s[toidx[idx]] = a[i] + '0';
      int n = a[i];
      if ((N - toidx[idx]) % 2 == 0)
        n *= 2;
      if (n >= 10)
        n = n / 10 + n % 10;
      res += dfs(idx + 1, sum + n);
    }
    return res;
  }
}

int main() {
  cin >> N >> s;
  L = 0;
  REP(i, N) if (s[i] == '*') toidx[L++] = i;
  cin >> M;
  REP(i, M) cin >> a[i];
  int sum = 0;
  REP(i, N) if (s[i] != '*') {
    int n = s[i] - '0';
    if ((N - i) % 2 == 0)
      n *= 2;
    if (n >= 10)
      n = n / 10 + n % 10;
    sum += n;
  }
  cout << dfs(0, sum % 10) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N;
string s;
int M;
int a[10];
int L;
int toidx[10];
int dfs(int idx) {
  if (idx == L) {
    int sum = 0;
    REP(i, N) {
      int n = s[i] - '0';
      if ((N - i) % 2 == 0)
        n *= 2;
      if (n >= 10)
        n = n / 10 + n % 10;
      sum += n;
    }
    if (sum % 10 == 0)
      return 1;
    else
      return 0;
  } else {
    int res = 0;
    REP(i, M) {
      s[toidx[idx]] = a[i] + '0';
      res += dfs(idx + 1);
    }
    return res;
  }
}

int main() {
  cin >> N >> s;
  L = 0;
  REP(i, N) if (s[i] == '*') toidx[L++] = i;
  cin >> M;
  REP(i, M) cin >> a[i];
  cout << dfs(0) << endl;
  return 0;
}