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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class t> using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"
long long int powint(long long int a, long long int b) {
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int ans = 1;
  long long int aa = powint(a, b / 2);
  ans *= aa * aa;
  if (b % 2)
    ans *= a;
  return ans;
}

int check(vector<int> &nums) {
  int sum = 0;
  for (int i = 0; i < nums.size(); ++i) {
    int anum;
    if (i % 2)
      anum = nums[i] * 2;
    else
      anum = nums[i];

    if (anum >= 10)
      anum = anum / 10 + anum % 10;
    sum += anum;
  }
  return sum % 10 == 0;
}

int memo[10][2];

int getnum(int num, const bool iseven) { return memo[num][iseven]; }

int main() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 2; ++j) {
      memo[i][j] = j ? (i * 2) % 10 + (i * 2) / 10 : i;
    }
  }
  int N;
  cin >> N;
  vector<int> asta_doubles;
  int ini_sum = 0;
  int evennum = 0;
  int oddnum = 0;
  {
    string st;
    cin >> st;
    for (int i = 0; i < st.size(); ++i) {
      char c = st[i];
      if (c == '*') {
        asta_doubles.emplace_back((st.size() - i) % 2 == 0);
      } else
        ini_sum += getnum(c - '0', (st.size() - i) % 2 == 0);
    }
  }

  int M;
  cin >> M;
  vector<int> kouhos(M);
  for (int i = 0; i < M; ++i) {
    cin >> kouhos[i];
  }

  int ans = 0;
  for (int i = 0; i < powint(M, asta_doubles.size()); ++i) {
    int n(i);
    int asum = ini_sum;
    for (int j = 0; j < asta_doubles.size(); ++j) {
      int anum = kouhos[n % M];
      n /= M;
      asum += memo[anum][asta_doubles[j]];
    }

    ans += asum % 10 == 0;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class t> using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"
long long int powint(long long int a, long long int b) {
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int ans = 1;
  long long int aa = powint(a, b / 2);
  ans *= aa * aa;
  if (b % 2)
    ans *= a;
  return ans;
}

int check(vector<int> &nums) {
  int sum = 0;
  for (int i = 0; i < nums.size(); ++i) {
    int anum;
    if (i % 2)
      anum = nums[i] * 2;
    else
      anum = nums[i];

    if (anum >= 10)
      anum = anum / 10 + anum % 10;
    sum += anum;
  }
  return sum % 10 == 0;
}

int memo[10][2];

int getnum(int num, const bool iseven) { return memo[num][iseven]; }

int main() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 2; ++j) {
      memo[i][j] = j ? (i * 2) % 10 + (i * 2) / 10 : i;
    }
  }
  int N;
  cin >> N;
  vector<int> asta_doubles;
  int ini_sum = 0;
  int evennum = 0;
  int oddnum = 0;
  {
    string st;
    cin >> st;
    for (int i = 0; i < st.size(); ++i) {
      char c = st[i];
      if (c == '*') {
        if (i % 2)
          evennum++;
        else
          oddnum++;
        asta_doubles.emplace_back((st.size() - i) % 2 == 0);
      } else
        ini_sum += getnum(c - '0', i % 2);
    }
  }

  int M;
  cin >> M;
  vector<int> kouhos(M);
  for (int i = 0; i < M; ++i) {
    cin >> kouhos[i];
  }

  int ans = 0;
  for (int i = 0; i < powint(M, asta_doubles.size()); ++i) {
    int n(i);
    vector<int> reals(N);
    int asum = ini_sum;
    for (int j = 0; j < asta_doubles.size(); ++j) {
      int anum = kouhos[n % M];
      n /= M;
      asum += memo[anum][asta_doubles[j]];
    }

    ans += asum % 10 == 0;
  }
  cout << ans << endl;
  return 0;
}