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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, k, ans;
vector<string> vs;
int m[15];
bool isfirst[30];
bool take[10];
int board[30];
int vl[30], vr[30];

inline void solve(int &p, vector<int> &v) {
  if (p == k) {
    bool ok = true;
    rep(i, 0, p) if (v[i] == 0 && isfirst[m[i]]) return;
    int l = 0, r = 0;
    rep(i, 0, p) l += v[i] * vl[m[i]];
    rep(i, 0, p) r += v[i] * vr[m[i]];
    if (l == r)
      ans++;
  } else {
    rep(i, 0, 10) {
      if (!take[i]) {
        take[i] = true;
        v[p] = i;
        board[m[p]] = v[p];
        p++;
        solve(p, v);
        p--;
        take[i] = false;
      }
    }
  }
}

signed main() {
  while (cin >> N, N) {
    memset(isfirst, false, sizeof(isfirst));
    vs.resize(N);

    k = 0;
    bool used[30] = {};
    rep(i, 0, N) {
      cin >> vs[i];
      rep(j, 0, vs[i].length()) {
        if (used[vs[i][j] - 'A'])
          continue;
        used[vs[i][j] - 'A'] = true;
        m[k++] = vs[i][j] - 'A';
      }

      if (vs[i].length() != 1)
        isfirst[vs[i][0] - 'A'] = true;
    }

    memset(vl, 0, sizeof(vl));
    memset(vr, 0, sizeof(vr));
    rep(i, 0, N) {
      int temp[30] = {};
      rep(j, 0, vs[i].length()) {
        rep(k, 0, 26) temp[k] *= 10;
        int c = vs[i][j] - 'A';
        temp[c]++;
      }
      rep(j, 0, 26) {
        if (i != N - 1)
          vl[j] += temp[j];
        else
          vr[j] += temp[j];
      }
    }

    ans = 0;
    int p = 0;
    vector<int> arr(k);
    solve(p, arr);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, k, ans;
vector<string> vs;
int m[15];
bool isfirst[30];
bool take[10];
int board[30];
int vl[30], vr[30];

inline void solve(int p, vector<int> &v) {
  if (p == k) {
    bool ok = true;
    rep(i, 0, p) if (v[i] == 0 && isfirst[m[i]]) return;
    int l = 0, r = 0;
    rep(i, 0, p) l += v[i] * vl[m[i]];
    rep(i, 0, p) r += v[i] * vr[m[i]];
    if (l == r)
      ans++;
  } else {
    rep(i, 0, 10) {
      if (!take[i]) {
        take[i] = true;
        v[p] = i;
        board[m[p]] = v[p];
        solve(p + 1, v);
        take[i] = false;
      }
    }
  }
}

signed main() {
  while (cin >> N, N) {
    memset(isfirst, false, sizeof(isfirst));
    vs.resize(N);

    k = 0;
    bool used[30] = {};
    rep(i, 0, N) {
      cin >> vs[i];
      rep(j, 0, vs[i].length()) {
        if (used[vs[i][j] - 'A'])
          continue;
        used[vs[i][j] - 'A'] = true;
        m[k++] = vs[i][j] - 'A';
      }

      if (vs[i].length() != 1)
        isfirst[vs[i][0] - 'A'] = true;
    }

    memset(vl, 0, sizeof(vl));
    memset(vr, 0, sizeof(vr));
    rep(i, 0, N) {
      int temp[30] = {};
      rep(j, 0, vs[i].length()) {
        rep(k, 0, 26) temp[k] *= 10;
        int c = vs[i][j] - 'A';
        temp[c]++;
      }
      rep(j, 0, 26) {
        if (i != N - 1)
          vl[j] += temp[j];
        else
          vr[j] += temp[j];
      }
    }

    ans = 0;
    vector<int> arr(k);
    solve(0, arr);
    cout << ans << endl;
  }
  return 0;
}