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

#include <bits/stdc++.h>
typedef int ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll n;
string s[200000];
ll used[100];
ll num[256];

ll al[10];

ll ans(ll *v, ll nn, ll d) {
  if (d >= nn) {
    /*rep(i,n) {
            if(num[s[i][0]] == 0 && s[i].size() > 1) {
                    return 0;
            }
    }*/

    ll sum = 0;
    rep(i, n - 1) {
      ll a = 0;
      rep(j, s[i].size()) {
        a *= 10;
        a += num[s[i][j]];
      }
      sum += a;
    }
    ll sn = 0;
    rep(i, s[n - 1].size()) {
      sn *= 10;
      sn += num[s[n - 1][i]];
    }
    return sum == sn;
  }

  ll ret = 0;
  rep(j, 10) {
    if (al[j])
      continue;
    bool f = false;
    if (j == 0) {
      rep(i, n) {
        if (s[i][0] == v[d] && s[i].size() > 1)
          f = true;
      }
    }
    if (f)
      continue;
    num[v[d]] = j;
    al[j] = true;
    ret += ans(v, nn, d + 1);
    al[j] = false;
  }

  return ret;
}

int main() {

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) cin >> s[i];

    rep(i, 26) used[i] = false;

    rep(i, n) {
      for (auto c : s[i]) {
        used[c - 'A'] = true;
      }
    }

    int v[100];
    ll c = 0;
    rep(i, 26) {
      if (used[i]) {
        v[c++] = i + 'A';
      }
    }

    cout << ans(v, c, 0) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll n;
string s[200000];
ll used[100];
ll num[256];

ll check(vector<ll> v, ll i, ll d) {
  if (d >= v.size()) {
    rep(i, n) {
      if (num[s[i][0]] == 0) {
        return 0;
      }
    }

    ll sum = 0;
    rep(i, n - 1) {
      ll a = 0;
      rep(j, s[i].size()) {
        a *= 10;
        a += num[s[i][j]];
      }
      sum += a;
    }
    ll sn = 0;
    rep(i, s[n - 1].size()) {
      sn *= 10;
      sn += num[s[n - 1][i]];
    }
    // cerr << sum << " " << sn << endl;
    return sum == sn;
  }

  ll ret = 0;
  FOR(j, i + 1, 10) {
    num[v[d]] = j;
    ret += check(v, j, d + 1);
  }
  return ret;
}

int main() {

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) cin >> s[i];

    rep(i, 26) used[i] = false;

    rep(i, n) {
      for (auto c : s[i]) {
        used[c - 'A'] = true;
      }
    }

    vector<ll> v;
    rep(i, 26) {
      if (used[i]) {
        v.pb(i + 'A');
      }
    }

    ll ans = 0;
    do {
      ans += check(v, -1, 0);
    } while (next_permutation(v.begin(), v.end()));
    cout << ans << endl;
  }

  return 0;
}