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

/* template.cpp {{{ */
#include <bits/stdc++.h>
using namespace std;
#define get_macro(a, b, c, d, name, ...) name
#define rep(...) get_macro(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...)                                                              \\
  get_macro(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define rep1(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, s) for (ll i = (a); i < (ll)(b); i += (ll)(s))
#define rrep1(n) rrep2(i_, n)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep3(i, a, b) rrep4(i, a, b, 1)
#define rrep4(i, a, b, s) for (ll i = (ll)(b)-1; i >= (ll)(a); i -= (ll)(s))
#define each(x, c) for (auto &&x : c)
#define fs first
#define sc second
#define all(c) begin(c), end(c)
using ui = unsigned;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
const ll mod = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[]{0, -1, 0, 1, -1, -1, 1, 1};
template <class T, class U> void chmin(T &x, const U &y) { x = min<T>(x, y); }
template <class T, class U> void chmax(T &x, const U &y) { x = max<T>(x, y); }
struct prepare_ {
  prepare_() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
  }
} prepare__;
/* }}} */

int n;
string s[12];
ll a[10];
bool ng[10];
string z;
map<ll, ll> s1[1 << 10], s2[1 << 10];

void dfs(int l, int r, int b, ll sm, map<ll, ll> mp[]) {
  if (l == r) {
    mp[b][sm]++;
    return;
  }
  rep(i, z.size()) {
    if (b >> i & 1)
      continue;
    if (l == 0 && ng[i])
      continue;
    dfs(l + 1, r, b | (1 << i), sm + a[i] * l, mp);
  }
  dfs(l + 1, r, b, sm, mp);
}

int main() {
  while (cin >> n, n) {
    memset(a, 0, sizeof(a));
    memset(ng, 0, sizeof(ng));
    z = "";
    rep(i, 1 << 10) s1[i].clear();
    rep(i, 1 << 10) s2[i].clear();

    rep(i, n) cin >> s[i], z += s[i];
    sort(all(z));
    z.erase(unique(all(z)), end(z));
    rep(i, n) {
      if (s[i].size() >= 2)
        ng[lower_bound(all(z), s[i][0]) - begin(z)] = true;
      reverse(all(s[i]));
      ll t = i + 1 < n ? 1 : -1;
      each(c, s[i]) {
        a[lower_bound(all(z), c) - begin(z)] += t;
        t *= 10;
      }
    }
    dfs(0, 5, 0, 0, s1);
    dfs(5, 10, 0, 0, s2);
    ll res = 0;
    rep(i, 1 << z.size()) {
      each(x, s1[i]) res += x.second * s2[~i & ((1 << z.size()) - 1)][-x.first];
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

int n;
string s[12];
int cv[256], cnt;
int us[10];

int dfs(int i, int bit) {
  if (i == cnt) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      if (s[j].size() > 1 && us[cv[s[j][0]]] == 0) {
        return 0;
      }
      int r = 0;
      for (int k = 0; k < s[j].size(); k++) {
        r = r * 10 + us[cv[s[j][k]]];
      }
      sum += j < n - 1 ? r : -r;
    }
    return !sum;
  }
  int res = 0;
  for (int j = 0; j < 10; j++) {
    if (bit & (1 << j))
      continue;
    us[i] = j;
    res += dfs(i + 1, bit | (1 << j));
  }
  return res;
}

int main() {
  while (cin >> n, n) {
    memset(cv, -1, sizeof(cv));
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      for (int j = 0; j < s[i].size(); j++) {
        if (cv[s[i][j]] == -1) {
          cv[s[i][j]] = cnt++;
        }
      }
    }
    cout << dfs(0, 0) << endl;
  }
}