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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

llint n, d;
string s[15];
map<char, llint> mp, mp2;
vector<llint> dvec;
vector<P> vec, vec2;
bool cant[15];
llint pop[1 << 10];

void dfs(llint pos, llint sum, llint mask) {
  if (pos == 5) {
    vec.push_back(P(mask, sum));
    return;
  }
  for (int i = 0; i < 10; i++) {
    if (mask & (1 << i))
      continue;
    if (pos == 0 && cant[i])
      continue;
    dfs(pos + 1, sum + dvec[i] * pos, mask | (1 << i));
  }
}
void dfs2(llint pos, llint sum, llint mask) {
  if (pos == 10) {
    vec2.push_back(P(mask, sum));
    return;
  }
  for (int i = 0; i < 10; i++) {
    if (mask & (1 << i))
      continue;
    dfs2(pos + 1, sum + dvec[i] * pos, mask | (1 << i));
  }
}

int main(void) {
  for (int i = 1; i < (1 << 10); i++)
    pop[i] = pop[i & (i - 1)] + 1;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> s[i];

    mp.clear(), mp2.clear();
    for (int i = 1; i <= n; i++) {
      if (s[i].size() >= 2)
        mp2[s[i][0]] = 1;
      reverse(s[i].begin(), s[i].end());
      llint mul = 1;
      for (int j = 0; j < s[i].size(); j++) {
        mp2[s[i][j]];
        if (i < n)
          mp[s[i][j]] += mul;
        else
          mp[s[i][j]] -= mul;
        mul *= 10;
      }
    }

    dvec.clear();
    d = mp.size();
    for (auto it = mp.begin(); it != mp.end(); it++)
      dvec.push_back(it->second);
    while (dvec.size() < 10)
      dvec.push_back(0);

    llint id = 0;
    for (int i = 0; i < 10; i++)
      cant[i] = false;
    for (auto it = mp2.begin(); it != mp2.end(); it++)
      cant[id++] = it->second;

    vec.clear(), vec2.clear();
    dfs(0, 0, 0);
    dfs2(5, 0, 0);

    sort(vec.begin(), vec.end());
    sort(vec2.begin(), vec2.end());

    llint ans = 0, D = 1 << 10;
    for (int i = 0; i < vec.size(); i++) {
      P p = P(D - 1 - vec[i].first, -vec[i].second);
      ans += upper_bound(vec2.begin(), vec2.end(), p) -
             lower_bound(vec2.begin(), vec2.end(), p);
    }
    for (int i = 1; i <= 10 - d; i++)
      ans /= i;
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int N;
string S[13];
vector<char> let;
char perm[10];
map<char, int> mp;

int replace(int s) {
  string tmp;
  vector<char>::iterator it;
  for (int i = 0; i < S[s].size(); i++) {
    tmp.push_back(perm[mp[S[s][i]]]);
    if (S[s].size() > 1 && tmp[0] == '0')
      return -1;
  }
  return atoi(tmp.c_str());
}

int fact(int i) {
  if (i == 0)
    return 1;
  else
    return i * fact(i - 1);
}

int main(void) {
  while (1) {
    cin >> N;
    if (N == 0)
      break;

    for (int i = 0; i < N; i++)
      cin >> S[i];

    let.clear();
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < S[i].size(); j++) {
        if (find(let.begin(), let.end(), S[i][j]) == let.end())
          let.push_back(S[i][j]);
      }
    }
    int t = let.size();

    mp.clear();
    for (int i = 0; i < t; i++)
      mp[let[i]] = i;

    for (int i = 0; i < 10; i++)
      perm[i] = '0' + i;

    int ans = 0;
    int val, sum;

    char mem = 'a';

    do {
      if (mem == perm[t - 1])
        continue;
      mem = perm[t - 1];

      sum = 0;
      for (int i = 0; i < N; i++) {
        val = replace(i);
        if (val < 0) {
          sum = 1;
          break;
        }
        if (i < N - 1)
          sum += val;
        else
          sum -= val;
      }
      if (sum == 0)
        ans++;
    } while (next_permutation(perm, perm + 10));

    cout << ans << endl;
  }

  return 0;
}