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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<int, int>
#define pb push_back

int N, l, used[10];
vector<P> v;

int pow10(int n) {
  if (n)
    return pow10(n - 1) * 10;
  return 1;
}

bool islarger(P a, P b) { return abs(a.first) > abs(b.first); }

int f(int n, int s) {
  if (n == l)
    return s ? 0 : 1;
  int res = 0;
  rep(i, 10) if (!used[i] && (i || !v[n].second)) {
    int x = s + v[n].first * i;
    if (x > 0) {
      rep2(j, n + 1, l) if (v[j].first < 0) x += v[j].first * 9;
      if (x > 0)
        continue;
    } else if (x < 0) {
      rep2(j, n + 1, l) if (v[j].first > 0) x += v[j].first * 9;
      if (x < 0)
        continue;
    }
    used[i] = 1;
    res += f(n + 1, s + v[n].first * i);
    used[i] = 0;
  }
  return res;
}

int main() {
  while (cin >> N && N) {
    map<char, int> M;
    map<char, int>::iterator it;
    set<char> S;
    rep(i, N) {
      string s;
      cin >> s;
      int l = s.size();
      if (l > 1)
        S.insert(s[0]);
      rep(j, l) M[s[j]] += pow10(l - 1 - j) * (i < N - 1 ? 1 : -1);
    }
    v.clear();
    for (it = M.begin(); it != M.end(); it++) {
      v.pb(P((*it).second, S.count((*it).first) ? 1 : 0));
    }
    sort(v.begin(), v.end(), islarger);
    l = v.size();
    fill(used, used + 10, 0);
    cout << f(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<int, int>
#define pb push_back

int N, l, used[10];
vector<int> v, top;

int pow10(int n) {
  if (n)
    return pow10(n - 1) * 10;
  return 1;
}

int f(int n, int s) {
  if (n == l)
    return s ? 0 : 1;
  int res = 0;
  rep(i, 10) if (!used[i] && (i || !top[n])) {
    used[i] = 1;
    res += f(n + 1, s + v[n] * i);
    used[i] = 0;
  }
  return res;
}

int main() {
  while (cin >> N && N) {
    map<char, int> M;
    map<char, int>::iterator it;
    set<char> S;
    rep(i, N) {
      string s;
      cin >> s;
      int l = s.size();
      if (l > 1)
        S.insert(s[0]);
      rep(j, l) M[s[j]] += pow10(l - 1 - j) * (i < N - 1 ? 1 : -1);
    }
    v.clear();
    top.clear();
    for (it = M.begin(); it != M.end(); it++) {
      v.pb((*it).second);
      top.pb(S.count((*it).first) ? 1 : 0);
    }
    l = v.size();
    fill(used, used + 10, 0);
    cout << f(0, 0) << endl;
  }
}