alexjercan/bugnet huggingface dataset:
Problem p00965 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define FRER() freopen("i.txt", "r", stdin);

using namespace std;

const int maxn = 200000 + 5;

int l[maxn], r[maxn], ml[maxn] = {0}, mr[maxn] = {0}, sum[maxn] = {0};

int main() {
  int n, ans1 = 0, ans2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &l[i], &r[i]);
    ++sum[l[i]];
    --sum[r[i]];
    ++ml[l[i]];
    ++mr[r[i]];
  }
  for (int i = 1; i < maxn; ++i) {
    sum[i] += sum[i - 1];
    ml[i] += ml[i - 1];
    mr[i] += mr[i - 1];
    ans2 = max(ans2, sum[i]);
  }
  for (int i = 0; i < n; ++i) {
    ans1 = max(ans1, ml[r[i] - 1] - mr[l[i]]);
  }
  printf("%d %d\\n", ans1, ans2);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define FRER() freopen("i.txt", "r", stdin);

using namespace std;

const int maxn = 100000 + 5;

int l[maxn], r[maxn], ml[maxn] = {0}, mr[maxn] = {0}, sum[maxn] = {0};

int main() {
  int n, ans1 = 0, ans2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &l[i], &r[i]);
    ++sum[l[i]];
    --sum[r[i]];
    ++ml[l[i]];
    ++mr[r[i]];
  }
  for (int i = 1; i < maxn; ++i) {
    sum[i] += sum[i - 1];
    ml[i] += ml[i - 1];
    mr[i] += mr[i - 1];
    ans2 = max(ans2, sum[i]);
  }
  for (int i = 0; i < n; ++i) {
    ans1 = max(ans1, ml[r[i] - 1] - mr[l[i]]);
  }
  printf("%d %d\\n", ans1, ans2);
  return 0;
}
Problem p00965 (C++) — buggy submission, verdict: Runtime Error

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

int l[200010], r[200010], lqz[200010], rqz[200010], s[200010];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &l[i], &r[i]);
    lqz[l[i]]++;
    rqz[r[i]]++;
    s[l[i]]++;
    s[r[i]]--;
  }
  for (int i = 1; i <= 100000; ++i) {
    lqz[i] += lqz[i - 1];
    rqz[i] += rqz[i - 1];
    s[i] += s[i - 1];
  }
  int ans1(0), ans2(0);
  for (int i = 0; i < n; ++i)
    ans1 = max(ans1, lqz[r[i] - 1] - rqz[l[i]]);
  for (int i = 1; i <= 100000; ++i)
    ans2 = max(ans2, s[i]);
  printf("%d %d\\n", ans1, ans2);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
struct po {
  int x;
  int y;
} a[100005];

int head[100005], tail[100005];
int num[100005];
int n;
int main() {
  scanf("%d ", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].x, &a[i].y);
    num[a[i].x]++;
    num[a[i].y]--;
    head[a[i].x + 1]++;
    tail[a[i].y]++;
  }
  int now = 0, Max = 0;
  for (int i = 1; i <= 100000; i++) {
    now += num[i];
    Max = max(now, Max);
  }
  for (int i = 2; i <= 100000; i++) {
    head[i] += head[i - 1];
    tail[i] += tail[i - 1];
  }
  int m = 0;
  for (int i = 1; i <= n; i++) {
    m = max(m, head[a[i].y] - tail[a[i].x]);
  }
  printf("%d %d\\n", m, Max);
}
Problem p00965 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn = 1e5 + 5;
int a[maxn];
int numl[maxn], numr[maxn];

struct ed {
  int l, r;
} eds[2 * maxn];

