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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int fac(int n) {
  if (n == 0)
    return 1;
  else
    return n * fac(n - 1);
}

int main() {
  int n;
  while (cin >> n, n) {
    vs v(n);
    REP(i, n) cin >> v[i];
    int var[26] = {};
    bool ex[26] = {};
    REP(i, n - 1) {
      int t = 1;
      REP(j, v[i].size()) {
        int c = v[i][v[i].size() - j - 1] - 'A';
        ex[c] = true;
        var[c] += t;
        t *= 10;
      }
    }
    int t = 1;
    REP(j, v.back().size()) {
      int c = v.back()[v.back().size() - j - 1] - 'A';
      ex[c] = true;
      var[c] -= t;
      t *= 10;
    }
    vi let, top;
    int rlet[26];
    t = 0;
    REP(i, 26) {
      if (ex[i]) {
        let.push_back(i);
        rlet[i] = t++;
      }
    }
    REP(i, v.size()) {
      if (v[i].size() == 1)
        continue;
      int c = v[i][0] - 'A';
      int ind = find(ALL(let), c) - let.begin();
      if (find(ALL(top), ind) != top.end())
        continue;
      top.push_back(ind);
    }
    int ans = 0;
    int per[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    do {
      bool f = false;
      REP(i, top.size()) {
        if (per[rlet[let[top[i]]]] == 0) {
          f = true;
          break;
        }
      }
      if (f)
        continue;
      int res = 0;
      REP(i, let.size()) {
        int tmp = var[let[i]] * per[rlet[let[i]]];
        res += tmp;
      }
      if (res == 0) {
        ans++;
      }
    } while (next_permutation(per, per + 10));
    cout << ans / fac(10 - let.size()) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int fact(int n) {
  if (n < 2)
    return 1;
  else
    return n * fact(n - 1);
}

int main() {
  int n;
  while (cin >> n, n) {
    vs v(n);
    REP(i, n) cin >> v[i];
    set<char> top, st;
    REP(i, n) if (v[i].size() > 1) top.insert(v[i][0]);
    REP(i, n) REP(j, v[i].size()) st.insert(v[i][j]);
    vector<char> vc(ALL(st));
    int ary[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int ans = 0;
    do {
      unordered_map<char, int> mp1;
      char zero;
      REP(i, vc.size()) {
        mp1[vc[i]] = ary[i];
        if (ary[i] == 0)
          zero = vc[i];
      }
      if (top.find(zero) != top.end())
        continue;
      int lhs = 0, rhs = 0;
      REP(i, n - 1) {
        int res = 0;
        REP(j, v[i].size()) {
          res *= 10;
          res += mp1[v[i][j]];
        }
        lhs += res;
      }
      REP(i, v.back().size()) {
        rhs *= 10;
        rhs += mp1[v.back()[i]];
      }
      if (lhs == rhs) {
        ans++;
      }
    } while (next_permutation(ary, ary + 10));
    cout << ans / fact(10 - vc.size()) << endl;
  }
  return 0;
}