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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " ["        \\
        << __LINE__ << "]" << endl)
struct DUMP : ostringstream {
  template <class T> DUMP &operator,(const T &t) {
    if (this->tellp())
      *this << ", ";
    *this << t;
    return *this;
  }
};
#else
#define dump(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == v.size() ? "" : " ");
  return os;
}
typedef vector<string> vs;

int a['Z' + 1];
bool nz['Z' + 1];
vector<char> app;
bool used[10];

int dfs(int k, int cur) {
  if (k == app.size())
    return cur == 0;
  int ans = 0;
  rep(i, 10) {
    if (used[i])
      continue;
    if (nz[app[k]] && i == 0)
      continue;
    used[i] = true;
    ans += dfs(k + 1, cur + i * a[app[k]]);
    used[i] = false;
  }
  return ans;
}

int main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  int n;
  while (cin >> n && n) {
    memset(a, 0, sizeof(a));
    memset(nz, 0, sizeof(nz));
    memset(used, 0, sizeof(used));
    app.clear();
    rep(i, n) {
      string s;
      cin >> s;
      int ten = 1;
      rep(j, s.size()) {
        char c = s[s.size() - 1 - j];
        a[c] += ten * (i == n - 1 ? -1 : 1);
        ten *= 10;
        app.pb(c);
      }
      if (s.size() != 1)
        nz[s[0]] = true;
    }
    sort(all(app));
    app.erase(unique(all(app)), app.end());
    cout << dfs(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " ["        \\
        << __LINE__ << "]" << endl)
struct DUMP : ostringstream {
  template <class T> DUMP &operator,(const T &t) {
    if (this->tellp())
      *this << ", ";
    *this << t;
    return *this;
  }
};
#else
#define dump(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == v.size() ? "" : " ");
  return os;
}
typedef vector<string> vs;

vector<char> app;
vi dic;
vs v;
int cnt;

int chk() {
  cnt++;
  int m['Z' + 1] = {};
  rep(i, app.size()) m[app[i]] = dic[i];
  vi trans(v.size());
  rep(i, v.size()) {
    string &x = v[i];
    if (x.size() != 1 && m[x[0]] == 0)
      return 0;
    int t = 0;
    rep(j, x.size()) {
      t *= 10;
      t += m[x[j]];
    }
    trans[i] = t;
  }
  int s = 0;
  rep(i, trans.size() - 1) s += trans[i];
  return s == trans.back();
}

bool used[10];

int dfs(int d) {
  if (d == app.size())
    return chk();
  int res = 0;
  rep(i, 10) {
    if (used[i])
      continue;
    dic[d] = i;
    used[i] = true;
    res += dfs(d + 1);
    used[i] = false;
  }
  return res;
}

int solve() {
  int n = v.size();
  app.clear();
  rep(i, n) rep(j, v[i].size()) app.pb(v[i][j]);
  sort(all(app));
  app.erase(unique(all(app)), app.end());
  if (app.size() >= 11)
    return 0;
  dic.assign(app.size(), 0);
  rep(i, 10) used[i] = 0;
  return dfs(0);
}

int main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  int n;
  while (cin >> n && n) {
    v.resize(n);
    rep(i, n) cin >> v[i];
    cnt = 0;
    cout << solve() << endl;
    dump(cnt);
  }
}