int main() {
  int n;
  int rd = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &eds[i].l, &eds[i].r);
    a[eds[i].l]++;
    a[eds[i].r]--;
    numl[eds[i].l]++;
    numr[eds[i].r]++;
    rd = max(rd, eds[i].r);
  }
  int ma1 = 0, num = 0, ma2 = 0;
  for (int i = 1; i <= rd; i++) {
    a[i] += a[i - 1];
    ma2 = max(ma2, a[i]);
    numl[i] += numl[i - 1];
    numr[i] += numr[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    int ans = numl[eds[i].r - 1] - numr[eds[i].l];
    ma1 = max(ma1, ans);
  }
  printf("%d %d\\n", ma1, ma2);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int a[100005];
int b[100005];
int l[100005];
int r[100005];
int s[100005];
int main() {
  int i, n;
  int x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d %d", &x, &y);
    a[x]++;
    b[y]++;
    l[i] = x;
    r[i] = y;
    s[x]++;
    s[y]--;
  }
  for (i = 1; i <= 100000; i++) {
    a[i] = a[i] + a[i - 1];
    b[i] = b[i] + b[i - 1];
    s[i] = s[i] + s[i - 1];
  }
  int ans1 = 0, ans2 = 0;
  for (i = 1; i <= n; i++) {
    ans1 = max(ans1, a[r[i] - 1] - b[l[i]]);
  }
  for (i = 1; i <= 100000; i++) {
    ans2 = max(ans2, s[i]);
  }
  printf("%d %d\\n", ans1, ans2);
  return 0;
}
Problem p00966 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
int L, n, m, Q;
int Y[1010], H[1010];
char Res[1010];
int w[1010];
char p[1010];
map<int, char> Map[1010];
vector<int> G[1010];
int Loc(int x) { return (lower_bound(Y, Y + m + 2, x + 1) - Y) - 1; }
int main() {
  int i;
  scanf("%d%d%d%d", &L, &n, &m, &Q);
  for (i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
    scanf("%s", p + i);
  }
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &Y[i], &H[i]);
  }
  Y[m + 1] = L + 1;
  for (i = 0; i <= m; i++)
    if (!H[i])
      H[i] = -L;
  for (i = 1; i <= n; i++) {
    int x = w[i];
    while (1) {
      int t = Loc(x);
      Map[t][x % (Y[t] - H[t])] = p[i];
      if (H[t] <= 0)
        break;
      x -= ((x - Y[t]) / (Y[t] - H[t]) + 1) * (Y[t] - H[t]);
    }
  }
  for (i = 1; i <= Q; i++) {
    int x;
    char u = '?';
    scanf("%d", &x);
    while (1) {
      int t = Loc(x);
      if (Map[t].count(x % (Y[t] - H[t])))
        u = Map[t][x % (Y[t] - H[t])];
      if (H[t] <= 0)
        break;
      x -= ((x - Y[t]) / (Y[t] - H[t]) + 1) * (Y[t] - H[t]);
    }
    printf("%c", u);
  }
  printf("\\n");
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
int L, n, m, Q;
int Y[1010], H[1010];
char Res[1010];
int w[1010];
char p[1010];
map<int, char> Map[1010];
vector<int> G[1010];
int Loc(int x) { return (lower_bound(Y, Y + m + 2, x + 1) - Y) - 1; }
int main() {
  int i;
  char p[3];
  scanf("%d%d%d%d", &L, &n, &m, &Q);
  for (i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
    scanf("%s", p + i);
  }
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &Y[i], &H[i]);
  }
  Y[m + 1] = L + 1;
  H[0] = -L;
  for (i = 1; i <= n; i++) {
    int x = w[i];
    while (1) {
      int t = Loc(x);
      Map[t][x % (Y[t] - H[t])] = p[i];
      if (H[t] <= 0)
        break;
      x -= ((x - Y[t]) / (Y[t] - H[t]) + 1) * (Y[t] - H[t]);
    }
  }
  for (i = 1; i <= Q; i++) {
    int x;
    char u = '?';
    scanf("%d", &x);
    while (1) {
      int t = Loc(x);
      if (Map[t].count(x % (Y[t] - H[t])))
        u = Map[t][x % (Y[t] - H[t])];
      if (H[t] <= 0)
        break;
      x -= ((x - Y[t]) / (Y[t] - H[t]) + 1) * (Y[t] - H[t]);
    }
    printf("%c", u);
  }
  printf("\\n");
}
Problem p00990 (C++) — buggy submission, verdict: Runtime Error

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

int ans, d[11], n, m, num[11], cnt, S, c;
string s;

bool check() {

  int sum = 0;

  for (int i = 0; i < cnt; i++) {

    int di = d[i];

    if (i < c)
      di *= 2;

    if (di >= 10)
      di = di % 10 + di / 10;

    sum += di;
  }

  return (sum + S) % 10 == 0;
}

