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

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define mp make_pair
#define pb push_back
#define EB emplace_back
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
constexpr double eps = 1e-14;
constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

int N;

ll TEN[10];

ll calc_TEN(int x) {
  ll ret = 1;
  for (int i = 0; i < x; i++)
    ret *= 10;
  return ret;
}

void solve() {
  vector<string> s(N);
  rep(i, N) cin >> s[i];
  set<char> st;
  rep(i, N) rep(j, sz(s[i])) st.insert(s[i][j]);
  int n = st.size();
  map<char, int> cton;
  map<int, char> ntoc;
  {
    int cnt = 0;
    for (char c : st)
      cton[c] = cnt++;
    for (char c : st)
      ntoc[cton[c]] = c;
  }

  vector<bool> okz(n, true);
  rep(i, N) {
    if (s[i].size() > 1) {
      okz[cton[s[i][0]]] = false;
    }
  }

  vector<vector<int>> calc(10, vi(10, 0));
  rep(i, N - 1) {
    string str = s[i];
    reverse(all(str));
    rep(j, str.size()) {
      char ch = str[j];
      calc[cton[ch]][j]++;
    }
  }

  vector<ll> g(n, 0);
  rep(i, n) {
    rep(j, 10) { g[i] += calc[i][j] * TEN[j]; }
  }

  int cnt = 0;
  for (int mask = 0; mask < (1 << 10); mask++) {
    if (__builtin_popcount(mask) != n)
      continue;
    vector<int> v;
    rep(i, 10) if (mask & (1 << i)) v.pb(i);
    vector<string> vs(N);
    do {
      bool check = true;
      rep(i, n) {
        if (v[i] == 0 && !okz[i]) {
          check = false;
          break;
        }
      }
      if (!check)
        continue;
      ll sum = 0;
      for (int i = 0; i < n; i++) {
        sum += v[i] * g[i];
      }

      ll sum2 = 0;
      for (int i = 0; i < s[N - 1].size(); i++) {
        int p = sz(s[N - 1]) - 1 - i;
        sum2 += TEN[p] * v[cton[s[N - 1][i]]];
      }
      if (sum == sum2)
        cnt++;
    } while (next_permutation(all(v)));
  }
  cout << cnt << endl;
}

int main() {
  for (int i = 0; i < 10; i++)
    TEN[i] = calc_TEN(i);
  while (cin >> N) {
    if (N == 0)
      break;
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define mp make_pair
#define pb push_back
#define EB emplace_back
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
constexpr double eps = 1e-14;
constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

int N;

ll TEN[10];

ll calc_TEN(int x) {
  ll ret = 1;
  for (int i = 0; i < x; i++)
    ret *= 10;
  return ret;
}

void solve() {
  vector<string> s(N);
  rep(i, N) cin >> s[i];
  set<char> st;
  rep(i, N) rep(j, sz(s[i])) st.insert(s[i][j]);
  int n = st.size();
  map<char, int> cton;
  map<int, char> ntoc;
  {
    int cnt = 0;
    for (char c : st)
      cton[c] = cnt++;
    for (char c : st)
      ntoc[cton[c]] = c;
  }

  vector<bool> okz(n, true);
  rep(i, N) {
    if (s[i].size() > 1) {
      okz[cton[s[i][0]]] = false;
    }
  }

  vector<vector<int>> calc(10, vi(10, 0));
  rep(i, N - 1) {
    string str = s[i];
    reverse(all(str));
    rep(j, str.size()) {
      char ch = str[j];
      calc[cton[ch]][j]++;
    }
  }

  int cnt = 0;
  for (int mask = 0; mask < (1 << 10); mask++) {
    if (__builtin_popcount(mask) != n)
      continue;
    vector<int> v;
    rep(i, 10) if (mask & (1 << i)) v.pb(i);
    vector<string> vs(N);
    do {
      bool check = true;
      rep(i, n) {
        if (v[i] == 0 && !okz[i]) {
          check = false;
          break;
        }
      }
      if (!check)
        continue;
      ll sum = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < 10; j++) {
          sum += TEN[j] * v[i] * calc[i][j];
        }
      }

      ll sum2 = 0;
      for (int i = 0; i < s[N - 1].size(); i++) {
        int p = sz(s[N - 1]) - 1 - i;
        sum2 += TEN[p] * v[cton[s[N - 1][i]]];
      }
      if (sum == sum2)
        cnt++;
    } while (next_permutation(all(v)));
  }
  cout << cnt << endl;
}

int main() {
  for (int i = 0; i < 10; i++)
    TEN[i] = calc_TEN(i);
  while (cin >> N) {
    if (N == 0)
      break;
    solve();
  }
}