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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 13;

string s[MAX_N];
vector<int> vec[MAX_N];
vector<char> c;
int dame[26];
int mp[26];
int cnt;
int n;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    cnt = 0;
    rep(i, n) { vec[i].clear(); }
    c.clear();
    rep(i, 26) { dame[i] = 0; }
    rep(i, n) { cin >> s[i]; }
    rep(i, n) {
      rep(j, s[i].length()) {
        if (j == 0 && s[i].length() > 1) {
          dame[(int)(s[i][j] - 'A')] = 1;
        }
        c.push_back(s[i][j]);
        vec[i].push_back((int)(s[i][j] - 'A'));
      }
    }
    sort(c.begin(), c.end());
    c.erase(unique(c.begin(), c.end()), c.end());
    rep(i, c.size()) { mp[(int)(c[i] - 'A')] = i; }
    vector<bool> v(10);
    fill(v.end() - c.size(), v.end(), true);
    do {
      vector<int> pm;
      for (int i = 0; i < 10; ++i) {
        if (v[i]) {
          pm.push_back(i);
        }
      }
      do {
        bool flag = false;
        rep(i, 26) {
          if (dame[i] > 0) {
            if (pm[mp[i]] == 0) {
              flag = true;
              break;
            }
          }
        }
        if (flag) {
          continue;
        }
        int a[MAX_N];
        rep(i, n) {
          ll hoge = 0;
          ll dig = 1;
          for (int j = vec[i].size() - 1; j >= 0; j--) {
            hoge += pm[mp[vec[i][j]]] * dig;
            dig *= 10LL;
          }
          a[i] = hoge;
        }
        ll sm = 0;
        rep(i, n - 1) { sm += a[i]; }
        if (a[n - 1] == sm) {
          cnt++;
        }
      } while (next_permutation(pm.begin(), pm.end()));
    } while (next_permutation(v.begin(), v.end()));
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 13;

string s[MAX_N];
vector<int> vec[MAX_N];
vector<char> c;
int dame[30];
int mp[30];
int bf[30];
int cnt;
int n;

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    cnt = 0;
    rep(i, n) { vec[i].clear(); }
    c.clear();
    rep(i, 30) { dame[i] = 0; }
    rep(i, n) { cin >> s[i]; }
    rep(i, n) {
      rep(j, s[i].length()) {
        if (j == 0 && s[i].length() > 1) {
          dame[(int)(s[i][j] - 'A')] = 1;
        }
        c.push_back(s[i][j]);
        vec[i].push_back((int)(s[i][j] - 'A'));
      }
    }
    sort(c.begin(), c.end());
    c.erase(unique(c.begin(), c.end()), c.end());
    rep(i, c.size()) { mp[(int)(c[i] - 'A')] = i; }
    vector<bool> v(10);
    fill(v.end() - c.size(), v.end(), true);
    do {
      vector<int> pm;
      for (int i = 0; i < 10; ++i) {
        if (v[i]) {
          pm.push_back(i);
        }
      }
      do {
        bool flag = false;
        rep(i, 30) {
          if (dame[i] > 0) {
            if (pm[mp[i]] == 0) {
              flag = true;
              break;
            }
          }
        }
        if (flag) {
          continue;
        }
        rep(i, 30) { bf[i] = pm[mp[i]]; }
        int a[MAX_N];
        rep(i, n) {
          ll hoge = 0;
          ll dig = 1;
          for (int j = vec[i].size() - 1; j >= 0; j--) {
            hoge += bf[vec[i][j]] * dig;
            dig *= 10LL;
          }
          a[i] = hoge;
        }
        ll sm = 0;
        rep(i, n - 1) { sm += a[i]; }
        if (a[n - 1] == sm) {
          cnt++;
        }
      } while (next_permutation(pm.begin(), pm.end()));
    } while (next_permutation(v.begin(), v.end()));
    printf("%d\\n", cnt);
  }
  return 0;
}