void dfs(int x) {

  if (x == cnt) {

    if (check())
      ans++;

    return;
  }

  for (int i = 0; i < m; i++) {
    d[x] = num[i];
    dfs(x + 1);
  }
}

int main() {

  cin >> n;

  cin >> s;

  reverse(s.begin(), s.end());

  for (int i = 0; i < n; i++)
    if (s[i] == '*') {
      cnt++;
      if (i % 2)
        c++;
    }

  for (int i = 0; i < n; i++) {
    if (s[i] == '*')
      continue;
    int t = s[i] - '0';
    if (i % 2)
      t *= 2;
    if (t >= 10)
      t = t % 10 + t / 10;
    S += t;
  }

  cin >> m;

  for (int i = 0; i < m; i++)
    cin >> num[i];

  dfs(0);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int ans, d[11], n, m, num[11], cnt;
string s;

bool check() {

  int idx = 0;

  int cp[11];

  for (int i = 0; i < n; i++) {
    if (s[i] == '*')
      cp[i] = d[idx++];
    else
      cp[i] = s[i] - '0';
  }

  int sum = 0;

  for (int i = 0; i < n; i++) {
    if (i % 2)
      cp[i] *= 2;
    if (cp[i] >= 10)
      cp[i] = cp[i] % 10 + cp[i] / 10;
    sum += cp[i];
  }

  return sum % 10 == 0;
}

void dfs(int x) {

  if (x == cnt) {

    if (check())
      ans++;

    return;
  }

  for (int i = 0; i < m; i++) {
    d[x] = num[i];
    dfs(x + 1);
  }
}

int main() {

  cin >> n;

  cin >> s;

  reverse(s.begin(), s.end());

  for (int i = 0; i < n; i++)
    if (s[i] == '*')
      cnt++;

  cin >> m;

  for (int i = 0; i < m; i++)
    cin >> num[i];

  dfs(0);

  cout << ans << endl;

  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int search(vector<int> &v, vector<int> &w, vector<vector<int>> &x, int id) {
  if (id == v.size())
    return x[id][0];
  if (v[id] != -1) {
    int num = v[id];
    if (id % 2 != 0)
      num *= 2;
    if (num > 9)
      num = (num / 10) + (num % 10);
    for (int i = 0; i < 10; i++) {
      x[id + 1][(i + num) % 10] += x[id][i];
    }
    return search(v, w, x, id + 1);
  }
  for (int i = 0; i < w.size(); i++) {
    int num = w[i];
    if (id % 2 != 0)
      num *= 2;
    if (num > 9)
      num = (num / 10) + (num % 10);
    for (int j = 0; j < 10; j++) {
      x[id + 1][(j + num) % 10] += x[id][j];
    }
  }
  return search(v, w, x, id + 1);
}
int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    char c = str[i];
    if (c == '*')
      v[n - 1 - i] = -1;
    else
      v[n - 1 - i] = c - '0';
  }
  int m;
  cin >> m;
  vector<int> w(m);
  for (int i = 0; i < m; i++) {
    cin >> w[i];
  }
  vector<vector<int>> x(n + 1, vector<int>(10, 0));
  x[0][0] = 1;
  int ret = search(v, w, x, 0);
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int search(vector<int> &v, vector<int> &w, int id, int sum) {
  if (id == v.size()) {
    if (sum % 10 == 0)
      return 1;
    else
      return 0;
  }
  if (v[id] != -1) {
    int num = v[id];
    if (id % 2 != 0)
      num *= 2;
    if (num > 9)
      num = (num / 10) + (num % 10);
    return search(v, w, id + 1, sum + num);
  }
  int count = 0;
  for (int i = 0; i < w.size(); i++) {
    int num = w[i];
    if (id % 2 != 0)
      num *= 2;
    if (num > 9)
      num = (num / 10) + (num % 10);
    count += search(v, w, id + 1, sum + num);
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    char c = str[i];
    if (c == '*')
      v[n - 1 - i] = -1;
    else
      v[n - 1 - i] = c - '0';
  }
  cin >> n;
  vector<int> w(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  int ret = search(v, w, 0, 0);
  cout << ret << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

// 41
#include <iostream>

using namespace std;

int n;

int d(int p, int i) {
  int j = i * 2;
  return (p % 2) ? j / 10 + j % 10 : i;
}

int main() {
  char ID[100001];
  int m;
  int a[10];
  cin >> n >> ID >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  static int dp[100001][10];
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      if (ID[n - 1 - i] != '*') {
        dp[i + 1][(j + d(i, ID[n - 1 - i] - '0')) % 10] += dp[i][j];
      } else {
        for (int k = 0; k < m; k++) {
          dp[i + 1][(j + d(i, a[k])) % 10] += dp[i][j];
        }
      }
    }
  }
  cout << dp[n][0] << endl;
  return 0;
}


Error: TLE

Fixed submission:

// 41
#include <iostream>

using namespace std;

int n;
char ID[100001];
int m;
int a[10];

int dfs(int s, int p) {
  if (p == n) {
    return s % 10 == 0;
  } else {
    if (ID[n - 1 - p] == '*') {
      int r = 0;
      for (int i = 0; i < m; i++) {
        int d = a[i] * 2;
        r += dfs(s + ((p % 2) ? d / 10 + d % 10 : a[i]), p + 1);
      }
      return r;
    } else {
      return dfs(s, p + 1);
    }
  }
}

int main() {
  cin >> n >> ID >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  int s = 0;
  for (int i = 0; i < n; i++) {
    if (ID[n - 1 - i] != '*') {
      int d = (ID[n - 1 - i] - '0') * 2;
      s += ((i % 2) ? d / 10 + d % 10 : ID[n - 1 - i] - '0');
    }
  }
  cout << dfs(s, 0) << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {

  int n;
  string ID;
  int m;
  int in;
  int wa;
  int dp[2][11] = {0};
  int ans;
  vector<int> a;

  cin >> n;
  cin >> ID;
  cin >> m;
  while (m--) {
    cin >> in;
    a.push_back(in);
  }

  dp[0][0] = 1;

  for (int i = 1; i <= ID.size(); i++) {
    for (int j = 10; j >= 0; j--) {
      if (dp[0][j] > 0) {
        if (ID[ID.size() - i] == '*') {
          for (int k = 0; k < a.size(); k++) {
            if (i % 2 == 0)
              wa = a[k] * 2;
            else
              wa = a[k];
            if (wa >= 10)
              wa = wa / 10 + wa % 10;
            dp[1][(j + wa) % 10] += dp[0][j];
          }
        } else {
          if (i % 2 == 0)
            wa = (ID[ID.size() - i] - '0') * 2;
          else
            wa = ID[ID.size() - i] - '0';
          if (wa >= 10)
            wa = wa / 10 + wa % 10;
          dp[1][(j + wa) % 10] += dp[0][j];
        }
      }
    }
    for (int j = 10; j >= 0; j--) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }

  cout << dp[0][0] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define NMAX 50

int main() {

  int n;
  string ID;
  int m;
  int in;
  int wa;
  int dp[2][100001] = {0};
  int ans;
  vector<int> a;

  cin >> n;
  cin >> ID;
  cin >> m;
  while (m--) {
    cin >> in;
    a.push_back(in);
  }

  dp[0][0] = 1;

  for (int i = 1; i <= ID.size(); i++) {
    for (int j = 100000; j >= 0; j--) {
      if (dp[0][j] > 0) {
        if (ID[ID.size() - i] == '*') {
          for (int k = 0; k < a.size(); k++) {
            if (i % 2 == 0)
              wa = a[k] * 2;
            else
              wa = a[k];
            if (wa >= 10)
              wa = wa / 10 + wa % 10;
            dp[1][j + wa] += dp[0][j];
          }
        } else {
          if (i % 2 == 0)
            wa = (ID[ID.size() - i] - '0') * 2;
          else
            wa = ID[ID.size() - i] - '0';
          if (wa >= 10)
            wa = wa / 10 + wa % 10;
          dp[1][j + wa] += dp[0][j];
        }
      }
    }
    for (int j = 100000; j >= 0; j--) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }

  ans = 0;
  for (int i = 10; i < 100001; i += 10) {
    ans += dp[0][i];
  }

  cout << ans << endl;

  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int n, m, a[10], len;
char ID[100001];

int f(int a) {
  a *= 2;
  return a < 10 ? a : a / 10 + a % 10;
}

int dp[100000][10];
int dfs(int i, int sum) {
  if (i == len)
    return sum % 10 == 0 ? 1 : 0;

  if (dp[i][sum % 10] != -1)
    return dp[i][sum % 10];

  int res = 0;
  if (ID[i] == '*') {
    if (i % 2 == 0)
      rep(j, m) res += dfs(i + 1, sum + a[j]);
    else
      rep(j, m) res += dfs(i + 1, sum + f(a[j]));
  } else {
    if (i % 2 == 0)
      res = dfs(i + 1, sum + ID[i] - '0');
    else
      res = dfs(i + 1, sum + f(ID[i] - '0'));
  }
  return dp[i][sum % 10] = res;
}

int main() {
  scanf("%d%s%d", &n, ID, &m);
  rep(i, m) scanf("%d", a + i);

  len = strlen(ID);
  reverse(ID, ID + len);

  memset(dp, -1, sizeof dp);
  printf("%d\\n", dfs(0, 0));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int n, m, a[10], len;
char ID[100001];

int f(int a) {
  a *= 2;
  return a < 10 ? a : a / 10 + a % 10;
}

int dfs(int i, int sum) {
  if (i == len)
    return sum % 10 == 0 ? 1 : 0;

  int res = 0;
  if (ID[i] == '*') {
    if (i % 2 == 0)
      rep(j, m) res += dfs(i + 1, sum + a[j]);
    else
      rep(j, m) res += dfs(i + 1, sum + f(a[j]));
  } else {
    if (i % 2 == 0)
      res = dfs(i + 1, sum + ID[i] - '0');
    else
      res = dfs(i + 1, sum + f(ID[i] - '0'));
  }
  return res;
}

int main() {
  scanf("%d%s%d", &n, ID, &m);
  rep(i, m) scanf("%d", a + i);

  len = strlen(ID);
  reverse(ID, ID + len);

  printf("%d\\n", dfs(0, 0));

  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n, m, a[10];
string ID;
int ans = 0;
int even[] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
vector<int> pos;

void solve(unsigned int idx, int sum) {
  if (idx == pos.size()) {
    if (sum % 10 == 0)
      ans++;
    return;
  }
  for (int i = 0; i < m; i++) {
    int hoge = (pos[idx] % 2 ? even[a[i]] : a[i]);
    solve(idx + 1, sum + hoge);
  }
  return;
}

int main() {
  cin >> n >> ID >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  reverse(ID.begin(), ID.end());
  for (int i = 1; i < n; i += 2) {
    if (ID[i] == '*')
      continue;
    ID[i] = even[ID[i] - '0'] + '0';
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (ID[i] != '*')
      sum += ID[i] - '0';
    else
      pos.push_back(i);
  }
  solve(0, sum);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, m, a[10];
string ID;
int ans = 0;
int even[] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};

void solve(int idx, int sum) {
  if (idx == n) {
    if (sum % 10 == 0)
      ans++;
    return;
  }
  if (ID[idx] != '*') {
    solve(idx + 1, sum + (ID[idx] - '0'));
  } else {
    for (int i = 0; i < m; i++) {
      int hoge = (idx % 2 ? even[a[i]] : a[i]);
      solve(idx + 1, sum + hoge);
    }
  }
  return;
}

int main() {
  cin >> n >> ID >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  reverse(ID.begin(), ID.end());
  for (int i = 1; i < n; i += 2) {
    if (ID[i] == '*')
      continue;
    ID[i] = even[ID[i] - '0'] + '0';
  }
  solve(0, 0);
  cout << ans << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int a[20], pos[10];
int n, m, cnt = 0;
int ans = 0, sum = 0;

void dfs(int size, int Sum) {
  if (size == cnt) {
    if ((sum + Sum) % 10 == 0)
      ans++;
    return;
  }
  rep(i, m) {
    if (pos[size] % 2 == 1)
      dfs(size + 1, Sum + a[i] * 2 % 10 + a[i] * 2 / 10);
    else
      dfs(size + 1, Sum + a[i]);
  }
}
int main() {
  string id;
  cin >> n >> id >> m;
  rep(i, m) scanf("%d", &a[i]);
  rep(i, n) {
    if (id[i] == '*')
      pos[cnt++] = n - i - 1;
    else if ((n - i - 1) % 2 == 1)
      sum += (id[i] - '0') * 2 % 10 + (id[i] - '0') * 2 / 10;
    else
      sum += id[i] - '0';
  }
  dfs(0, 0);
  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int a[20], pos[10];
int n, m, cnt = 0;
multiset<int> se;

void dfs(int size, int sum) {
  if (size == cnt) {
    se.insert(sum);
    return;
  }
  rep(i, m) {
    if (pos[size] & 1) {
      if (a[i] * 2 > 9)
        dfs(size + 1, sum + (a[i] * 2) % 10 + 1);
      else
        dfs(size + 1, sum + a[i] * 2);
    } else
      dfs(size + 1, sum + a[i]);
  }
}
int main() {
  string id;
  cin >> n >> id >> m;
  rep(i, m) scanf("%d", &a[i]);
  int sum = 0;
  rep(i, n) {
    if (id[i] == '*')
      pos[cnt++] = i;
    else if (i & 1) {
      if ((id[i] - '0') * 2 > 9)
        sum += (int)(id[i] - '0') % 10 + 1;
      else
        sum += (int)(id[i] - '0') * 2;
    } else
      sum += id[i] - '0';
  }
  dfs(0, 0);
  int ans = 0;
  for (int i : se) {
    if ((sum + i) % 10 == 0)
      ans++;
  }
  printf("%d\\n", ans);
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, m, *id, *a, result = 0, known = 0;
vector<int> undefined;
bool isCollectID(void) {
  int sum = 0;
  for (int i = 0; i < undefined.size(); i++) {
    int j = undefined[i];
    if ((n + j) % 2)
      sum += id[j];
    else
      sum += id[j] * 2 >= 10 ? id[j] * 2 - 9 : id[j] * 2;
  }
  return (known + sum) % 10 == 0;
}

void searchCadidate(int i) {
  if (i == undefined.size()) {
    if (isCollectID())
      result++;
  } else {
    /*
    for (int j = 0; j < n; j++)
      if (id[j] > 0)
        cout << id[j];
      else
        cout << '*';
    cout << endl;
    */
    for (int j = 0; j < m; j++) {
      id[undefined[i]] = a[j];
      searchCadidate(i + 1);
    }
  }
}

int main(int argc, char *argv[]) {
  string s;
  cin >> n;
  cin >> s;
  cin >> m;
  a = new int[m];
  for (int i = 0; i < m; i++)
    cin >> a[i];
  id = new int[n];
  for (int i = 0; i < n; i++)
    if (s[i] == '*') {
      id[i] = -1; // test
      undefined.push_back(i);
    } else {
      id[i] = s[i] - '0';
      if ((n + i) % 2)
        known += s[i] - '0';
      else
        known += id[i] * 2 >= 10 ? id[i] * 2 - 9 : id[i] * 2;
    }

  searchCadidate(0);

  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, m, *id, *a, result = 0;
vector<int> undefined;
bool isCollectID(void) {
  int sum = 0;
  for (int i = 0; i < n; i++)
    if ((n + i) % 2)
      sum += id[i];
    else
      sum += id[i] * 2 >= 10 ? id[i] * 2 - 9 : id[i] * 2;
  return sum % 10 == 0;
}

void searchCadidate(int i) {
  if (i == undefined.size()) {
    if (isCollectID())
      result++;
  } else {
    /*
    for (int j = 0; j < n; j++)
      if (id[j] > 0)
        cout << id[j];
      else
        cout << '*';
    cout << endl;
    */
    for (int j = 0; j < m; j++) {
      id[undefined[i]] = a[j];
      searchCadidate(i + 1);
    }
  }
}

int main(int argc, char *argv[]) {
  string s;
  cin >> n;
  cin >> s;
  cin >> m;
  a = new int[m];
  for (int i = 0; i < m; i++)
    cin >> a[i];
  id = new int[n];
  for (int i = 0; i < n; i++)
    if (s[i] == '*') {
      id[i] = -1; // test
      undefined.push_back(i);
    } else {
      id[i] = s[i] - '0';
    }

  searchCadidate(0);

  cout << result << endl;
  return 0;
}